use of com.github.davidmoten.odata.client.RequestHeader in project odata-client by davidmoten.
the class DefaultHttpService method getStream.
@Override
public InputStream getStream(HttpMethod method, String url, List<RequestHeader> requestHeaders, HttpRequestOptions options) {
try {
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setInstanceFollowRedirects(true);
c.setRequestMethod(method.toString());
for (RequestHeader header : requestHeadersModifier.apply(requestHeaders)) {
c.setRequestProperty(header.name(), header.value());
}
c.setDoInput(true);
c.setDoOutput(false);
// apply just before connection established so further configuration can take
// place like timeouts
consumer.accept(c);
// TODO check error code and throw message read from input stream
return c.getInputStream();
} catch (IOException e) {
throw new ClientException(e);
}
}
use of com.github.davidmoten.odata.client.RequestHeader 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.RequestHeader 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.RequestHeader 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.RequestHeader 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);
}
Aggregations