Beispiel #1
0
        private async Task <dynamic> _request(string url, object data, dynamic options, Restler.Method method)
        {
            options = dyn.exp(options, true);
            if (!Connection.HttpTestExp.IsMatch(url))
            {
                var baseUrl = dyn.getProp(_options, "url", config.apiOwly_url);
                url = baseUrl + url;
            }
            _log($"Request: {url}");
            Func <bool, Task <JObject> > requestFn = async(forceOAuth) =>
            {
                try
                {
                    var y = await _rest.request(url, options, method);

                    var d = (JObject)y;
                    if ((string)d["success"] == "false" && d["errors"] != null)
                    {
                        _log($"Request failed: {d}");
                        throw new InvalidOperationException(d["errors"].ToString());
                    }
                    return((dynamic)d);
                }
                catch (RestlerOperationException res)
                {
                    var err = (JObject)res.Content;
                    if (err["errors"] != null)
                    {
                        _log($"Request failed: {err}"); throw;
                    }
                    else
                    {
                        var statusCode = Math.Floor((int)res.StatusCode / 100M);
                        if (statusCode == 5)
                        {
                            throw res;
                        }
                        else if (statusCode == 4)
                        {
                            throw res;
                        }
                        else
                        {
                            throw res;
                        }
                    }
                }
            };

            return(await _retry.start(requestFn));
        }
Beispiel #2
0
        private async Task <dynamic> _request(string url, object data, dynamic options, Restler.Method method)
        {
            options = dyn.exp(options, true);
            if (!HttpTestExp.IsMatch(url))
            {
                var baseUrl = dyn.getProp(_options, "url", config.api_url);
                url = baseUrl + url;
            }
            _log($"Request: {url}");
            Func <Task <JObject>, Task <JObject> > requestFn = async(x) =>
            {
                if (!x.IsFaulted)
                {
                    var token = x.Result;
                    options.headers = dyn.getObj(options, "headers");
                    options.headers.Authorization = $"Bearer {token["access_token"]}";
                    try
                    {
                        var d = data == null ? (JObject)await _rest.request(url, options, method) : await _rest.json(url, data, options, method);

                        if (d["errors"] != null)
                        {
                            _log($"Request failed: {d}");
                            throw new InvalidOperationException(d["errors"].ToString());
                        }
                        return((dynamic)d);
                    }
                    catch (RestlerOperationException res)
                    {
                        var e   = res.E;
                        var err = (JObject)res.Content;
                        if (err != null && err["errors"] != null)
                        {
                            _log($"Request failed: {err}"); throw;
                        }
                        else
                        {
                            var statusCode = Math.Floor((int)res.StatusCode / 100M);
                            if (statusCode == 5)
                            {
                                throw e;
                            }
                            else if (statusCode == 4)
                            {
                                throw e;
                            }
                            else
                            {
                                throw e;
                            }
                        }
                    }
                }
                else
                {
                    var err = x.Exception;
                    _log($"_request failed: {err}"); throw err;
                }
            };
            Func <bool, Task <JObject> > requestFn2 = async(forceOAuth) => await requestFn(GetOAuthToken(forceOAuth));

            return(await _retry.start(requestFn2));
        }