Search in sources :

Example 1 with RestParams

use of com.instructure.canvasapi2.builders.RestParams in project instructure-android by instructure.

the class CanvasRestAdapter method buildAdapterNoRedirects.

// region Adapter Builders
public Retrofit buildAdapterNoRedirects(@NonNull RestParams params) {
    if (params.getDomain() == null || params.getDomain().length() == 0) {
        params = new RestParams.Builder(params).withDomain(ApiPrefs.getFullDomain()).build();
    }
    if (mCallback != null) {
        mCallback.onCallbackStarted();
    }
    // Can make this check as we KNOW that the setter doesn't allow empty strings.
    if (params.getDomain().equals("")) {
        Logger.d("The RestAdapter hasn't been set up yet. Call setupInstance(context,token,domain)");
        return new Retrofit.Builder().baseUrl("http://invalid.domain.com/").build();
    }
    String apiContext = "";
    if (params.getCanvasContext() != null) {
        if (params.getCanvasContext().getType() == CanvasContext.Type.COURSE) {
            apiContext = "courses/";
        } else if (params.getCanvasContext().getType() == CanvasContext.Type.GROUP) {
            apiContext = "groups/";
        } else if (params.getCanvasContext().getType() == CanvasContext.Type.SECTION) {
            apiContext = "sections/";
        } else {
            apiContext = "users/";
        }
    }
    // Sets the auth token, user agent, and handles masquerading.
    final RestParams restParams = params;
    return new Retrofit.Builder().baseUrl(params.getDomain() + params.getAPIVersion() + apiContext).addConverterFactory(GsonConverterFactory.create()).callFactory(new Call.Factory() {

        @Override
        public Call newCall(Request request) {
            // Tag this request with the rest params so we can access them later in RequestInterceptor
            request = request.newBuilder().tag(restParams).build();
            return getOkHttpClientNoRedirects().newCall(request);
        }
    }).build();
}
Also used : RestParams(com.instructure.canvasapi2.builders.RestParams) GsonBuilder(com.google.gson.GsonBuilder) Request(okhttp3.Request) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory)

Example 2 with RestParams

use of com.instructure.canvasapi2.builders.RestParams in project instructure-android by instructure.

the class CanvasRestAdapter method buildAdapterSerializeNulls.

public Retrofit buildAdapterSerializeNulls(@NonNull RestParams params) {
    if (params.getDomain() == null || params.getDomain().length() == 0) {
        params = new RestParams.Builder(params).withDomain(ApiPrefs.getFullDomain()).build();
    }
    if (mCallback != null) {
        mCallback.onCallbackStarted();
    }
    // Can make this check as we KNOW that the setter doesn't allow empty strings.
    if (params.getDomain().equals("")) {
        Logger.d("The RestAdapter hasn't been set up yet. Call setupInstance(context,token,domain)");
        return new Retrofit.Builder().baseUrl("http://invalid.domain.com/").build();
    }
    String apiContext = "";
    if (params.getCanvasContext() != null) {
        if (params.getCanvasContext().getType() == CanvasContext.Type.COURSE) {
            apiContext = "courses/";
        } else if (params.getCanvasContext().getType() == CanvasContext.Type.GROUP) {
            apiContext = "groups/";
        } else if (params.getCanvasContext().getType() == CanvasContext.Type.SECTION) {
            apiContext = "sections/";
        } else {
            apiContext = "users/";
        }
    }
    // Sets the auth token, user agent, and handles masquerading.
    final RestParams restParams = params;
    return new Retrofit.Builder().baseUrl(params.getDomain() + params.getAPIVersion() + apiContext).addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create())).callFactory(new Call.Factory() {

        @Override
        public Call newCall(Request request) {
            // Tag this request with the rest params so we can access them later in RequestInterceptor
            request = request.newBuilder().tag(restParams).build();
            return getOkHttpClient().newCall(request);
        }
    }).build();
}
Also used : RestParams(com.instructure.canvasapi2.builders.RestParams) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Request(okhttp3.Request) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory)

Example 3 with RestParams

use of com.instructure.canvasapi2.builders.RestParams in project instructure-android by instructure.

