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