-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from visenze/feature/multisearch
[API-9945] Multisearch support
- Loading branch information
Showing
12 changed files
with
463 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/visenze/productsearch/AutoCompleteResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.visenze.productsearch; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.visenze.common.util.ViJsonMapper; | ||
import com.visenze.visearch.AutoCompleteResultItem; | ||
import com.visenze.visearch.ResponseMessages; | ||
import com.visenze.visearch.internal.InternalViSearchException; | ||
import com.visenze.visearch.internal.http.ViSearchHttpResponse; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by Hung on 24/11/23. | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class AutoCompleteResponse extends BaseProductSearchResponse { | ||
|
||
@JsonProperty("result") | ||
private List<AutoCompleteResultItem> result; | ||
|
||
/** | ||
* Delegated construction with a ViHttpResponse will automatically parse the | ||
* response into data members. | ||
* | ||
* Q: Why do we need to use a static function to create object instead of | ||
* calling constructor straight? | ||
* A: Because Java don't support self assignment. i.e. We cannot convert a | ||
* response json text body into T object from T's own constructor. | ||
* | ||
* @param response The ViHttpResponse received by calling ViHttpClient | ||
* api functions. | ||
* @return Product Search autocomplete response | ||
*/ | ||
public static AutoCompleteResponse fromResponse(ViSearchHttpResponse response) { | ||
try { | ||
return mapper.readValue(response.getBody(), AutoCompleteResponse.class); | ||
} catch(IOException e){ | ||
throw new InternalViSearchException(ResponseMessages.PARSE_RESPONSE_ERROR, e.getMessage()); | ||
} | ||
} | ||
|
||
public List<AutoCompleteResultItem> getResult() { | ||
return result; | ||
} | ||
|
||
public void setResult(List<AutoCompleteResultItem> result) { | ||
this.result = result; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
src/main/java/com/visenze/productsearch/BaseProductSearchResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.visenze.productsearch; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.visenze.common.util.ViJsonMapper; | ||
import com.visenze.productsearch.response.ErrorMsg; | ||
|
||
/** | ||
* Created by Hung on 24/11/23. | ||
*/ | ||
public abstract class BaseProductSearchResponse extends ViJsonMapper { | ||
|
||
/** | ||
* Each request is associated with an id to help with tracking api calls. | ||
* | ||
* The actual field name is called 'reqid' but we want to store it as | ||
* 'requestId' inside this class. The @JsonProperty("...") annotation tells | ||
* JSON that this variable 'requestId' will be serialized/deserialized as | ||
* 'reqid' from JSON's point of view. | ||
* i.e. some_json_map_thing["reqid"] = this_class.requestId; | ||
*/ | ||
@JsonProperty("reqid") | ||
private String requestId; | ||
|
||
/** | ||
* The request status, either “OK”, or “fail”. | ||
*/ | ||
@JsonProperty("status") | ||
private String status; | ||
|
||
/** | ||
* The result page number. Each response is tied to 1 'page' of a response. | ||
* Since there can be potentially 1000 results and we limit to 10 results at | ||
* a time, meaning 100 pages of 10 results each. This page number indicates | ||
* which page it is displaying. | ||
*/ | ||
@JsonProperty("page") | ||
private int page; | ||
|
||
/** | ||
* The number of results per page. Use to determine how many results we will | ||
* display one each 'page' at a time. | ||
*/ | ||
@JsonProperty("limit") | ||
private int limit; | ||
|
||
/** | ||
* Total number of search results. | ||
*/ | ||
@JsonProperty("total") | ||
private int total; | ||
|
||
/** | ||
* Error message and code if the request was not successful | ||
* i.e. when status is “fail”. | ||
*/ | ||
@JsonProperty("error") | ||
private ErrorMsg error; | ||
|
||
/** | ||
* Each request is associated with an id to help with tracking api calls. | ||
* | ||
* @return String representation of the request identifier | ||
*/ | ||
public String getRequestId() { | ||
return requestId; | ||
} | ||
|
||
/** | ||
* The request status, either “OK”, or “fail”. | ||
* | ||
* @return String status of the request sent | ||
*/ | ||
public String getStatus() { | ||
return status; | ||
} | ||
|
||
/** | ||
* Get Page number. | ||
* | ||
* @return The result page number | ||
*/ | ||
public int getPage() { | ||
return page; | ||
} | ||
|
||
/** | ||
* Get Results per page. | ||
* | ||
* @return The number of results per page | ||
*/ | ||
public int getLimit() { | ||
return limit; | ||
} | ||
|
||
/** | ||
* Get Number of results. | ||
* | ||
* @return The total number of results | ||
*/ | ||
public int getTotal() { | ||
return total; | ||
} | ||
|
||
/** | ||
* Get the error code. | ||
* | ||
* @return Map of error key-value pairs. | ||
*/ | ||
public ErrorMsg getError() { | ||
return error; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/visenze/visearch/AutoCompleteResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.visenze.visearch; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by Hung on 24/11/23. | ||
*/ | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class AutoCompleteResult extends PagedResult<AutoCompleteResultItem> { | ||
|
||
private String reqId; | ||
|
||
private String rawJson; | ||
|
||
public AutoCompleteResult(List<AutoCompleteResultItem> result) { | ||
this.result = result; | ||
} | ||
|
||
public AutoCompleteResult(String errorMessage, Throwable e, String rawResponse) { | ||
super.setErrorMessage(errorMessage); | ||
super.setCause(e); | ||
super.setRawResponseMessage(rawResponse); | ||
} | ||
|
||
public String getReqId() { | ||
return reqId; | ||
} | ||
|
||
public void setReqId(String reqId) { | ||
this.reqId = reqId; | ||
} | ||
|
||
public String getRawJson() { | ||
return rawJson; | ||
} | ||
|
||
public void setRawJson(String rawJson) { | ||
this.rawJson = rawJson; | ||
} | ||
} |
Oops, something went wrong.