diff --git a/src/classes/BaseHttpClient.cls b/src/classes/BaseHttpClient.cls index 89744bb..93cbe68 100644 --- a/src/classes/BaseHttpClient.cls +++ b/src/classes/BaseHttpClient.cls @@ -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; @@ -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 qryParams, String body){ + Map 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){ diff --git a/src/classes/BaseHttpClientTests.cls b/src/classes/BaseHttpClientTests.cls index e90e73b..c04c24f 100644 --- a/src/classes/BaseHttpClientTests.cls +++ b/src/classes/BaseHttpClientTests.cls @@ -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 { diff --git a/src/classes/BaseHttpJsonClient.cls b/src/classes/BaseHttpJsonClient.cls index 6fc6942..c402e92 100644 --- a/src/classes/BaseHttpJsonClient.cls +++ b/src/classes/BaseHttpJsonClient.cls @@ -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) @@ -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 qryParams, String body, Type returnType){ + Map qryParams, Object body, Type returnType){ HttpResponse resp = super.request(method, uri, qryParams, body); try{ @@ -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();