Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.constructor.client;

/**
* Constructor.io Get All Redirects Request
*/
public class AllRedirectsRequest {
private int resultsPerPage;
private int page;
private int offset;
private String status;
private String query;

/**
* Creates a All Redirects request
*
*/
public AllRedirectsRequest() { }

/**
* @param page the page of results to return
*/
public void setPage(int page) {
this.page = page;
}

/**
* @return the page of results to return
*/
public int getPage() {
return page;
}

/**
* @param resultsPerPage the number of redirects to return - maximum value 100
*/
public void setResultsPerPage(int resultsPerPage) {
this.resultsPerPage = resultsPerPage;
}

/**
* @return the results per page
*/
public int getResultsPerPage() {
return resultsPerPage;
}

/**
* @param offset the offset of results to return (can't be used with page)
*/
public void setOffset(int offset) {
this.offset = offset;
}

/**
* @return the offset of results to return
*/
public int getOffset() {
return offset;
}

/**
* @param status The status of redirects to return - 'current', 'pending', 'expired'
*/
public void setStatus(String status) {
this.status = status;
}

/**
* @return the status
*/
public String getStatus() {
return status;
}

/**
* @return the query
*/
public String getQuery() {
return query;
}

/**
* @param query The query to use to filter against match pattern of redirects
*/
public void setQuery(String query) {
this.query = query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,11 @@

import com.google.gson.Gson;

import io.constructor.client.models.*;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import io.constructor.client.models.AutocompleteResponse;
import io.constructor.client.models.BrowseResponse;
import io.constructor.client.models.ItemsResponse;
import io.constructor.client.models.SearchResponse;
import io.constructor.client.models.RecommendationsResponse;
import io.constructor.client.models.ServerError;
import io.constructor.client.models.AllTasksResponse;
import io.constructor.client.models.Task;
import io.constructor.client.models.QuizQuestionResponse;
import io.constructor.client.models.QuizResultsResponse;
import io.constructor.client.models.VariationsResponse;
import io.constructor.client.models.BrowseFacetOptionsResponse;
import io.constructor.client.models.BrowseFacetsResponse;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
Expand Down Expand Up @@ -1614,7 +1602,7 @@ protected HttpUrl makeUrl(List<String> paths) throws UnsupportedEncodingExceptio
.addQueryParameter("key", this.apiKey)
.host(this.host);

if (!paths.contains("catalog") && !paths.contains("tasks") && !paths.contains("task")) {
if (!paths.contains("catalog") && !paths.contains("tasks") && !paths.contains("task") && !paths.contains("redirect_rules")) {
builder.addQueryParameter("c", this.version);
}

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

/**
* Transforms a JSON string to a new JSON string for easy Gson parsing into an All Redirects response.
* Using JSON objects to achieve this is considerably less error prone than attempting to do it in
* a Gson Type Adapter.
*/
protected static AllRedirectsResponse createAllRedirectsResponse(String string) {
JSONObject json = new JSONObject(string);
String transformed = json.toString();
return new Gson().fromJson(transformed, AllRedirectsResponse.class);
}

/**
* Transforms a JSON string to a new JSON string for easy Gson parsing into a Task response.
* Using JSON objects to achieve this is considerably less error prone than attempting to do it in
Expand Down Expand Up @@ -2301,6 +2300,97 @@ public String taskAsJson(TaskRequest req) throws ConstructorException {
}
}

/**
* Creates a All Redirects OkHttp request
*
* @param req the All Redirects request
* @return a All Redirects OkHttp request
* @throws ConstructorException
*/
protected Request createAllRedirectsRequest(AllRedirectsRequest req) throws ConstructorException {
try {
List<String> paths = Arrays.asList("v1", "redirect_rules");
HttpUrl url = this.makeUrl(paths);
HttpUrl.Builder urlBuilder = url.newBuilder();

String resultsPerPage = String.valueOf(req.getResultsPerPage());
String status = req.getStatus();
String query = req.getQuery();
int offset = req.getOffset();
int page = req.getPage();

if (page != 0 && offset != 0) {
throw new IllegalArgumentException("page and offset cannot be used together");
}

if (page != 0) {
urlBuilder.addQueryParameter("page", String.valueOf(page));
}

if (resultsPerPage != null && !resultsPerPage.equals("0")) {
urlBuilder.addQueryParameter("num_results_per_page", resultsPerPage);
}

if (query != null) {
urlBuilder.addQueryParameter("query", query);
}

if (status != null) {
urlBuilder.addQueryParameter("status", status);
}

if (offset != 0) {
urlBuilder.addQueryParameter("offset", String.valueOf(offset));
}

url = urlBuilder.build();

Request request = this.makeAuthorizedRequestBuilder()
.url(url)
.get()
.build();

return request;
} catch (Exception exception) {
throw new ConstructorException(exception);
}
}

/**
* Queries the redirects service for all redirects
*
* @param req the all redirects request
* @return a all redirects response
* @throws ConstructorException if the request is invalid.
*/
public AllRedirectsResponse allRedirects(AllRedirectsRequest req) throws ConstructorException {
try {
Request request = createAllRedirectsRequest(req);
Response response = clientWithRetry.newCall(request).execute();
String json = getResponseBody(response);
return createAllRedirectsResponse(json);
} catch (Exception exception) {
throw new ConstructorException(exception);
}
}

/**
* Queries the redirects service for all redirects
*
* @param req the all redirects request
* @return a string of JSON
* @throws ConstructorException if the request is invalid.
*/
public String allRedirectsAsJson(AllRedirectsRequest req) throws ConstructorException {
try {
Request request = createAllRedirectsRequest(req);
Response response = clientWithRetry.newCall(request).execute();
return getResponseBody(response);
} catch (Exception exception) {
throw new ConstructorException(exception);
}
}

/**
* Creates a Quiz OkHttp request
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.constructor.client.models;

import java.util.List;

import com.google.gson.annotations.SerializedName;

/**
* Constructor.io All Redirects Response ... uses Gson/Reflection to load data in
*/
public class AllRedirectsResponse {

@SerializedName("redirect_rules")
private List<RedirectRule> redirectRules;

@SerializedName("total_count")
private int totalCount;

/**
* @return the redirect_rules
*/
public List<RedirectRule> getRedirectRules() {
return redirectRules;
}

/**
* @return the total_count
*/
public int getTotalCount() {
return totalCount;
}

public void setRedirectRules(List<RedirectRule> redirectRules) {
this.redirectRules = redirectRules;
}

public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package io.constructor.client.models;

import java.util.List;

import com.google.gson.annotations.SerializedName;

/**
* Constructor.io Redirect Rule ... uses Gson/Reflection to load data in
*/

public class RedirectRule {

@SerializedName("id")
private int id;

@SerializedName("start_time")
private String startTime;

@SerializedName("end_time")
private String endTime;

@SerializedName("user_segments")
private List<String> userSegments;

@SerializedName("metadata")
private Object metadata;

@SerializedName("url")
private String url;

@SerializedName("matches")
private List<RedirectRuleMatch> matches;

/**
* @return the id of the redirect
*/
public int getId() {
return id;
}

/**
* @return the start_time of the redirect
*/
public String getStartTime() {
return startTime;
}

/**
* @return the end_time of the redirect
*/
public String getEndTime() {
return endTime;
}

/**
* @return the user_segments of the redirect
*/
public List<String> getUserSegments() {
return userSegments;
}

/**
* @return the metadata of the redirect
*/
public Object getMetadata() {
return metadata;
}

/**
* @return the url of the redirect
*/
public String getUrl() {
return url;
}

/**
* @return the matches of the redirect
*/
public List<RedirectRuleMatch> getMatches() {
return matches;
}

public void setMatches(List<RedirectRuleMatch> matches) {
this.matches = matches;
}

public void setId(int id) {
this.id = id;
}

public void setUrl(String url) {
this.url = url;
}

public void setMetadata(Object metadata) {
this.metadata = metadata;
}

public void setUserSegments(List<String> userSegments) {
this.userSegments = userSegments;
}

public void setEndTime(String endTime) {
this.endTime = endTime;
}
}
Loading