All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.davidmoten.odata.client.CollectionPageNonEntityRequest Maven / Gradle / Ivy

The newest version!
package com.github.davidmoten.odata.client;

import static com.github.davidmoten.odata.client.internal.Util.odataTypeNameFromAny;

import java.net.HttpURLConnection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonIgnoreType;
import com.github.davidmoten.guavamini.Preconditions;
import com.github.davidmoten.odata.client.internal.RequestHelper;
import com.github.davidmoten.odata.client.internal.TypedObject;

@JsonIgnoreType
public class CollectionPageNonEntityRequest implements Iterable {

    private final ContextPath contextPath;
    private final Class cls;

    // initial call made with this method, further pages use HttpMethod.GET
    private final HttpMethod method;
    private final Map parameters;
    private final int expectedResponseCode;

    // should not be public api
    public CollectionPageNonEntityRequest(ContextPath contextPath, Class cls,
            HttpMethod method, Map parameters,
            int expectedResponseCode) {
        Preconditions.checkArgument(method != HttpMethod.POST || !parameters.isEmpty());
        this.contextPath = contextPath;
        this.cls = cls;
        this.method = method;
        this.parameters = parameters;
        this.expectedResponseCode = expectedResponseCode;
    }

    public CollectionPageNonEntityRequest(ContextPath contextPath, Class cls) {
        this(contextPath, cls, HttpMethod.GET, Collections.emptyMap(),
                HttpURLConnection.HTTP_CREATED);
    }
    
    public static  CollectionPageNonEntityRequest forAction(ContextPath contextPath,
            Class returnClass, Map parameters) {
        return new CollectionPageNonEntityRequest( //
                contextPath, //
                returnClass, //
                HttpMethod.POST, //
                parameters, //
                HttpURLConnection.HTTP_OK);
    }

    public static  CollectionPageNonEntityRequest forFunction(ContextPath contextPath,
            Class returnClass, Map parameters) {
        return new CollectionPageNonEntityRequest( //
                contextPath, //
                returnClass, //
                HttpMethod.GET, //
                parameters, //
                HttpURLConnection.HTTP_OK);
    }
    
    CollectionPage get(RequestOptions options) {
        final HttpResponse r;
        List h = RequestHelper.cleanAndSupplementRequestHeaders(options, "minimal",
                method != HttpMethod.GET);
        Serializer serializer = contextPath.context().serializer();
        final ContextPath cpBase = contextPath.addQueries(options.getQueries());
        final ContextPath cp;
        if (method == HttpMethod.GET) {
            cp = cpBase.appendToSegment( //
                    InlineParameterSyntax.encode(serializer, parameters)); //
            r = cp.context().service().get(cp.toUrl(), h, options);
        } else {
            String json = serializer.serialize(parameters);
            cp = cpBase;
            r = cp.context().service().post(cp.toUrl(), h, json, options);
        }
        RequestHelper.checkResponseCode(cp, r, expectedResponseCode);
        return serializer //
                .deserializeCollectionPage( //
                        r.getText(), //
                        cls, //
                        cp, //
                        h, //
                        options, //
                        null);
    }
    
    public CollectionPage get() {
        return new CollectionNonEntityRequestOptionsBuilder(this).get();
    }
    
    public void forEach(Consumer consumer) {
        stream().forEach(consumer);
    }
    
    public Stream stream() {
        return get().stream();
    }
    
    public Stream> streamWithDeltaLink() {
        return get().streamWithDeltaLink();
    }
    
    public  S to(Function,? extends S> function) {
    	return function.apply(get());
    }
    
    public List toList() {
        return get().toList();
    }
    
    @Override
    public Iterator iterator() {
        return get().iterator();
    }
    

    /**
     * Returns a request for only those members of the collection that are of the
     * requested type. This is referred to in the OData
     * 4.01 specification as a "restriction to instances of the derived type".
     * 
     * @param 
     *            the type ("derived type") to be restricting to
     * @param cls
     *            the Class of the type to restrict to
     * @return a request for a collection of instances with the given type
     */
    public  CollectionPageNonEntityRequest filter(Class cls) {
        return new CollectionPageNonEntityRequest( //
                contextPath.addSegment(odataTypeNameFromAny(cls)), cls);
    }

    public CollectionNonEntityRequestOptionsBuilder requestHeader(String key, String value) {
        return new CollectionNonEntityRequestOptionsBuilder(this).requestHeader(key, value);
    }
    
    public CollectionNonEntityRequestOptionsBuilder query(String name, String value) {
        return new CollectionNonEntityRequestOptionsBuilder(this).query(name, value);
    }
    
    public CollectionNonEntityRequestOptionsBuilder requestHeader(RequestHeader header) {
        return new CollectionNonEntityRequestOptionsBuilder(this).requestHeader(header);
    }
    
    public CollectionNonEntityRequestOptionsBuilder maxPageSize(int maxPageSize) {
        return new CollectionNonEntityRequestOptionsBuilder(this).maxPageSize(maxPageSize);
    }
    
    public CollectionNonEntityRequestOptionsBuilder search(String clause) {
        return new CollectionNonEntityRequestOptionsBuilder(this).search(clause);
    }

    public CollectionNonEntityRequestOptionsBuilder filter(String clause) {
        return new CollectionNonEntityRequestOptionsBuilder(this).filter(clause);
    }

    public CollectionNonEntityRequestOptionsBuilder orderBy(String clause) {
        return new CollectionNonEntityRequestOptionsBuilder(this).orderBy(clause);
    }

    public CollectionNonEntityRequestOptionsBuilder skip(long n) {
        return new CollectionNonEntityRequestOptionsBuilder(this).skip(n);
    }

    public CollectionNonEntityRequestOptionsBuilder top(long n) {
        return new CollectionNonEntityRequestOptionsBuilder(this).top(n);
    }

    public CollectionNonEntityRequestOptionsBuilder select(String clause) {
        return new CollectionNonEntityRequestOptionsBuilder(this).select(clause);
    }

    public CollectionNonEntityRequestOptionsBuilder metadataFull() {
        return new CollectionNonEntityRequestOptionsBuilder(this).metadataFull();
    }

    public CollectionNonEntityRequestOptionsBuilder metadataMinimal() {
        return new CollectionNonEntityRequestOptionsBuilder(this).metadataMinimal();
    }

    public CollectionNonEntityRequestOptionsBuilder metadataNone() {
        return new CollectionNonEntityRequestOptionsBuilder(this).metadataNone();
    }
    
    public CollectionNonEntityRequestOptionsBuilder urlOverride(String urlOverride) {
        return new CollectionNonEntityRequestOptionsBuilder(this).urlOverride(urlOverride);
    }

    public CollectionNonEntityRequestOptionsBuilder deltaTokenLatest() {
        return new CollectionNonEntityRequestOptionsBuilder(this).deltaTokenLatest();
    }
}