use of com.instructure.canvasapi.model.User in project instructure-android by instructure.
the class CanvasContextPermissionUnitTest method testUserPermissions.
@Test
public void testUserPermissions() {
Gson gson = CanvasRestAdapter.getGSONParser();
User user = gson.fromJson(userPermissionJSON, User.class);
assertNotNull(user);
assertNotNull(user.getPermissions());
assertFalse(user.getPermissions().canCreateDiscussionTopic());
assertTrue(user.canUpdateAvatar());
assertTrue(user.canUpdateName());
assertTrue(user.getPermissions().canUpdateAvatar());
assertTrue(user.getPermissions().canUpdateName());
}
use of com.instructure.canvasapi.model.User in project instructure-android by instructure.
the class SectionUnitTest method sectionsWithStudentsTest.
@Test
public void sectionsWithStudentsTest() {
Gson gson = CanvasRestAdapter.getGSONParser();
Section[] sections = gson.fromJson(sectionsWithStudentsJSON, Section[].class);
assertNotNull(sections);
Section section = sections[0];
assertNotNull(section);
List<User> sectionStudents = section.getStudents();
assertNotNull(sectionStudents);
User user = sectionStudents.get(0);
assertNotNull(user);
assertTrue(user.getId() == 3558540);
assertNotNull(user.getName());
assertNotNull(user.getShortName());
}
use of com.instructure.canvasapi.model.User in project instructure-android by instructure.
the class OAuthWebLogin method setupCallback.
public void setupCallback() {
mobileVerifyCallback = new CanvasCallback<DomainVerificationResult>(APIHelpers.statusDelegateWithContext(OAuthWebLogin.this)) {
@Override
public void cache(DomainVerificationResult domainVerificationResult) {
}
@Override
public void firstPage(DomainVerificationResult domainVerificationResult, LinkHeaders linkHeaders, Response response) {
if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.Success) {
// Domain is now verified.
// save domain to the preferences.
String domain = "";
// mobile verify can change the hostname we need to use
if (domainVerificationResult.getBase_url() != null && !domainVerificationResult.getBase_url().equals("")) {
domain = domainVerificationResult.getBase_url();
} else {
domain = url;
}
// The domain gets set afterwards in SetUpInstance, but domain is required a bit before that works.
APIHelpers.setDomain(OAuthWebLogin.this, domain);
client_id = domainVerificationResult.getClient_id();
client_secret = domainVerificationResult.getClient_secret();
// Get the protocol
api_protocol = domainVerificationResult.getProtocol();
// Set the protocol
APIHelpers.setProtocol(domainVerificationResult.getProtocol(), OAuthWebLogin.this);
// Get device name for the login request.
String deviceName = Build.MODEL;
if (deviceName == null || deviceName.equals("")) {
deviceName = getString(R.string.unknownDevice);
}
// Remove spaces
deviceName = deviceName.replace(" ", "_");
// changed for the online update to have an actual formatted login page
authenticationURL = api_protocol + "://" + domain + "/login/oauth2/auth?client_id=" + client_id + "&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&mobile=1";
authenticationURL += "&purpose=" + deviceName;
if (canvas_login == 1) {
authenticationURL += "&canvas_login=1";
} else if (canvas_login == 2) {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setCookie(api_protocol + "://" + domain, "canvas_sa_delegated=1");
}
web.loadUrl(authenticationURL);
} else {
// Error message
int errorId;
if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.GeneralError) {
errorId = R.string.mobileVerifyGeneral;
} else if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.DomainNotAuthorized) {
errorId = R.string.mobileVerifyDomainUnauthorized;
} else if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.UnknownUserAgent) {
errorId = R.string.mobileVerifyUserAgentUnauthorized;
} else {
errorId = R.string.mobileVerifyUnknownError;
}
MaterialDialog.Builder builder = new MaterialDialog.Builder(OAuthWebLogin.this);
builder.title(R.string.errorOccurred);
builder.content(errorId);
builder.cancelable(true);
builder.positiveColor(Color.BLACK);
MaterialDialog dialog = builder.build();
dialog.show();
}
}
};
getToken = new CanvasCallback<OAuthToken>(APIHelpers.statusDelegateWithContext(OAuthWebLogin.this)) {
@Override
public void cache(OAuthToken oAuthToken, LinkHeaders linkHeaders, Response response) {
}
@Override
public void firstPage(OAuthToken oAuthToken, LinkHeaders linkHeaders, Response response) {
// Set up the rest adapter and such.
APIHelpers.setToken(getContext(), oAuthToken.getAccess_token());
CanvasRestAdapter.setupInstance(getContext(), oAuthToken.getAccess_token(), APIHelpers.loadProtocol(getContext()) + "://" + APIHelpers.getDomain(getContext()));
// save the successful domain to be remembered for later
JSONArray domains = SavedDomains.getSavedDomains(OAuthWebLogin.this, prefNamePreviousDomain);
String domain = APIHelpers.getDomain(OAuthWebLogin.this);
domains.put(domain);
// save the new domain
SavedDomains.setSavedDomains(OAuthWebLogin.this, domains, prefNamePreviousDomain);
// Set the last used domain.
setLastSignedInDomain(domain, OAuthWebLogin.this);
// We now need to get the cache user
UserAPI.getSelf(new UserCallback(APIHelpers.statusDelegateWithContext(OAuthWebLogin.this)) {
@Override
public void cachedUser(User user) {
}
@Override
public void user(User user, Response response) {
Intent intent = OAuthWebLogin.this.getIntent();
intent.putExtra(URLSignIn.loggedInIntent, true);
if (passedURI != null) {
intent.putExtra(Const.PASSED_URI, passedURI);
}
OAuthWebLogin.this.setResult(RESULT_OK, intent);
OAuthWebLogin.this.finish();
}
});
}
@Override
public boolean onFailure(RetrofitError retrofitError) {
if (!specialCase) {
Toast.makeText(OAuthWebLogin.this, R.string.errorOccurred, Toast.LENGTH_SHORT).show();
} else {
specialCase = false;
}
web.loadUrl(authenticationURL);
return true;
}
};
}
use of com.instructure.canvasapi.model.User in project instructure-android by instructure.
the class GlobalDataSyncAPI method setGlobalData.
public static void setGlobalData(Context context, NAMESPACE namespace, GlobalDataSync data) {
try {
User user = APIHelpers.getCacheUser(context);
RestAdapter restAdapter = CanvasRestAdapter.buildAdapter(context);
restAdapter.create(DataSyncInterface.class).setGlobalData(user.getId(), namespace.toString(), GlobalDataSync.toJsonString(data), "");
} catch (Exception e) {
Utils.e("===> GLOBAL DATA SYNC ERROR *SET*: " + e);
}
}
use of com.instructure.canvasapi.model.User in project instructure-android by instructure.
the class GlobalDataSyncAPI method getGlobalData.
public static GlobalDataSync getGlobalData(Context context, NAMESPACE namespace) {
try {
User user = APIHelpers.getCacheUser(context);
RestAdapter restAdapter = CanvasRestAdapter.buildAdapter(context);
JsonElement element = restAdapter.create(DataSyncInterface.class).getGlobalData(user.getId(), namespace.toString());
if (element != null) {
String json = cleanJson(element.toString());
return new Gson().fromJson(json, GlobalDataSync.class);
}
return null;
} catch (Exception e) {
Utils.e("===> GLOBAL DATA SYNC ERROR *GET*: " + e);
return null;
}
}
Aggregations