use of com.github.davidmoten.odata.client.ContextPath in project odata-client by davidmoten.
the class RequestHelper method uploader.
public static Optional<StreamUploaderSingleCall> uploader(ContextPath contextPath, ODataType item, String fieldName, HttpMethod method) {
Preconditions.checkNotNull(fieldName);
String editLink = (String) item.getUnmappedFields().get(fieldName + "@odata.mediaEditLink");
String contentType = (String) item.getUnmappedFields().get(fieldName + "@odata.mediaContentType");
if (editLink == null) {
return Optional.empty();
} else {
// TODO support relative editLink?
Context context = contextPath.context();
if (contentType == null) {
contentType = CONTENT_TYPE_APPLICATION_OCTET_STREAM;
}
Path path = new Path(editLink, contextPath.path().style());
return Optional.of(new StreamUploaderSingleCall(new ContextPath(context, path), contentType, method));
}
}
use of com.github.davidmoten.odata.client.ContextPath 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.ContextPath 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.ContextPath 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.ContextPath in project odata-client by davidmoten.
the class Generator method writeEntityCollectionRequest.
private void writeEntityCollectionRequest(Schema schema, TEntityType entityType, Map<String, List<Action>> collectionTypeActions, Map<String, List<Function>> collectionTypeFunctions, Set<String> collectionTypes) {
EntityType t = new EntityType(entityType, names);
if (!collectionTypes.contains(t.getFullType())) {
return;
}
names.getDirectoryEntityCollectionRequest(schema).mkdirs();
String simpleClassName = names.getSimpleClassNameCollectionRequest(schema, t.getName());
Imports imports = new Imports(names.getFullClassNameCollectionRequest(schema, t.getName()));
Indent indent = new Indent();
StringWriter w = new StringWriter();
try (PrintWriter p = new PrintWriter(w)) {
p.format("package %s;\n\n", names.getPackageCollectionRequest(schema));
p.format(IMPORTSHERE);
p.format("public class %s extends %s<%s, %s>{\n\n", simpleClassName, imports.add(CollectionPageEntityRequest.class), //
imports.add(names.getFullClassNameFromTypeWithoutNamespace(schema, t.getName())), imports.add(names.getFullClassNameEntityRequest(schema, t.getName())));
indent.right();
addContextPathField(imports, indent, p);
// add constructor
//
p.format(//
"\n%spublic %s(%s contextPath, %s<%s> value) {\n", //
indent, //
simpleClassName, //
imports.add(ContextPath.class), //
imports.add(Optional.class), imports.add(Object.class));
//
p.format(//
"%ssuper(contextPath, %s.class, cp -> new %s(cp, Optional.empty()), value);\n", //
indent.right(), //
imports.add(names.getFullClassNameFromTypeWithoutNamespace(schema, t.getName())), imports.add(names.getFullClassNameEntityRequestFromTypeWithoutNamespace(schema, t.getName())));
p.format("%sthis.contextPath = contextPath;\n", indent);
p.format("%s}\n", indent.left());
// write fields from properties
//
t.getNavigationProperties().forEach(x -> {
Schema sch = names.getSchema(names.getInnerType(names.getType(x)));
if (x.getType().get(0).startsWith(COLLECTION_PREFIX)) {
String y = names.getInnerType(names.getType(x));
if (names.isEntityWithNamespace(y)) {
String entityRequestType = names.getFullClassNameEntityRequestFromTypeWithNamespace(sch, y);
EntityType et = names.getEntityType(y);
KeyInfo k = getKeyInfo(et, imports);
if (!k.isEmpty()) {
p.format("\n%spublic %s %s(%s) {\n", indent, imports.add(entityRequestType), Names.getIdentifier(x.getName()), k.typedParams);
p.format("%sreturn new %s(contextPath.addSegment(\"%s\")%s, %s.empty());\n", indent.right(), imports.add(entityRequestType), x.getName(), k.addKeys, imports.add(Optional.class));
p.format("%s}\n", indent.left());
}
}
//
p.format(//
"\n%spublic %s %s() {\n", //
indent, imports.add(//
names.getFullClassNameCollectionRequestFromTypeWithNamespace(sch, y)), Names.getIdentifier(x.getName()));
//
p.format(//
"%sreturn new %s(contextPath.addSegment(\"%s\"), %s.empty());\n", //
indent.right(), imports.add(//
names.getFullClassNameCollectionRequestFromTypeWithNamespace(sch, y)), //
x.getName(), imports.add(Optional.class));
p.format("%s}\n", indent.left());
}
});
Set<String> methodNames = new HashSet<>();
writeBoundActionMethods(t, collectionTypeActions, imports, indent, p, methodNames);
writeBoundFunctionMethods(t, collectionTypeFunctions, imports, indent, p, methodNames);
indent.left();
p.format("\n}\n");
writeToFile(imports, w, t.getClassFileCollectionRequest());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations