Exemple #1
0
 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));
 }
Exemple #2
0
        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));
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        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);
        }
Exemple #5
0
 public static Task <IEnumerable <ExerciseDetails> > GetExercisesAsync(TMCContext context, int courseId) => GetAsync <IEnumerable <ExerciseDetails> >(context, $"/courses/{courseId}/exercises");
Exemple #6
0
 public static Task <IEnumerable <ExerciseDetails> > GetExercisesAsync(TMCContext context, CourseDetails course) => GetExercisesAsync(context, course.Id);
Exemple #7
0
 public static Task <UserInfo> GetUserByIdAsync(TMCContext context, int userId) => GetAsync <UserInfo>(context, "/users/" + userId);
Exemple #8
0
 public static Task <UserInfo> GetCurrentUserAsync(TMCContext context) => GetAsync <UserInfo>(context, "/users/current");
Exemple #9
0
 public static Task <IEnumerable <Organization> > GetOrganizationsAsync(TMCContext context) => GetAsync <IEnumerable <Organization> >(context, "/org");
Exemple #10
0
 public static Task UnlockAsync(TMCContext context, CourseDetails course) => PostAsync(context, $"/core/courses/{course.Id}/unlock");
Exemple #11
0
 public static Task <CoreExercise> GetExerciseInformationAsync(TMCContext context, ExerciseDetails exercise) => GetAsync <CoreExercise>(context, $"/core/exercises/{exercise.Id}");
Exemple #12
0
 public static Task <IEnumerable <Review> > GetCourseReviewsAsync(TMCContext context, CourseDetails course) => GetAsync <IEnumerable <Review> >(context, $"/core/courses/{course.Id}/reviews");
Exemple #13
0
 public static Task <CourseDetails> GetCourseInformationAsync(TMCContext context, CourseDetails course) => GetAsync <CourseDetails>(context, "/core/courses/" + course.Id);
Exemple #14
0
 public static Task <IEnumerable <Organization> > GetCoursesAsync(TMCContext context, Organization org) => GetAsync <IEnumerable <Organization> >(context, $"/core/org/{org.Slug}/courses");
Exemple #15
0
 public static Task <OAuthCredentials> GetOAuthCredentialsAsync(TMCContext context) => GetAsync <OAuthCredentials>(context, $"/application/{CLIENT_NAME}/credentials");
Exemple #16
0
 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);
 }
Exemple #17
0
 public static Task <byte[]> DownloadSubmissionAsync(TMCContext context, ExerciseDetails exercise) => GetAsync <byte[]>(context, $"/core/exercises/{exercise.Id}/download");