the class RequestInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    Request.Builder builder = request.newBuilder();
    final String token = ApiPrefs.getToken();
    final String userAgent = ApiPrefs.getUserAgent();
    final String domain = ApiPrefs.getFullDomain();
    /* Nearly all requests are instantiated using RestBuilder and will have been tagged with
        a RestParams instance. Here we will attempt to retrieve it, but if unsuccessful we will
        fall back to a new RestParams instance with default values. */
    RestParams params;
    if (request.tag() != null && request.tag() instanceof RestParams) {
        params = (RestParams) request.tag();
    } else {
        params = new RestParams.Builder().build();
    }
    // Set the UserAgent
    if (!userAgent.equals("")) {
        builder.addHeader("User-Agent", userAgent);
    }
    // Authenticate if possible
    if (!params.shouldIgnoreToken() && !token.equals("")) {
        builder.addHeader("Authorization", "Bearer " + token);
    }
    // Add Accept-Language header for a11y
    builder.addHeader("accept-language", getAcceptedLanguageString());
    if (!APIHelper.hasNetworkConnection() || params.isForceReadFromCache()) {
        // Offline or only want cached data
        builder.cacheControl(CacheControl.FORCE_CACHE);
    } else if (params.isForceReadFromNetwork()) {
        // Typical from a pull-to-refresh
        builder.cacheControl(CacheControl.FORCE_NETWORK);
    }
    // Fun Fact: HTTP referer (originally a misspelling of referrer) is an HTTP header field that identifies
    // the address of the webpage that linked to the resource being requested
    // Source: https://en.wikipedia.org/wiki/HTTP_referer
    // Institutions need the referrer for a variety of reasons - mostly for restricted content
    // Strip out non-ascii characters, otherwise addHeader may throw an exception
    builder.addHeader("Referer", domain.replaceAll("[^\\x20-\\x7e]", ""));
    request = builder.build();
    // Masquerade if necessary
    if (ApiPrefs.isMasquerading()) {
        HttpUrl url = request.url().newBuilder().addQueryParameter("as_user_id", Long.toString(ApiPrefs.getMasqueradeId())).build();
        request = request.newBuilder().url(url).build();
    }
    if (params.usePerPageQueryParam()) {
        HttpUrl url = request.url().newBuilder().addQueryParameter("per_page", Integer.toString(ApiPrefs.getPerPageCount())).build();
        request = request.newBuilder().url(url).build();
    }
    return chain.proceed(request);
}
Also used : RestParams(com.instructure.canvasapi2.builders.RestParams) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl)

Example 4 with RestParams

use of com.instructure.canvasapi2.builders.RestParams in project instructure-android by instructure.

the class AccountDomainAPI method searchAccounts.

public static void searchAccounts(final String query, final StatusCallback<List<AccountDomain>> callback) {
    if (query == null || query.length() < 3)
        return;
    final RestBuilder adapter = new RestBuilder(callback);
    final RestParams params = new RestParams.Builder().withShouldIgnoreToken(true).withPerPageQueryParam(true).withForceReadFromCache(false).withForceReadFromNetwork(true).withDomain(DEFAULT_DOMAIN).build();
    if (query.contains(".")) {
        callback.addCall(adapter.build(AccountDomainInterface.class, params).campusDomainSearch(query)).enqueue(callback);
    } else {
        callback.addCall(adapter.build(AccountDomainInterface.class, params).campusNameSearch(query)).enqueue(callback);
    }
}
Also used : RestParams(com.instructure.canvasapi2.builders.RestParams) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder)

Example 5 with RestParams

use of com.instructure.canvasapi2.builders.RestParams in project instructure-android by instructure.

the class AccountDomainAPI method getAllAccountDomains.

public static void getAllAccountDomains(final StatusCallback<List<AccountDomain>> callback) {
    final RestBuilder adapter = new RestBuilder(callback);
    final RestParams params = new RestParams.Builder().withShouldIgnoreToken(true).withPerPageQueryParam(true).withForceReadFromCache(false).withForceReadFromNetwork(true).withDomain(DEFAULT_DOMAIN).build();
    if (StatusCallback.isFirstPage(callback.getLinkHeaders())) {
        callback.addCall(adapter.build(AccountDomainInterface.class, params).getDomains()).enqueue(callback);
    } else if (StatusCallback.moreCallsExist(callback.getLinkHeaders()) && callback.getLinkHeaders() != null) {
        callback.addCall(adapter.build(AccountDomainInterface.class, params).next(callback.getLinkHeaders().nextUrl)).enqueue(callback);
    }
}
Also used : RestParams(com.instructure.canvasapi2.builders.RestParams) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder) RestBuilder(com.instructure.canvasapi2.builders.RestBuilder)

Aggregations

RestBuilder (com.instructure.canvasapi2.builders.RestBuilder)276 RestParams (com.instructure.canvasapi2.builders.RestParams)264 List (java.util.List)33 StatusCallback (com.instructure.canvasapi2.StatusCallback)28 ExhaustiveListCallback (com.instructure.canvasapi2.utils.ExhaustiveListCallback)25 NonNull (android.support.annotation.NonNull)23 ArrayList (java.util.ArrayList)21 LinkHeaders (com.instructure.canvasapi2.utils.LinkHeaders)20 Response (okhttp3.Response)16 ParentResponse (com.instructure.canvasapi2.models.ParentResponse)10 Conversation (com.instructure.canvasapi2.models.Conversation)7 FileFolder (com.instructure.canvasapi2.models.FileFolder)7 UpdateFileFolder (com.instructure.canvasapi2.models.UpdateFileFolder)7 Group (com.instructure.canvasapi2.models.Group)6 NotNull (org.jetbrains.annotations.NotNull)5 DiscussionTopicHeader (com.instructure.canvasapi2.models.DiscussionTopicHeader)4 ApiType (com.instructure.canvasapi2.utils.ApiType)4 Nullable (android.support.annotation.Nullable)3 WorkerThread (android.support.annotation.WorkerThread)3 Course (com.instructure.canvasapi2.models.Course)3