Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made apiRequest a public method #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 21 additions & 9 deletions src/main/java/com/force/api/ForceApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;

Expand Down Expand Up @@ -193,20 +194,31 @@ public ResourceRepresentation getSObject(String type, String id) throws Resource
jsonMapper);
}

public String createSObject(String type, Object sObject) {
public String createSObject(String type, Object sObject) {
Map<String, String> noHeaders = Collections.emptyMap();
return createSObject(type, sObject, noHeaders);
}

public String createSObject(String type, Object sObject, Map<String, String> headers) {
try {
// We're trying to keep Http classes clean with no reference to JSON/Jackson
// Therefore, we serialize to bytes before we pass object to HttpRequest().
// But it would be nice to have a streaming implementation. We can do that
// by using ObjectMapper.writeValue() passing in output stream, but then we have
// polluted the Http layer.
CreateResponse result = jsonMapper.readValue(apiRequest(new HttpRequest()
.url(uriBase()+"/sobjects/"+type)
.method("POST")
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.expectsCode(201)
.content(jsonMapper.writeValueAsBytes(sObject))).getStream(),CreateResponse.class);
HttpRequest httpRequest = new HttpRequest()
.url(uriBase()+"/sobjects/"+type)
.method("POST")
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.expectsCode(201)
.content(jsonMapper.writeValueAsBytes(sObject));

for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
httpRequest.header(headerEntry.getKey(), headerEntry.getValue());
}

CreateResponse result = jsonMapper.readValue(apiRequest(httpRequest).getStream(),CreateResponse.class);

if (result.isSuccess()) {
return (result.getId());
Expand Down Expand Up @@ -421,7 +433,7 @@ private final String uriBase() {
return(session.getApiEndpoint()+"/services/data/"+config.getApiVersionString());
}

private final HttpResponse apiRequest(HttpRequest req) {
private HttpResponse apiRequest(HttpRequest req) {
req.setAuthorization("Bearer "+session.getAccessToken());
req.setRequestTimeout(this.config.getRequestTimeout());
HttpResponse res = Http.send(req);
Expand Down