Skip to content

Commit

Permalink
make path public
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed May 3, 2024
1 parent 43005f1 commit b4f10d7
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions unirest/src/main/java/kong/unirest/core/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,26 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Path {
/**
* Class for building a URI with query params
*/
public class Path {
private String url;
private String rawPath;

/**
* construct a path
* @param url the URL
*/
public Path(String url) {
this(url, null);
}

/**
* Construct a path with a URL that could be relative and a default base for it
* @param url the url
* @param defaultBasePath the default base
*/
Path(String url, String defaultBasePath) {
if(defaultBasePath != null && url != null && !url.toLowerCase().startsWith("http")){
String full = defaultBasePath + url;
Expand All @@ -48,14 +64,19 @@ class Path {
}
}

public Path(String url) {
this(url, null);
}

/**
* replace path params designated with curley braces with a value
* @param params a map of param names and values
*/
public void param(Map<String, Object> params) {
params.forEach((key, value) -> param(key, String.valueOf(value)));
}

/**
* replace a single path param by name
* @param name the name of the path param
* @param value the value to replace it with
*/
public void param(String name, String value) {
Matcher matcher = Pattern.compile("\\{" + name + "\\}").matcher(url);
if (!matcher.find()) {
Expand All @@ -71,12 +92,22 @@ private String encodePath(String value) {
return Util.encode(value).replaceAll("\\+", "%20");
}

/**
* Add a query param. This will result in a query param per value
* @param name the name
* @param value a collection of values
*/
public void queryString(String name, Collection<?> value){
for (Object cur : value) {
queryString(name, cur);
}
}

/**
* Add a query param
* @param name the name
* @param value the value
*/
public void queryString(String name, Object value) {
StringBuilder queryString = new StringBuilder();
if (url.contains("?")) {
Expand All @@ -95,6 +126,10 @@ public void queryString(String name, Object value) {
url += queryString.toString();
}

/**
* Add query params as a map of key/values
* @param parameters the params to add
*/
public void queryString(Map<String, Object> parameters) {
if (parameters != null) {
for (Map.Entry<String, Object> param : parameters.entrySet()) {
Expand All @@ -112,17 +147,26 @@ private String escape(String string) {
return string.replaceAll(" ", "%20").replaceAll("\t", "%09");
}

/**
* @return the full raw path
*/
public String rawPath() {
return rawPath;
}

/**
* @return the URL without the query string
*/
public String baseUrl() {
if(url != null && url.contains("?")){
return url.substring(0, url.indexOf("?"));
}
return url;
}

/**
* @return just the query string
*/
public String getQueryString(){
return url.substring(url.indexOf("?")+1);
}
Expand Down

0 comments on commit b4f10d7

Please sign in to comment.