Ejemplo n.º 1
0
        /// <summary>
        /// Requests an access and refresh token from the code provided by the mercado libre callback.
        /// </summary>
        /// <param name="code">The code.</param>
        /// <param name="redirectUri">The redirect URI.</param>
        /// <returns>
        /// Return True when the operation is successful.
        /// </returns>
        public async Task <bool> AuthorizeAsync(string code, string redirectUri)
        {
            if (Credentials == null)
            {
                throw new ApplicationException("Credentials property not initalised.");
            }

            var success = false;

            var parameters = new HttpParams().Add("grant_type", "authorization_code")
                             .Add("client_id", Credentials.ClientId)
                             .Add("client_secret", Credentials.ClientSecret)
                             .Add("code", code)
                             .Add("redirect_uri", redirectUri);

            using (var client = HttpClientProvider.Create(false))
            {
                var tokens = await SendAsync <TokenResponse>(client, HttpMethod.Post, ApiUrl, "/oauth/token", parameters);

                if (tokens != null)
                {
                    Credentials.SetTokens(tokens);

                    success = true;
                }
            }

            return(success);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Sends a DELETE request and deserialises the JSON response.
 /// </summary>
 /// <typeparam name="T">The class to use to deserialise the JSON response.</typeparam>
 /// <param name="resource">The resource.</param>
 /// <param name="parameters">The parameters.</param>
 /// <returns></returns>
 public async Task <T> DeleteAsync <T>(string resource, HttpParams parameters = null)
 {
     using (var client = HttpClientProvider.Create())
     {
         return(await SendAsync <T>(client, HttpMethod.Delete, ApiUrl, resource, parameters));
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Sends a PUT request and deserialises the JSON response.
 /// </summary>
 /// <typeparam name="T">The class to use to deserialise the JSON response.</typeparam>
 /// <param name="resource">The resource.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="content">The content.</param>
 /// <returns></returns>
 public async Task <T> PutAsync <T>(string resource, HttpParams parameters = null, object content = null)
 {
     using (var client = HttpClientProvider.Create())
     {
         return(await SendAsync <T>(client, HttpMethod.Put, ApiUrl, resource, parameters, content));
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Sends a POST request.
 /// </summary>
 /// <param name="resource">The resource.</param>
 /// <param name="parameters">The parameters.</param>
 /// <param name="content">The payload for the content of the HTTP request.</param>
 /// <returns></returns>
 public async Task <HttpResponseMessage> PostAsync(string resource, HttpParams parameters = null, object content = null)
 {
     using (var client = HttpClientProvider.Create())
     {
         return(await SendAsync(client, HttpMethod.Post, ApiUrl, resource, parameters, content));
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Sends a DELETE request and deserialises the JSON response.
        /// </summary>
        /// <typeparam name="T">The class to use to deserialise the JSON response.</typeparam>
        /// <param name="resource">The resource.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="handler">The handler.</param>
        /// <returns></returns>
        public async Task <T> DeleteAsync <T>(string resource, HttpParams parameters = null, HttpClientHandler handler = null)
        {
            handler = handler ?? new HttpClientHandler();

            using (var client = HttpClientProvider.Create(handler))
            {
                return(await SendAsync <T>(client, HttpMethod.Delete, ApiUrl, resource, parameters));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Sends a PUT request.
        /// </summary>
        /// <param name="resource">The resource.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="content">The content.</param>
        /// <param name="handler">The handler.</param>
        /// <returns></returns>
        public async Task <HttpResponseMessage> PutAsync(string resource, HttpParams parameters = null, object content = null, HttpClientHandler handler = null)
        {
            handler = handler ?? new HttpClientHandler();

            using (var client = HttpClientProvider.Create(handler))
            {
                return(await SendAsync(client, HttpMethod.Put, ApiUrl, resource, parameters, content));
            }
        }