/// <summary>
        /// Runs a single poll to check that status of the ShortCode.
        /// </summary>
        /// <param name="cancellationToken">Cancellation for the operation</param>
        /// <returns>The state of the shortcode</returns>
        /// <exception cref="UnexpectedHttpException">If an unknown status
        /// code is returned.</exception>
        public async Task <ShortcodeState> PollAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            if (DateTimeOffset.UtcNow >= this.ExpiresAt)
            {
                return(ShortcodeState.Expired);
            }

            using (var response = await this.options.Client.GetAsync(
                       $"{this.options.Host}/api/v1/oauth/shortcode/check/{this.Handle}",
                       cancellationToken))
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    var parsed = JsonConvert.DeserializeObject <ShortcodeAuthorizedResponse>(
                        await response.Content.ReadAsStringAsync());
                    this.readCode = parsed.Code;
                    return(ShortcodeState.Accepted);

                case HttpStatusCode.NoContent:
                    return(ShortcodeState.Waiting);

                case HttpStatusCode.Forbidden:
                    return(ShortcodeState.Denied);

                case HttpStatusCode.NotFound:
                    return(ShortcodeState.Expired);

                default:
                    throw await HttpHelpers.CreateExceptionFromResponse(response);
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Throws an <see cref="UnexpectedHttpException"/> if the response
 /// is not a 2xx.
 /// </summary>
 /// <param name="response">Response to check</param>
 /// <returns>A task that is completed or faulted depending on
 /// the status.</returns>
 public static async Task ThrowInvalidContentsAsync(HttpResponseMessage response)
 {
     if ((int)response.StatusCode >= 300)
     {
         throw await HttpHelpers.CreateExceptionFromResponse(response);
     }
 }