Skip to content

Commit

Permalink
add RestRequestBuilder, add global header, query and path parameters,…
Browse files Browse the repository at this point in the history
… remove dependency on domino-history
  • Loading branch information
vegegoku committed Nov 6, 2024
1 parent 2c723e9 commit 237ea58
Show file tree
Hide file tree
Showing 9 changed files with 589 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import static java.util.Objects.nonNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dominokit.jackson.JacksonContextProvider;
import org.dominokit.rest.js.DefaultServiceRoot;
import org.dominokit.rest.js.ServerEventFactory;
Expand Down Expand Up @@ -69,6 +71,10 @@ public class DominoRestConfig implements RestConfig {

private static NullQueryParamStrategy nullQueryParamStrategy = NullQueryParamStrategy.EMPTY;

private static final Map<String, String> globalPathParams = new HashMap<>();
private static final Map<String, String> globalHeaderParams = new HashMap<>();
private static final Map<String, List<String>> globalQueryParams = new HashMap<>();

/**
* Gets and initialize the instance with the default configurations
*
Expand Down Expand Up @@ -270,4 +276,78 @@ public UrlTokenRegexMatcher getUrlTokenRegexMatcher() {
return url;
};
}

@Override
public Map<String, String> getGlobalPathParameters() {
return globalPathParams;
}

@Override
public Map<String, String> getGlobalHeaderParameters() {
return globalHeaderParams;
}

@Override
public Map<String, List<String>> getGlobalQueryParameters() {
return globalQueryParams;
}

@Override
public RestConfig setGlobalPathParameter(String name, String value) {
getGlobalPathParameters().put(name, value);
return this;
}

@Override
public RestConfig setGlobalPathParameters(Map<String, String> pathParameters) {
getGlobalPathParameters().putAll(pathParameters);
return this;
}

@Override
public RestConfig setGlobalHeaderParameter(String name, String value) {
getGlobalHeaderParameters().put(name, value);
return this;
}

@Override
public RestConfig setGlobalHeaderParameters(Map<String, String> headerParameters) {
getGlobalHeaderParameters().putAll(headerParameters);
return this;
}

@Override
public RestConfig setGlobalQueryParameter(String name, String value) {
getGlobalQueryParameters().put(name, new ArrayList<>());
addGlobalQueryParameter(name, value);
return this;
}

@Override
public RestConfig addGlobalQueryParameter(String name, String value) {
if (getGlobalQueryParameters().containsKey(name)) {
getGlobalQueryParameters().get(name).add(value);
} else {
setGlobalQueryParameter(name, value);
}
return this;
}

@Override
public RestConfig setGlobalQueryParameters(Map<String, List<String>> parameters) {
parameters
.keySet()
.forEach(
name -> parameters.get(name).forEach(value -> addGlobalQueryParameter(name, value)));
return this;
}

@Override
public RestConfig addGlobalQueryParameters(Map<String, List<String>> parameters) {
parameters.forEach(
(key, values) -> {
values.forEach(value -> addGlobalQueryParameter(key, value));
});
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -70,6 +72,10 @@ public class DominoRestConfig implements RestConfig {

private static NullQueryParamStrategy nullQueryParamStrategy = NullQueryParamStrategy.EMPTY;

private static final Map<String, String> globalPathParams = new HashMap<>();
private static final Map<String, String> globalHeaderParams = new HashMap<>();
private static final Map<String, List<String>> globalQueryParams = new HashMap<>();

/**
* Gets and initialize the instance with the default configurations
*
Expand Down Expand Up @@ -270,4 +276,78 @@ public UrlTokenRegexMatcher getUrlTokenRegexMatcher() {
return url;
};
}

@Override
public Map<String, String> getGlobalPathParameters() {
return globalPathParams;
}

@Override
public Map<String, String> getGlobalHeaderParameters() {
return globalHeaderParams;
}

@Override
public Map<String, List<String>> getGlobalQueryParameters() {
return globalQueryParams;
}

@Override
public RestConfig setGlobalPathParameter(String name, String value) {
getGlobalPathParameters().put(name, value);
return this;
}

@Override
public RestConfig setGlobalPathParameters(Map<String, String> pathParameters) {
getGlobalPathParameters().putAll(pathParameters);
return this;
}

@Override
public RestConfig setGlobalHeaderParameter(String name, String value) {
getGlobalHeaderParameters().put(name, value);
return this;
}

@Override
public RestConfig setGlobalHeaderParameters(Map<String, String> headerParameters) {
getGlobalHeaderParameters().putAll(headerParameters);
return this;
}

@Override
public RestConfig setGlobalQueryParameter(String name, String value) {
getGlobalQueryParameters().put(name, new ArrayList<>());
addGlobalQueryParameter(name, value);
return this;
}

@Override
public RestConfig addGlobalQueryParameter(String name, String value) {
if (getGlobalQueryParameters().containsKey(name)) {
getGlobalQueryParameters().get(name).add(value);
} else {
setGlobalQueryParameter(name, value);
}
return this;
}

@Override
public RestConfig setGlobalQueryParameters(Map<String, List<String>> parameters) {
parameters
.keySet()
.forEach(
name -> parameters.get(name).forEach(value -> addGlobalQueryParameter(name, value)));
return this;
}

@Override
public RestConfig addGlobalQueryParameters(Map<String, List<String>> parameters) {
parameters.forEach(
(key, values) -> {
values.forEach(value -> addGlobalQueryParameter(key, value));
});
return this;
}
}
5 changes: 0 additions & 5 deletions domino-rest-shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@
<artifactId>domino-aggregator-shared</artifactId>
<version>${domino.aggregator.version}</version>
</dependency>
<dependency>
<groupId>org.dominokit</groupId>
<artifactId>domino-history-shared</artifactId>
<version>${domino.history.version}</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.dominokit.rest.shared.request.service.annotations.DateFormat;

/** The global configurations for domino rest. */
Expand Down Expand Up @@ -176,6 +177,28 @@ default NullQueryParamStrategy getNullQueryParamStrategy() {

UrlTokenRegexMatcher getUrlTokenRegexMatcher();

Map<String, String> getGlobalPathParameters();

Map<String, String> getGlobalHeaderParameters();

Map<String, List<String>> getGlobalQueryParameters();

RestConfig setGlobalPathParameter(String name, String value);

RestConfig setGlobalPathParameters(Map<String, String> pathParameters);

RestConfig setGlobalHeaderParameter(String name, String value);

RestConfig setGlobalHeaderParameters(Map<String, String> headerParameters);

RestConfig setGlobalQueryParameter(String name, String value);

RestConfig addGlobalQueryParameter(String name, String value);

RestConfig setGlobalQueryParameters(Map<String, List<String>> parameters);

RestConfig addGlobalQueryParameters(Map<String, List<String>> parameters);

/** Formatter to format the date parameter based on a patter */
@FunctionalInterface
interface DateParamFormatter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ public void normalizeUrl() {
(isNull(this.serviceRoot) || this.serviceRoot.isEmpty())
? ServiceRootMatcher.matchedServiceRoot(path)
: (this.serviceRoot + path);
UrlFormatter urlFormatter = new UrlFormatter(pathParameters);
Map<String, String> combinedParams = new HashMap<>();
combinedParams.putAll(DominoRestContext.make().getConfig().getGlobalPathParameters());
combinedParams.putAll(pathParameters);
UrlFormatter urlFormatter = new UrlFormatter(combinedParams);
this.setUrl(urlFormatter.formatUrl(root));
}
}
Expand Down
Loading

0 comments on commit 237ea58

Please sign in to comment.