Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
 protected Task <ResponseObject <T> > Delete(UriFragment uriFragment, T item, Trying trying = null, CancellationToken ct = default(CancellationToken))
 {
     return(Request <T>(
                uriFragment,
                (webClient, uri) => webClient.UploadStringAsync(uri, HttpMethod.Delete, JsonConvert.SerializeObject(item), ct),
                trying));
 }
Ejemplo n.º 3
0
 protected Task <ResponseObject <T> > Get(UriFragment uriFragment, Trying trying = null, CancellationToken ct = default(CancellationToken))
 {
     return(Request <T>(
                uriFragment,
                (webClient, uri) => webClient.DownloadStringAsync(uri, ct),
                trying));
 }
Ejemplo n.º 4
0
 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));
 }
Ejemplo n.º 5
0
 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);
 }
Ejemplo n.º 6
0
        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!
        }
Ejemplo n.º 7
0
        protected async Task <T> PostAsync(UriFragment uriFragment, T item, Trying trying = null, CancellationToken ct = default(CancellationToken))
        {
            string json = JsonConvert.SerializeObject(item);

            return(await Request(
                       uriFragment,
                       async (webClient, uri) =>
            {
                string response = await webClient.UploadStringAsync(uri, HttpMethod.Post, json, ct).ConfigureAwait(false);
                return JsonConvert.DeserializeObject <T>(response);
            },
                       trying,
                       null).ConfigureAwait(false));
        }
Ejemplo n.º 8
0
        protected async Task <T> PutAsync(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 JsonConvert.DeserializeObject <T>(Encoding.UTF8.GetString(response));
            },
                       trying,
                       null).ConfigureAwait(false));
        }
Ejemplo n.º 9
0
 protected Task <ResponseObject <IEnumerable <T> > > PostMany(UriFragment uriFragment, IEnumerable <T> items, Trying trying = null, CancellationToken ct = default(CancellationToken))
 {
     return(Request <IEnumerable <T> >(
                uriFragment,
                (webClient, uri) => webClient.UploadStringAsync(uri, HttpMethod.Post, JsonConvert.SerializeObject(items), ct),
                trying));
 }
Ejemplo n.º 10
0
 protected void OnTrying(string str)
 {
     Trying?.Invoke(this, str);
 }
Ejemplo n.º 11
0
        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;
        }