protected Task <ResponseObject <TValue> > Put <TValue>(UriFragment uriFragment, TValue item, Trying trying = null, CancellationToken ct = default(CancellationToken)) { return(Request <TValue>( uriFragment, (webClient, uri) => webClient.UploadStringAsync(uri, HttpMethod.Put, JsonConvert.SerializeObject(item), ct), trying)); }
protected Task <ResponseObject <int> > DeleteMany(UriFragment uriFragment, IEnumerable <T> item, Trying trying = null, CancellationToken ct = default(CancellationToken)) { return(Request <int>( uriFragment, (webClient, uri) => webClient.UploadStringAsync(uri, HttpMethod.Delete, JsonConvert.SerializeObject(item), ct), trying)); }
private Task <ResponseObject <TResult> > Request <TResult>( UriFragment uriFragment, Func <IEirWebClient, Uri, Task <string> > webFunction, Trying trying, [CallerMemberName] string memberName = null) { return(Request( uriFragment, async(webClient, uri) => { string content = await webFunction(webClient, uri).ConfigureAwait(false); ResponseObject <TResult> deserializedResponse; // First make an attempt to deserialize the response according to V2 standards (as a response object) if (!TryDeserializeResponseObject(content, out deserializedResponse)) { // ... and if that fails, fall back to deserializing the old way. deserializedResponse = GetResponseObjectFromOldStyleContent <TResult>(content); } if (deserializedResponse?.Errors.Any() ?? false) { throw new RestException(deserializedResponse.Errors); } return deserializedResponse; }, trying, null, // ReSharper disable once ExplicitCallerInfoArgument memberName)); }
protected Task <ResponseObject <IEnumerable <TValue> > > GetAll <TValue>(UriFragment uriFragment, Trying trying = null, CancellationToken ct = default(CancellationToken)) { return(Request <IEnumerable <TValue> >( uriFragment, (webClient, uri) => webClient.DownloadStringAsync(uri, ct), trying)); }
protected async Task <string> PostAsStringAsync(UriFragment uriFragment, T item, Trying trying = null, CancellationToken ct = default(CancellationToken)) { string json = JsonConvert.SerializeObject(item); return(await Request( uriFragment, async (webClient, uri) => await webClient.UploadStringAsync(uri, HttpMethod.Post, json, ct).ConfigureAwait(false), trying, null).ConfigureAwait(false)); }
protected Task <TValue> GetAsync <TValue>(UriFragment uriFragment, Trying trying = null, Func <TValue> getResultIfFail = null, CancellationToken ct = default(CancellationToken)) { return(Request( uriFragment, async(webClient, uri) => { string content = await webClient.DownloadStringAsync(uri, ct).ConfigureAwait(false); return JsonConvert.DeserializeObject <TValue>(content); }, trying, getResultIfFail)); }
protected T Get(UriFragment uriFragment, Trying trying = null, Func <T> getResultIfFail = null) { return(Request( uriFragment, (webClient, uri) => { string content = webClient.DownloadString(uri); return Task.FromResult(JsonConvert.DeserializeObject <T>(content)); }, trying, getResultIfFail).Result); }
protected async Task <string> PutAsStringAsync(UriFragment uriFragment, T item, Trying trying = null, CancellationToken ct = default(CancellationToken)) { string json = JsonConvert.SerializeObject(item); return(await Request( uriFragment, async (webClient, uri) => { byte[] response = await webClient.UploadDataAsync(uri, HttpMethod.Put, Encoding.UTF8.GetBytes(json), ct).ConfigureAwait(false); return Encoding.UTF8.GetString(response); }, trying, null).ConfigureAwait(false)); }
protected Task <bool> DeleteAsync(UriFragment uriFragment, T item, Trying trying = null, CancellationToken ct = default(CancellationToken)) { string json = JsonConvert.SerializeObject(item); return(Request( uriFragment, async(webClient, uri) => { await webClient.UploadDataAsync(uri, HttpMethod.Delete, Encoding.UTF8.GetBytes(json), ct).ConfigureAwait(false); return true; }, trying, () => false /* Return-value if failed. */)); // TODO: <<-- Throw on delete instead of returning false! }
public static string Combine(string baseUri, UriFragment uriFragment) { return(uriFragment == null ? baseUri : Combine(baseUri, uriFragment.ApiEndpointFragment, uriFragment.Args)); }
protected async Task <T> Request <T>( UriFragment uriFragment, Func <IEirWebClient, Uri, Task <T> > webFunction, Trying trying, Func <T> getResultIfFail, [CallerMemberName] string memberName = null) { var exceptions = new List <Exception>(); if (trying == null) { trying = Trying.Once; } for (int retry = 0; retry < trying.Count; retry++) { if (retry > 0) { await Task.Delay(trying.Pause).ConfigureAwait(false); } foreach (Uri baseUri in _baseUris.Uris) { try { Uri uri = new Uri(UriSupport.Combine(baseUri.AbsoluteUri, uriFragment)); using (IEirWebClient webClient = GetWebClient()) { return(await webFunction(webClient, uri).ConfigureAwait(false)); } } catch (WebException ex) { Log.To.Main.AddException($"Error in request to {uriFragment} on '{baseUri}'", ex); exceptions.Add(ex); if (!ShouldTryNextUri(ex.Status)) { break; } } catch (Exception ex) { Log.To.Main.AddException("WebStore general exception", ex); throw; } } } if (getResultIfFail != null) { return(getResultIfFail()); } var exception = new AggregateException( $"Error in {GetType().Name}<{typeof(T).Name}>.{memberName}(\"{uriFragment}\")", exceptions); Log.To.Main.AddException("WebStore Exception", exception); throw exception; }