private async Task <string> SendAsync(HttpRequest request, CancellationToken cancellationToken) { //Authenticate with oauth _oauthHandler.Authenticate(request); HttpResponseMessage response; try { response = await _httpClient.SendAsync(request, cancellationToken); } catch (TaskCanceledException ex) { // Expiration (cancellation) of the longpoll call is a normal situation, it happens regularly if longpoll set to higher values (480 is the max) and as a part of an expected workflow. Seen from this angle it is not an 'exception'. // For that reason (and the possibility of a simpler consumer code) we simulate "no changes" reply from the server in this very particular case instead of throwing a generic DropBoxException. if (request.RequestUri.AbsolutePath.Contains("longpoll")) { return("{\"changes\" : false}"); } throw new DropboxException(ex); } catch (Exception ex) { throw new DropboxException(ex); } //TODO - More Error Handling if (response.StatusCode != HttpStatusCode.OK) { throw new DropboxException(response); } return(await response.Content.ReadAsStringAsync()); }
/// <summary> /// Wrapper around the HttpClient.SendAsync function with error handling /// </summary> /// <typeparam name="T"></typeparam> /// <param name="request"></param> /// <returns></returns> private async Task <T> SendAsync <T>(HttpRequest request) where T : class { //Authenticate with oauth _oauthHandler.Authenticate(request); HttpResponseMessage response; try { response = await _httpClient.SendAsync(request); } catch (Exception ex) { throw new DropboxException(ex); } //TODO - More Error Handling if (response.StatusCode != HttpStatusCode.OK) { throw new DropboxException(response); } string responseBody = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <T>(responseBody)); }
private async Task <string> SendAsync(HttpRequest request, CancellationToken cancellationToken) { //Authenticate with oauth _oauthHandler.Authenticate(request); HttpResponseMessage response; try { response = await _httpClient.SendAsync(request, cancellationToken); } catch (Exception ex) { throw new DropboxException(ex); } //TODO - More Error Handling if (response.StatusCode != HttpStatusCode.OK) { throw new DropboxException(response); } return(await response.Content.ReadAsStringAsync()); }