use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.
the class RequestHelper method get.
/**
* Returns the json from an HTTP GET of the url built from the contextPath and
* options. In the case where the returned object is actually a sub-class of T
* we lookup the sub-class from context schemaInfos based on the namespaced type
* of the return object.
*
* @param <T> return object type
* @param contextPath context and current path
* @param returnCls return class
* @param options request options
* @return object hydrated from json
*/
public static <T> T get(ContextPath contextPath, Class<T> returnCls, RequestOptions options) {
// build the url
ContextPath cp = contextPath.addQueries(options.getQueries());
List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", false);
// get the response
HttpResponse response = cp.context().service().get(cp.toUrl(), h, options);
checkResponseCode(cp, response, HttpURLConnection.HTTP_OK);
// deserialize
// Though cls might be Class<Attachment> we might actually want to return a
// sub-class like FileAttachment (which extends Attachment). This method returns
// the actual sub-class by inspecting the json response.
Class<? extends T> c = getSubClass(cp, contextPath.context().schemas(), returnCls, response.getText());
// FileAttachment which is a subclass of Attachment)
return cp.context().serializer().deserialize(response.getText(), c, contextPath, false);
}
use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.
the class RequestHelper method delete.
public static <T extends ODataEntityType> void delete(ContextPath cp, RequestOptions options) {
String url = cp.toUrl();
List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
HttpResponse response = cp.context().service().delete(url, h, options);
checkResponseCode(cp, response, HttpURLConnection.HTTP_NO_CONTENT);
}
use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.
the class RequestHelper method post.
public static void post(Map<String, Object> parameters, ContextPath contextPath, RequestOptions options) {
String json = Serializer.INSTANCE.serialize(parameters);
// build the url
ContextPath cp = contextPath.addQueries(options.getQueries());
List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
final String url = cp.toUrl();
// get the response
HttpService service = cp.context().service();
final HttpResponse response = service.post(url, h, json, options);
checkResponseCodeOk(cp, response);
}
use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.
the class RequestHelper method postAnyWithParametricType.
public static <T, S> T postAnyWithParametricType(Object object, ContextPath contextPath, Class<T> cls, Class<S> parametricTypeClass, RequestOptions options) {
// build the url
ContextPath cp = contextPath.addQueries(options.getQueries());
String json = Serializer.INSTANCE.serialize(object);
List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
// get the response
HttpResponse response = cp.context().service().post(cp.toUrl(), h, json, options);
checkResponseCode(cp, response, HttpURLConnection.HTTP_CREATED);
String text = response.getText();
// deserialize
Class<? extends T> c = getSubClass(cp, contextPath.context().schemas(), cls, text);
// FileAttachment which is a subclass of Attachment)
return cp.context().serializer().deserializeWithParametricType(text, c, parametricTypeClass, contextPath, false);
}
use of com.github.davidmoten.odata.client.HttpResponse in project odata-client by davidmoten.
the class GraphServiceTest method testDriveIssue173Put.
@Test
public void testDriveIssue173Put() {
GraphService client = clientBuilder().expectRequest("/drives/123/items/456:/filename.txt:/content").withMethod(//
HttpMethod.PUT).withPayload(//
"/request-upload-bytes.txt").withRequestHeaders(RequestHeader.CONTENT_TYPE_TEXT_PLAIN).withResponseStatusCode(//
201).withResponse(//
"/response-drive-item-put.json").build();
String url = "https://graph.microsoft.com/v1.0/drives/123/items/456:/filename.txt:/content";
String content = "hello";
{
// use HttpService
List<RequestHeader> headers = Collections.singletonList(RequestHeader.CONTENT_TYPE_TEXT_PLAIN);
HttpResponse u = //
client._service().submit(HttpMethod.PUT, url, headers, content, HttpRequestOptions.EMPTY);
assertTrue(u.getText().contains("0123456789abc"));
}
{
// use CustomRequest (with bytes content this time)
String json = //
client._custom().submitBytesReturnsString(HttpMethod.PUT, url, content.getBytes(StandardCharsets.UTF_8), HttpRequestOptions.EMPTY, RequestHeader.CONTENT_TYPE_TEXT_PLAIN);
assertTrue(json.contains("0123456789abc"));
}
}
Aggregations