Search in sources :

Example 1 with ContextPath

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));
    }
}
Also used : Context(com.github.davidmoten.odata.client.Context) ContextPath(com.github.davidmoten.odata.client.ContextPath) Path(com.github.davidmoten.odata.client.Path) ContextPath(com.github.davidmoten.odata.client.ContextPath) StreamUploaderSingleCall(com.github.davidmoten.odata.client.StreamUploaderSingleCall)

Example 2 with ContextPath

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);
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 3 with ContextPath

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);
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) HttpService(com.github.davidmoten.odata.client.HttpService) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 4 with ContextPath

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);
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 5 with ContextPath

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);
    }
}
Also used : Optional(java.util.Optional) Schema(org.oasisopen.odata.csdl.v4.Schema) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) EntityType(com.github.davidmoten.odata.client.generator.model.EntityType) ODataEntityType(com.github.davidmoten.odata.client.ODataEntityType) TEntityType(org.oasisopen.odata.csdl.v4.TEntityType) ContextPath(com.github.davidmoten.odata.client.ContextPath) StringWriter(java.io.StringWriter) TypedObject(com.github.davidmoten.odata.client.internal.TypedObject) CollectionPageEntityRequest(com.github.davidmoten.odata.client.CollectionPageEntityRequest) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Aggregations

ContextPath (com.github.davidmoten.odata.client.ContextPath)16 HttpResponse (com.github.davidmoten.odata.client.HttpResponse)7 Path (com.github.davidmoten.odata.client.Path)7 RequestHeader (com.github.davidmoten.odata.client.RequestHeader)7 Context (com.github.davidmoten.odata.client.Context)6 HttpService (com.github.davidmoten.odata.client.HttpService)6 Optional (java.util.Optional)6 StreamUploaderSingleCall (com.github.davidmoten.odata.client.StreamUploaderSingleCall)5 IOException (java.io.IOException)5 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 UncheckedIOException (java.io.UncheckedIOException)5 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)4 ClientException (com.github.davidmoten.odata.client.ClientException)4 JacksonInject (com.fasterxml.jackson.annotation.JacksonInject)3 JsonAnyGetter (com.fasterxml.jackson.annotation.JsonAnyGetter)3 JsonAnySetter (com.fasterxml.jackson.annotation.JsonAnySetter)3 JsonIgnoreType (com.fasterxml.jackson.annotation.JsonIgnoreType)3 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)3 Include (com.fasterxml.jackson.annotation.JsonInclude.Include)3