Skip to content

Commit

Permalink
feat (core): Provide URLParameter helper to add a query param to a list
Browse files Browse the repository at this point in the history
  • Loading branch information
JPPortier committed Dec 17, 2024
1 parent 94c0645 commit fd899b2
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 5 deletions.
13 changes: 12 additions & 1 deletion core/src/main/com/sinch/sdk/core/http/URLParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public enum STYLE {
SIMPLE,
SPACE_DELIMITED,
PIPE_DELIMITED,
DEEP_OBJECT
DEEP_OBJECT;
}

// the following constants do not follow java standard by purpose
// Aim is to have direct access to OpenApi Spec authorized values as constant without overhead
// ref: https://spec.openapis.org/oas/latest.html#style-values
public static final STYLE matrix = STYLE.MATRIX;
public static final STYLE label = STYLE.LABEL;
public static final STYLE form = STYLE.FORM;
public static final STYLE simple = STYLE.SIMPLE;
public static final STYLE spaceDelimited = STYLE.SPACE_DELIMITED;
public static final STYLE pipeDelimited = STYLE.PIPE_DELIMITED;
public static final STYLE deepObject = STYLE.DEEP_OBJECT;
}
25 changes: 23 additions & 2 deletions core/src/main/com/sinch/sdk/core/http/URLParameterUtils.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.sinch.sdk.core.http;

import com.sinch.sdk.core.databind.QueryParameterSerializer;
import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.http.URLParameter.STYLE;
import com.sinch.sdk.core.models.OptionalValue;
import com.sinch.sdk.core.utils.EnumDynamic;
import com.sinch.sdk.core.utils.StringUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -26,7 +30,7 @@ public static String encodeParametersAsString(Collection<URLParameter> parameter
.collect(Collectors.joining("&"));
}

public static Optional<String> encode(URLParameter parameter) {
protected static Optional<String> encode(URLParameter parameter) {

if (null == parameter
|| StringUtil.isEmpty(parameter.getName())
Expand Down Expand Up @@ -195,12 +199,29 @@ private static Optional<String> encodeObject(URLParameter parameter) {
throw new ApiException("Not yet implemented '" + parameter + "'");
}

public static String encodeParameterValue(String value) {
private static String encodeParameterValue(String value) {

try {
return URLEncoder.encode(value, "UTF-8").replaceAll("\\.", "%2E");
} catch (UnsupportedEncodingException e) {
return value;
}
}

public static <T> void addQueryParam(
OptionalValue<T> optionalValue,
String paramName,
STYLE paramStyle,
QueryParameterSerializer<T, ?> serializer,
List<URLParameter> queryParametersList,
boolean explode) {
optionalValue.ifPresent(
value ->
queryParametersList.add(
new URLParameter(
paramName,
null == serializer ? value : serializer.apply(value),
paramStyle,
explode)));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.sinch.sdk.core.databind.query_parameter;

import static org.junit.jupiter.api.Assertions.*;

import com.sinch.sdk.core.TestHelpers;
import java.time.Instant;
import org.junit.jupiter.api.Assertions;
Expand Down
44 changes: 44 additions & 0 deletions core/src/test/java/com/sinch/sdk/core/http/URLParameterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.sinch.sdk.core.http;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.sinch.sdk.core.http.URLParameter.STYLE;
import org.junit.jupiter.api.Test;

class URLParameterTest {

@Test
void matrix() {
assertEquals(STYLE.MATRIX, URLParameter.matrix);
}

@Test
void label() {
assertEquals(STYLE.LABEL, URLParameter.label);
}

@Test
void form() {
assertEquals(STYLE.FORM, URLParameter.form);
}

@Test
void simple() {
assertEquals(STYLE.SIMPLE, URLParameter.simple);
}

@Test
void spaceDelimited() {
assertEquals(STYLE.SPACE_DELIMITED, URLParameter.spaceDelimited);
}

@Test
void pipeDelimited() {
assertEquals(STYLE.PIPE_DELIMITED, URLParameter.pipeDelimited);
}

@Test
void deepObject() {
assertEquals(STYLE.DEEP_OBJECT, URLParameter.deepObject);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.sinch.sdk.core.http;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.sinch.sdk.core.TestHelpers;
import com.sinch.sdk.core.databind.query_parameter.InstantToIso8601Serializer;
import com.sinch.sdk.core.exceptions.ApiException;
import com.sinch.sdk.core.http.URLParameter.STYLE;
import com.sinch.sdk.core.models.OptionalValue;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -374,4 +382,51 @@ void encodePipeDelimitedArrayExplode() {
URLParameter.STYLE.PIPE_DELIMITED,
true)));
}

@Test
void addQueryParamNotPresent() {

// Collections.emptyList() is not modifiable and will throw an exception if trying to add an
// entry
assertDoesNotThrow(
() ->
URLParameterUtils.addQueryParam(
OptionalValue.empty(),
"foo name",
STYLE.DEEP_OBJECT,
null,
Collections.emptyList(),
false),
"Ignored empty value");
}

@Test
void addQueryParamInteger() {
ArrayList<URLParameter> list = new ArrayList<>();

URLParameterUtils.addQueryParam(
OptionalValue.of(15), "foo name", STYLE.DEEP_OBJECT, null, list, true);
assertEquals(1, list.size());

TestHelpers.recursiveEquals(
list.get(0), new URLParameter("foo name", 15, STYLE.DEEP_OBJECT, true));
}

@Test
void addQueryParamInstant() {
ArrayList<URLParameter> list = new ArrayList<>();

URLParameterUtils.addQueryParam(
OptionalValue.of(Instant.parse("2024-05-04T10:00:00.123Z")),
"foo name",
STYLE.DEEP_OBJECT,
InstantToIso8601Serializer.getInstance(),
list,
true);
assertEquals(1, list.size());

TestHelpers.recursiveEquals(
list.get(0),
new URLParameter("foo name", "2024-05-04T10:00:00.123Z", STYLE.DEEP_OBJECT, true));
}
}

0 comments on commit fd899b2

Please sign in to comment.