public static async Task <HttpResponseMessage> PostAsync(TMCContext context, string path, IDictionary <string, object> queryParameters = default, IDictionary <string, string> parameters = default) { using var request = CreateRequest(context, HttpMethod.Post, path, queryParameters, parameters); return(await _client.SendAsync(request).ConfigureAwait(false)); }
public static async Task <T> GetAsync <T>(TMCContext context, string path, IDictionary <string, object> queryParameters = default, Func <HttpContent, Task <T> > contentDeserializer = default) { using var request = CreateRequest(context, HttpMethod.Get, path, queryParameters); using var response = await _client.SendAsync(request).ConfigureAwait(false); return(await DeserializeContentAsync(response, contentDeserializer).ConfigureAwait(false)); }
public static async Task <string> FetchAuthorizationTokenAsync(TMCContext context, string username, string password) { var parameters = new Dictionary <string, object> { { "grant_type", "password" }, { "client_id", context.OAuthCredentials.ApplicationId }, { "client_secret", context.OAuthCredentials.Secret }, { "redirect_uri", "urn:ietf:wg:oauth:2.0:oob" }, { "username", username }, { "password", password }, }; string oauthTokenUrl = context.BaseUri.GetLeftPart(UriPartial.Authority) + "/oauth/token"; using var request = CreateRequest(HttpMethod.Post, oauthTokenUrl, parameters); using var response = await _client.SendAsync(request).ConfigureAwait(false); OAuthAccessToken tokenResponse = await DeserializeContentAsync <OAuthAccessToken>(response).ConfigureAwait(false); return(tokenResponse.AccessToken); }
public static HttpRequestMessage CreateRequest(TMCContext context, HttpMethod method, string path, IDictionary <string, object> queryParameters = default, IEnumerable <KeyValuePair <string, string> > parameters = default) { queryParameters ??= new Dictionary <string, object>(); if (context.IsAuthenticated) { AddSessionParameters(context, queryParameters); } string apiVersionSegment = "/api/v" + API_VERSION; var request = new HttpRequestMessage(method, context.BaseUri.GetLeftPart(UriPartial.Authority) + apiVersionSegment + path + GetQueryString(queryParameters)); if (parameters != default) { request.Content = new FormUrlEncodedContent(parameters); } return(request); }
public static Task <IEnumerable <ExerciseDetails> > GetExercisesAsync(TMCContext context, int courseId) => GetAsync <IEnumerable <ExerciseDetails> >(context, $"/courses/{courseId}/exercises");
public static Task <IEnumerable <ExerciseDetails> > GetExercisesAsync(TMCContext context, CourseDetails course) => GetExercisesAsync(context, course.Id);
public static Task <UserInfo> GetUserByIdAsync(TMCContext context, int userId) => GetAsync <UserInfo>(context, "/users/" + userId);
public static Task <UserInfo> GetCurrentUserAsync(TMCContext context) => GetAsync <UserInfo>(context, "/users/current");
public static Task <IEnumerable <Organization> > GetOrganizationsAsync(TMCContext context) => GetAsync <IEnumerable <Organization> >(context, "/org");
public static Task UnlockAsync(TMCContext context, CourseDetails course) => PostAsync(context, $"/core/courses/{course.Id}/unlock");
public static Task <CoreExercise> GetExerciseInformationAsync(TMCContext context, ExerciseDetails exercise) => GetAsync <CoreExercise>(context, $"/core/exercises/{exercise.Id}");
public static Task <IEnumerable <Review> > GetCourseReviewsAsync(TMCContext context, CourseDetails course) => GetAsync <IEnumerable <Review> >(context, $"/core/courses/{course.Id}/reviews");
public static Task <CourseDetails> GetCourseInformationAsync(TMCContext context, CourseDetails course) => GetAsync <CourseDetails>(context, "/core/courses/" + course.Id);
public static Task <IEnumerable <Organization> > GetCoursesAsync(TMCContext context, Organization org) => GetAsync <IEnumerable <Organization> >(context, $"/core/org/{org.Slug}/courses");
public static Task <OAuthCredentials> GetOAuthCredentialsAsync(TMCContext context) => GetAsync <OAuthCredentials>(context, $"/application/{CLIENT_NAME}/credentials");
private static void AddSessionParameters(TMCContext context, IDictionary <string, object> queryParameters) { queryParameters.Add("client", CLIENT_NAME); queryParameters.Add("client_version", CLIENT_VERSION); queryParameters.Add("access_token", context.Token); }
public static Task <byte[]> DownloadSubmissionAsync(TMCContext context, ExerciseDetails exercise) => GetAsync <byte[]>(context, $"/core/exercises/{exercise.Id}/download");