Пример #1
0
        public async Task <ObjectResponse <TResult>?> UrlPostToJsonObject <TResult, TData>(string request, TData?data = null, string?referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) where TResult : class where TData : class
        {
            if (string.IsNullOrEmpty(request) || (maxTries == 0))
            {
                throw new ArgumentNullException(nameof(request) + " || " + nameof(maxTries));
            }

            ObjectResponse <TResult>?result = null;

            for (byte i = 0; i < maxTries; i++)
            {
                await using StreamResponse? response = await UrlPostToStream(request, data, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false);

                if (response?.StatusCode.IsClientErrorCode() == true)
                {
                    if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors))
                    {
                        result = new ObjectResponse <TResult>(response);
                    }

                    break;
                }

                if (response?.StatusCode.IsServerErrorCode() == true)
                {
                    if (requestOptions.HasFlag(ERequestOptions.ReturnServerErrors))
                    {
                        result = new ObjectResponse <TResult>(response);
                    }

                    break;
                }

                if (response?.Content == null)
                {
                    continue;
                }

                TResult?obj;

                try {
                    using StreamReader steamReader = new StreamReader(response.Content);
                    using JsonReader jsonReader    = new JsonTextReader(steamReader);
                    JsonSerializer serializer = new JsonSerializer();

                    obj = serializer.Deserialize <TResult>(jsonReader);

                    if (obj == null)
                    {
                        ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorIsEmpty, nameof(obj)));

                        continue;
                    }
                } catch (Exception e) {
                    ArchiLogger.LogGenericWarningException(e);

                    continue;
                }

                return(new ObjectResponse <TResult>(response, obj));
            }

            if (maxTries > 1)
            {
                ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries));
                ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request));
            }

            return(result);
        }
Пример #2
0
        public async Task <ObjectResponse <T> > UrlPostToJsonObject <T>(string request, IReadOnlyCollection <KeyValuePair <string, string> > data = null, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) where T : class
        {
            if (string.IsNullOrEmpty(request) || (maxTries == 0))
            {
                ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries));

                return(null);
            }

            ObjectResponse <T> result = null;

            for (byte i = 0; i < maxTries; i++)
            {
                StringResponse response = await UrlPostToString(request, data, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false);

                if (response == null)
                {
                    return(null);
                }

                if (response.StatusCode.IsClientErrorCode())
                {
                    if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors))
                    {
                        result = new ObjectResponse <T>(response);
                    }

                    break;
                }

                if (string.IsNullOrEmpty(response.Content))
                {
                    continue;
                }

                T obj;

                try {
                    obj = JsonConvert.DeserializeObject <T>(response.Content);
                } catch (JsonException e) {
                    ArchiLogger.LogGenericWarningException(e);

                    if (Debugging.IsUserDebugging)
                    {
                        ArchiLogger.LogGenericDebug(string.Format(Strings.Content, response.Content));
                    }

                    continue;
                }

                return(new ObjectResponse <T>(response, obj));
            }

            if (maxTries > 1)
            {
                ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries));
                ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request));
            }

            return(result);
        }
Пример #3
0
        public async Task <ObjectResponse <T> > UrlGetToJsonObject <T>(string request, string referer = null, ERequestOptions requestOptions = ERequestOptions.None, byte maxTries = MaxTries) where T : class
        {
            if (string.IsNullOrEmpty(request) || (maxTries == 0))
            {
                ArchiLogger.LogNullError(nameof(request) + " || " + nameof(maxTries));

                return(null);
            }

            ObjectResponse <T> result = null;

            for (byte i = 0; i < maxTries; i++)
            {
                using StreamResponse response = await UrlGetToStream(request, referer, requestOptions | ERequestOptions.ReturnClientErrors, 1).ConfigureAwait(false);

                // ReSharper disable once UseNullPropagationWhenPossible - false check
                if (response == null)
                {
                    continue;
                }

                if (response.StatusCode.IsClientErrorCode())
                {
                    if (requestOptions.HasFlag(ERequestOptions.ReturnClientErrors))
                    {
                        result = new ObjectResponse <T>(response);
                    }

                    break;
                }

                if (response.Content == null)
                {
                    continue;
                }

                T obj;

                try {
                    using StreamReader streamReader = new StreamReader(response.Content);
                    using JsonReader jsonReader     = new JsonTextReader(streamReader);
                    JsonSerializer serializer = new JsonSerializer();

                    obj = serializer.Deserialize <T>(jsonReader);
                } catch (Exception e) {
                    ArchiLogger.LogGenericWarningException(e);

                    continue;
                }

                return(new ObjectResponse <T>(response, obj));
            }

            if (maxTries > 1)
            {
                ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorRequestFailedTooManyTimes, maxTries));
                ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request));
            }

            return(result);
        }