Exemple #1
0
        protected virtual async Task <T> PostAsync <T>(string method, FormUrlEncodedContent content, CancellationToken cancellationToken)
        {
            var response = await httpClient.PostAsync(rootEndPoint + method, content, cancellationToken).ConfigureAwait(false);

            using (var stream = await response.EnsureSuccessStatusCode().Content.ReadAsStreamAsync().ConfigureAwait(false))
            {
                return((T)ContentFormatter.Deserialize(typeof(T), stream));
            }
        }
Exemple #2
0
        protected virtual IEnumerator _PostAsync <T>(string method, WWWForm content, Action <T> onCompleted, Action <Exception> onError, Action <float> reportProgress)
        {
            using (var www = new WWW(rootEndPoint + method, content.data, MergeHeaders(content.headers)))
            {
                while (!www.isDone)
                {
                    if (reportProgress != null)
                    {
                        try
                        {
                            reportProgress(www.progress);
                        }
                        catch (Exception ex)
                        {
                            if (onError != null)
                            {
                                onError(ex);
                            }
                            yield break;
                        }
                    }
                    yield return(null);
                }

                if (www.error != null)
                {
                    var ex = new Exception(www.error ?? "");
                    if (onError != null)
                    {
                        onError(ex);
                    }
                }
                else
                {
                    try
                    {
                        using (var ms = new MemoryStream(www.bytes))
                        {
                            var value = (T)ContentFormatter.Deserialize(typeof(T), ms);
                            onCompleted(value);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (onError != null)
                        {
                            onError(ex);
                        }
                    }
                }
            }
        }
Exemple #3
0
        protected virtual async Task <T> PostAsync <T>(string method, FormUrlEncodedContent content, CancellationToken cancellationToken)
        {
            if (ContentFormatter.MediaType != null)
            {
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(ContentFormatter.MediaType);
            }
            var response = await httpClient.PostAsync(rootEndPoint + method, content, cancellationToken).ConfigureAwait(false);

            using (var stream = await response.EnsureSuccessStatusCode().Content.ReadAsStreamAsync().ConfigureAwait(false))
            {
                return((T)ContentFormatter.Deserialize(typeof(T), stream));
            }
        }
        protected virtual IObservable <T> _PostAsync <T>(string contract, string operation, WWWForm content, List <KeyValuePair <string, string[]> > contentList, IProgress <float> reportProgress)
        {
            var deferredOperation = Observable.Defer(() =>
            {
                var headers = CopyHeaders();
                if (contentList.Count == 0)
                {
                    content.AddField("_", "_");                         // add dummy
                }
                OnBeforeRequest(contract, operation, contentList, ref headers);
                var postObservable = (headers == null)
                    ? ObservableWWW.PostWWW(rootEndPoint + "/" + contract + "/" + operation, content, reportProgress)
                    : ObservableWWW.PostWWW(rootEndPoint + "/" + contract + "/" + operation, content, headers, reportProgress);
                var weboperation = postObservable
                                   .Select(x =>
                {
                    string header;
                    if (x.responseHeaders.TryGetValue("Content-Encoding", out header) && header == "gzip")
                    {
                        using (var ms = new MemoryStream(x.bytes))
                        {
                            var value = (T)ContentFormatter.Deserialize(typeof(T), ms);
                            return(value);
                        }
                    }
                    else
                    {
                        using (var ms = new MemoryStream(x.bytes))
                        {
                            var value = (T)plainJsonContentFormatter.Deserialize(typeof(T), ms);
                            return(value);
                        }
                    }
                });

                return(weboperation);
            });

            return(deferredOperation);
        }
Exemple #5
0
        protected virtual IObservable <T> _PostAsync <T>(string contract, string operation, WWWForm content, IProgress <float> reportProgress)
        {
            var deferredOperation = Observable.Defer(() =>
            {
                var postObservable = (defaultHeaders == null)
                    ? ObservableWWW.PostAndGetBytes(rootEndPoint + "/" + contract + "/" + operation, content, reportProgress)
                    : ObservableWWW.PostAndGetBytes(rootEndPoint + "/" + contract + "/" + operation, content, defaultHeaders, reportProgress);
                var weboperation = postObservable
                                   .Select(x =>
                {
                    using (var ms = new MemoryStream(x))
                    {
                        var value = (T)ContentFormatter.Deserialize(typeof(T), ms);
                        return(value);
                    }
                });

                return(weboperation);
            });

            AddAdditionalFlow(contract, operation, ref deferredOperation);
            return(deferredOperation);
        }