Skip to content

Commit 58c47c9

Browse files
authored
[CSL-2107] Add get all redirect rules support (#113)
* Add get all redirect rules support * Format
1 parent 9f149e1 commit 58c47c9

File tree

9 files changed

+658
-14
lines changed

9 files changed

+658
-14
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.constructor.client;
2+
3+
/**
4+
* Constructor.io Get All Redirects Request
5+
*/
6+
public class AllRedirectsRequest {
7+
private int resultsPerPage;
8+
private int page;
9+
private int offset;
10+
private String status;
11+
private String query;
12+
13+
/**
14+
* Creates a All Redirects request
15+
*
16+
*/
17+
public AllRedirectsRequest() { }
18+
19+
/**
20+
* @param page the page of results to return
21+
*/
22+
public void setPage(int page) {
23+
this.page = page;
24+
}
25+
26+
/**
27+
* @return the page of results to return
28+
*/
29+
public int getPage() {
30+
return page;
31+
}
32+
33+
/**
34+
* @param resultsPerPage the number of redirects to return - maximum value 100
35+
*/
36+
public void setResultsPerPage(int resultsPerPage) {
37+
this.resultsPerPage = resultsPerPage;
38+
}
39+
40+
/**
41+
* @return the results per page
42+
*/
43+
public int getResultsPerPage() {
44+
return resultsPerPage;
45+
}
46+
47+
/**
48+
* @param offset the offset of results to return (can't be used with page)
49+
*/
50+
public void setOffset(int offset) {
51+
this.offset = offset;
52+
}
53+
54+
/**
55+
* @return the offset of results to return
56+
*/
57+
public int getOffset() {
58+
return offset;
59+
}
60+
61+
/**
62+
* @param status The status of redirects to return - 'current', 'pending', 'expired'
63+
*/
64+
public void setStatus(String status) {
65+
this.status = status;
66+
}
67+
68+
/**
69+
* @return the status
70+
*/
71+
public String getStatus() {
72+
return status;
73+
}
74+
75+
/**
76+
* @return the query
77+
*/
78+
public String getQuery() {
79+
return query;
80+
}
81+
82+
/**
83+
* @param query The query to use to filter against match pattern of redirects
84+
*/
85+
public void setQuery(String query) {
86+
this.query = query;
87+
}
88+
}

constructorio-client/src/main/java/io/constructor/client/ConstructorIO.java

Lines changed: 104 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,11 @@
1313

1414
import com.google.gson.Gson;
1515

16+
import io.constructor.client.models.*;
1617
import org.apache.commons.lang3.StringUtils;
1718
import org.json.JSONArray;
1819
import org.json.JSONObject;
1920

20-
import io.constructor.client.models.AutocompleteResponse;
21-
import io.constructor.client.models.BrowseResponse;
22-
import io.constructor.client.models.ItemsResponse;
23-
import io.constructor.client.models.SearchResponse;
24-
import io.constructor.client.models.RecommendationsResponse;
25-
import io.constructor.client.models.ServerError;
26-
import io.constructor.client.models.AllTasksResponse;
27-
import io.constructor.client.models.Task;
28-
import io.constructor.client.models.QuizQuestionResponse;
29-
import io.constructor.client.models.QuizResultsResponse;
30-
import io.constructor.client.models.VariationsResponse;
31-
import io.constructor.client.models.BrowseFacetOptionsResponse;
32-
import io.constructor.client.models.BrowseFacetsResponse;
3321
import okhttp3.Call;
3422
import okhttp3.Callback;
3523
import okhttp3.HttpUrl;
@@ -1614,7 +1602,7 @@ protected HttpUrl makeUrl(List<String> paths) throws UnsupportedEncodingExceptio
16141602
.addQueryParameter("key", this.apiKey)
16151603
.host(this.host);
16161604

1617-
if (!paths.contains("catalog") && !paths.contains("tasks") && !paths.contains("task")) {
1605+
if (!paths.contains("catalog") && !paths.contains("tasks") && !paths.contains("task") && !paths.contains("redirect_rules")) {
16181606
builder.addQueryParameter("c", this.version);
16191607
}
16201608

@@ -1848,6 +1836,17 @@ protected static AllTasksResponse createAllTasksResponse(String string) {
18481836
return new Gson().fromJson(transformed, AllTasksResponse.class);
18491837
}
18501838

1839+
/**
1840+
* Transforms a JSON string to a new JSON string for easy Gson parsing into an All Redirects response.
1841+
* Using JSON objects to achieve this is considerably less error prone than attempting to do it in
1842+
* a Gson Type Adapter.
1843+
*/
1844+
protected static AllRedirectsResponse createAllRedirectsResponse(String string) {
1845+
JSONObject json = new JSONObject(string);
1846+
String transformed = json.toString();
1847+
return new Gson().fromJson(transformed, AllRedirectsResponse.class);
1848+
}
1849+
18511850
/**
18521851
* Transforms a JSON string to a new JSON string for easy Gson parsing into a Task response.
18531852
* Using JSON objects to achieve this is considerably less error prone than attempting to do it in
@@ -2301,6 +2300,97 @@ public String taskAsJson(TaskRequest req) throws ConstructorException {
23012300
}
23022301
}
23032302

2303+
/**
2304+
* Creates a All Redirects OkHttp request
2305+
*
2306+
* @param req the All Redirects request
2307+
* @return a All Redirects OkHttp request
2308+
* @throws ConstructorException
2309+
*/
2310+
protected Request createAllRedirectsRequest(AllRedirectsRequest req) throws ConstructorException {
2311+
try {
2312+
List<String> paths = Arrays.asList("v1", "redirect_rules");
2313+
HttpUrl url = this.makeUrl(paths);
2314+
HttpUrl.Builder urlBuilder = url.newBuilder();
2315+
2316+
String resultsPerPage = String.valueOf(req.getResultsPerPage());
2317+
String status = req.getStatus();
2318+
String query = req.getQuery();
2319+
int offset = req.getOffset();
2320+
int page = req.getPage();
2321+
2322+
if (page != 0 && offset != 0) {
2323+
throw new IllegalArgumentException("page and offset cannot be used together");
2324+
}
2325+
2326+
if (page != 0) {
2327+
urlBuilder.addQueryParameter("page", String.valueOf(page));
2328+
}
2329+
2330+
if (resultsPerPage != null && !resultsPerPage.equals("0")) {
2331+
urlBuilder.addQueryParameter("num_results_per_page", resultsPerPage);
2332+
}
2333+
2334+
if (query != null) {
2335+
urlBuilder.addQueryParameter("query", query);
2336+
}
2337+
2338+
if (status != null) {
2339+
urlBuilder.addQueryParameter("status", status);
2340+
}
2341+
2342+
if (offset != 0) {
2343+
urlBuilder.addQueryParameter("offset", String.valueOf(offset));
2344+
}
2345+
2346+
url = urlBuilder.build();
2347+
2348+
Request request = this.makeAuthorizedRequestBuilder()
2349+
.url(url)
2350+
.get()
2351+
.build();
2352+
2353+
return request;
2354+
} catch (Exception exception) {
2355+
throw new ConstructorException(exception);
2356+
}
2357+
}
2358+
2359+
/**
2360+
* Queries the redirects service for all redirects
2361+
*
2362+
* @param req the all redirects request
2363+
* @return a all redirects response
2364+
* @throws ConstructorException if the request is invalid.
2365+
*/
2366+
public AllRedirectsResponse allRedirects(AllRedirectsRequest req) throws ConstructorException {
2367+
try {
2368+
Request request = createAllRedirectsRequest(req);
2369+
Response response = clientWithRetry.newCall(request).execute();
2370+
String json = getResponseBody(response);
2371+
return createAllRedirectsResponse(json);
2372+
} catch (Exception exception) {
2373+
throw new ConstructorException(exception);
2374+
}
2375+
}
2376+
2377+
/**
2378+
* Queries the redirects service for all redirects
2379+
*
2380+
* @param req the all redirects request
2381+
* @return a string of JSON
2382+
* @throws ConstructorException if the request is invalid.
2383+
*/
2384+
public String allRedirectsAsJson(AllRedirectsRequest req) throws ConstructorException {
2385+
try {
2386+
Request request = createAllRedirectsRequest(req);
2387+
Response response = clientWithRetry.newCall(request).execute();
2388+
return getResponseBody(response);
2389+
} catch (Exception exception) {
2390+
throw new ConstructorException(exception);
2391+
}
2392+
}
2393+
23042394
/**
23052395
* Creates a Quiz OkHttp request
23062396
*
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.constructor.client.models;
2+
3+
import java.util.List;
4+
5+
import com.google.gson.annotations.SerializedName;
6+
7+
/**
8+
* Constructor.io All Redirects Response ... uses Gson/Reflection to load data in
9+
*/
10+
public class AllRedirectsResponse {
11+
12+
@SerializedName("redirect_rules")
13+
private List<RedirectRule> redirectRules;
14+
15+
@SerializedName("total_count")
16+
private int totalCount;
17+
18+
/**
19+
* @return the redirect_rules
20+
*/
21+
public List<RedirectRule> getRedirectRules() {
22+
return redirectRules;
23+
}
24+
25+
/**
26+
* @return the total_count
27+
*/
28+
public int getTotalCount() {
29+
return totalCount;
30+
}
31+
32+
public void setRedirectRules(List<RedirectRule> redirectRules) {
33+
this.redirectRules = redirectRules;
34+
}
35+
36+
public void setTotalCount(int totalCount) {
37+
this.totalCount = totalCount;
38+
}
39+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package io.constructor.client.models;
2+
3+
import java.util.List;
4+
5+
import com.google.gson.annotations.SerializedName;
6+
7+
/**
8+
* Constructor.io Redirect Rule ... uses Gson/Reflection to load data in
9+
*/
10+
11+
public class RedirectRule {
12+
13+
@SerializedName("id")
14+
private int id;
15+
16+
@SerializedName("start_time")
17+
private String startTime;
18+
19+
@SerializedName("end_time")
20+
private String endTime;
21+
22+
@SerializedName("user_segments")
23+
private List<String> userSegments;
24+
25+
@SerializedName("metadata")
26+
private Object metadata;
27+
28+
@SerializedName("url")
29+
private String url;
30+
31+
@SerializedName("matches")
32+
private List<RedirectRuleMatch> matches;
33+
34+
/**
35+
* @return the id of the redirect
36+
*/
37+
public int getId() {
38+
return id;
39+
}
40+
41+
/**
42+
* @return the start_time of the redirect
43+
*/
44+
public String getStartTime() {
45+
return startTime;
46+
}
47+
48+
/**
49+
* @return the end_time of the redirect
50+
*/
51+
public String getEndTime() {
52+
return endTime;
53+
}
54+
55+
/**
56+
* @return the user_segments of the redirect
57+
*/
58+
public List<String> getUserSegments() {
59+
return userSegments;
60+
}
61+
62+
/**
63+
* @return the metadata of the redirect
64+
*/
65+
public Object getMetadata() {
66+
return metadata;
67+
}
68+
69+
/**
70+
* @return the url of the redirect
71+
*/
72+
public String getUrl() {
73+
return url;
74+
}
75+
76+
/**
77+
* @return the matches of the redirect
78+
*/
79+
public List<RedirectRuleMatch> getMatches() {
80+
return matches;
81+
}
82+
83+
public void setMatches(List<RedirectRuleMatch> matches) {
84+
this.matches = matches;
85+
}
86+
87+
public void setId(int id) {
88+
this.id = id;
89+
}
90+
91+
public void setUrl(String url) {
92+
this.url = url;
93+
}
94+
95+
public void setMetadata(Object metadata) {
96+
this.metadata = metadata;
97+
}
98+
99+
public void setUserSegments(List<String> userSegments) {
100+
this.userSegments = userSegments;
101+
}
102+
103+
public void setEndTime(String endTime) {
104+
this.endTime = endTime;
105+
}
106+
}

0 commit comments

Comments
 (0)