Skip to content

Commit

Permalink
1.2
Browse files Browse the repository at this point in the history
- better default handling of request body. Ability to override
- automaticly set content-type header on JSON client
  • Loading branch information
ChuckJonas committed Mar 1, 2019
1 parent b2e9272 commit eda23cf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
32 changes: 24 additions & 8 deletions src/classes/BaseHttpClient.cls
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Base Client v1.1
* Base Client v1.2
* giturl: https://github.com/ChuckJonas/BaseHttpClient
*/
public abstract class BaseHttpClient {
protected String baseProductionEndpoint;
Expand Down Expand Up @@ -36,30 +37,45 @@ public abstract class BaseHttpClient {
* @param baseEndpoint: the base endpoint to send all request to (typically HOST)
*/
public BaseHttpClient(String baseEndpoint) {
this.baseProductionEndpoint = baseEndpoint;
}
this.baseProductionEndpoint = baseEndpoint;
}

/**
* @description: build request -> send request -> check response -> parse response body (optional)
* @param method: HTTP Method to send
* @param uri: the URI. Realitive to base endpoint
* @param qryParams: url query params to add to URI
* @param body: Request Body
* @param body: Request Body. Can be string, blob or other. If other, will automaticly call JSON.serialize
* @return The HttpResponse object
*/
protected virtual HttpResponse request(String method, String uri,
Map<String, String> qryParams, String body){
Map<String, String> qryParams, Object body){

uri = buildEndpoint(uri, qryParams);
HttpRequest req = genRequest(method, uri);
if(body != null){
req.setBody(body);
}
setRequestBody(req, body);
HttpResponse resp = sendRequest(req);
checkResponse(req, resp);
return resp;
}

/**
* @description: sets the request body based on the incoming object type
* @param req: HttpRequest req to set the body on
* @param body: Request Body. Can be string, blob or other. If other, will automaticly call JSON.serialize
* @return Void. Mutates req.body
*/
protected virtual void setRequestBody(HttpRequest req, Object body){
if(body != null){
if(body instanceOf String){
req.setBody((String)body);
}else if(body instanceOf Blob){
req.setBodyAsBlob((Blob)body);
}else{
req.setBody(JSON.serialize(body));
}
}
}

/** FIRE! */
protected virtual HttpResponse sendRequest(HttpRequest req){
Expand Down
1 change: 1 addition & 0 deletions src/classes/BaseHttpClientTests.cls
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//test coverage could be improved, but actual implementation tests should will validate base functionality
// giturl: https://github.com/ChuckJonas/BaseHttpClient
@isTest
private class BaseHttpClientTests {

Expand Down
13 changes: 10 additions & 3 deletions src/classes/BaseHttpJsonClient.cls
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* Base JSON Client v1.1
* Base JSON Client v1.2
* giturl: https://github.com/ChuckJonas/BaseHttpClient
*/
public abstract class BaseHttpJsonClient extends BaseHttpClient {

public BaseHttpJsonClient(String baseEndpoint) {
super(baseEndpoint);
}
}

/**
* @description: build request -> send request -> check response -> parse response body (optional)
Expand All @@ -17,7 +18,7 @@ public abstract class BaseHttpJsonClient extends BaseHttpClient {
* @return The parsed object (if appliciable)
*/
protected virtual Object request(String method, String uri,
Map<String, String> qryParams, String body, Type returnType){
Map<String, String> qryParams, Object body, Type returnType){

HttpResponse resp = super.request(method, uri, qryParams, body);
try{
Expand All @@ -30,6 +31,12 @@ public abstract class BaseHttpJsonClient extends BaseHttpClient {
}
}

protected virtual override HttpRequest genRequest(String method, String uri){
HttpRequest req = super.genRequest(method,uri);
req.setHeader('Content-Type', 'application/json');
return req;
}

/* parse JSON/XML/Other to response DTO type */
protected virtual Object parseResponse(HttpResponse resp, Type returnType){
String resBody = resp.getBody();
Expand Down

0 comments on commit eda23cf

Please sign in to comment.