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();
}
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();
}
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);
}
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);
}
}
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);
}
}
Aggregations