use of com.instructure.canvasapi2.builders.RestBuilder 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.RestBuilder 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.RestBuilder 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);
}
}
use of com.instructure.canvasapi2.builders.RestBuilder in project instructure-android by instructure.
the class AssignmentAPI method getFirstPageGradeableStudentsForAssignment.
public static void getFirstPageGradeableStudentsForAssignment(long courseId, long assignmentId, @NonNull RestBuilder adapter, @NonNull StatusCallback<List<GradeableStudent>> callback) {
RestParams params = new RestParams.Builder().withShouldIgnoreToken(false).withPerPageQueryParam(true).build();
callback.addCall(adapter.build(AssignmentInterface.class, params).getFirstPageGradeableStudentsForAssignment(courseId, assignmentId)).enqueue(callback);
}
use of com.instructure.canvasapi2.builders.RestBuilder in project instructure-android by instructure.
the class AssignmentAPI method getFirstPageSubmissionsForAssignment.
public static void getFirstPageSubmissionsForAssignment(long courseId, long assignmentId, boolean forceNetwork, @NonNull RestBuilder adapter, @NonNull StatusCallback<List<Submission>> callback) {
RestParams params = new RestParams.Builder().withShouldIgnoreToken(false).withPerPageQueryParam(true).withForceReadFromNetwork(forceNetwork).build();
callback.addCall(adapter.build(AssignmentInterface.class, params).getFirstPageSubmissionsForAssignment(courseId, assignmentId)).enqueue(callback);
}
Aggregations