Created
November 30, 2012 18:21
-
-
Save jevonearth/4177541 to your computer and use it in GitHub Desktop.
bug report draft
This file contains hidden or 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
/* | |
The CRUD methods in ForceApi all take java objects, and de-serializes them to json internally. | |
I do not want to annotate my domain objects specifically for sobjects, instead I would like to manage my own serialization. | |
Perhaps we could add an additional method per CRUD method which takes a json string instead of a Object, allowing the client more control of how it manages its domain model. | |
*/ | |
public String createSObject(String type, Object sObject) { | |
return createSObject(type, jsonMapper.writeValueAsBytes(sObject)); | |
} | |
public String createSObject(String type, String sObjectJson) { | |
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(sObjectJson)).getStream(),CreateResponse.class); | |
if (result.isSuccess()) { | |
return (result.getId()); | |
} else { | |
throw new SObjectException(result.getErrors()); | |
} | |
} catch (JsonGenerationException e) { | |
throw new ResourceException(e); | |
} catch (JsonMappingException e) { | |
throw new ResourceException(e); | |
} catch (IOException e) { | |
throw new ResourceException(e); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment