private string makeRequest(string urlFragment, string method, string body)
        //throws NonSuccessfulResponseCodeException, PushNetworkException
        {
            HttpResponseMessage connection = getConnection(urlFragment, method, body);
            HttpStatusCode      responseCode;
            string responseMessage;
            string responseBody;

            try
            {
                responseCode    = connection.StatusCode;
                responseMessage = connection.ReasonPhrase;
                responseBody    = connection.Content.ReadAsStringAsync().Result;
            }
            catch (Exception ioe)
            {
                Debug.WriteLine(ioe.Message);
                Debug.WriteLine(ioe.StackTrace);
                throw new PushNetworkException(ioe);
            }

            switch (responseCode)
            {
            case HttpStatusCode.RequestEntityTooLarge:     // 413
                throw new RateLimitException("Rate limit exceeded: " + responseCode);

            case HttpStatusCode.Unauthorized:  // 401
            case HttpStatusCode.Forbidden:     // 403
                throw new AuthorizationFailedException("Authorization failed!");

            case HttpStatusCode.NotFound:     // 404
                throw new NotFoundException("Not found");

            case HttpStatusCode.Conflict:     // 409
                MismatchedDevices mismatchedDevices = null;
                try
                {
                    mismatchedDevices = JsonUtil.fromJson <MismatchedDevices>(responseBody);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    Debug.WriteLine(e.StackTrace);
                    throw new PushNetworkException(e);
                }
                throw new MismatchedDevicesException(mismatchedDevices);

            case HttpStatusCode.Gone:     // 410
                StaleDevices staleDevices = null;
                try
                {
                    staleDevices = JsonUtil.fromJson <StaleDevices>(responseBody);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    Debug.WriteLine(e.StackTrace);
                    throw new PushNetworkException(e);
                }
                throw new StaleDevicesException(staleDevices);

            case HttpStatusCode.LengthRequired:    //411:
                DeviceLimit deviceLimit = null;
                try
                {
                    deviceLimit = JsonUtil.fromJson <DeviceLimit>(responseBody);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    Debug.WriteLine(e.StackTrace);
                    throw new PushNetworkException(e);
                }
                throw new DeviceLimitExceededException(deviceLimit);

            case HttpStatusCode.ExpectationFailed:     // 417
                throw new ExpectationFailedException();
            }

            if (responseCode != HttpStatusCode.OK && responseCode != HttpStatusCode.NoContent) // 200 & 204
            {
                throw new NonSuccessfulResponseCodeException("Bad response: " + (int)responseCode + " " +
                                                             responseMessage);
            }

            return(responseBody);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="urlFragment"></param>
        /// <param name="method"></param>
        /// <param name="body"></param>
        /// <param name="unidentifiedAccess"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        /// <exception cref="NonSuccessfulResponseCodeException"></exception>
        /// <exception cref="PushNetworkException"></exception>
        /// <exception cref="MalformedResponseException"></exception>
        private async Task <string> MakeServiceRequestAsync(string urlFragment, string method, string?body, Action <int> responseCodeHandler, UnidentifiedAccess?unidentifiedAccess, CancellationToken?token = null)
        {
            if (token == null)
            {
                token = CancellationToken.None;
            }
            HttpResponseMessage connection = await GetServiceConnectionAsync(urlFragment, method, body, unidentifiedAccess, token.Value);

            HttpStatusCode responseCode;
            string         responseMessage;
            string         responseBody;

            try
            {
                responseCode    = connection.StatusCode;
                responseMessage = connection.ReasonPhrase;
                responseBody    = await connection.Content.ReadAsStringAsync();
            }
            catch (Exception ioe)
            {
                Logger.LogError("MakeServiceRequestAsync failed: {0}\n{1}", ioe.Message, ioe.StackTrace);
                throw new PushNetworkException(ioe);
            }

            responseCodeHandler.Invoke((int)responseCode);

            switch ((uint)responseCode)
            {
            case 413:     // HttpStatusCode.RequestEntityTooLarge
                throw new RateLimitException("Rate limit exceeded: " + responseCode);

            case 401:     // HttpStatusCode.Unauthorized
            case 403:     // HttpStatusCode.Forbidden
                throw new AuthorizationFailedException((int)responseCode, "Authorization failed!");

            case 404:     // HttpStatusCode.NotFound
                throw new NotFoundException("Not found");

            case 409:     // HttpStatusCode.Conflict
                MismatchedDevices mismatchedDevices = null;
                try
                {
                    mismatchedDevices = JsonUtil.FromJson <MismatchedDevices>(responseBody);
                }
                catch (Exception e)
                {
                    Logger.LogError("MakeServiceRequestAsync() failed: {0}\n{1}", e.Message, e.StackTrace);
                    throw new PushNetworkException(e);
                }
                throw new MismatchedDevicesException(mismatchedDevices);

            case 410:     // HttpStatusCode.Gone
                StaleDevices staleDevices = null;
                try
                {
                    staleDevices = JsonUtil.FromJson <StaleDevices>(responseBody);
                }
                catch (Exception e)
                {
                    Logger.LogError("MakeServiceRequestAsync() failed: {0}\n{1}", e.Message, e.StackTrace);
                    throw new PushNetworkException(e);
                }
                throw new StaleDevicesException(staleDevices);

            case 411:     //HttpStatusCode.LengthRequired
                DeviceLimit deviceLimit = null;
                try
                {
                    deviceLimit = JsonUtil.FromJson <DeviceLimit>(responseBody);
                }
                catch (Exception e)
                {
                    throw new PushNetworkException(e);
                }
                throw new DeviceLimitExceededException(deviceLimit);

            case 417:     // HttpStatusCode.ExpectationFailed
                throw new ExpectationFailedException();

            case 423:
                RegistrationLockFailure accountLockFailure;
                try
                {
                    accountLockFailure = JsonUtil.FromJson <RegistrationLockFailure>(responseBody);
                }
                catch (Exception e)
                {
                    throw new PushNetworkException(e);
                }
                throw new LockedException(accountLockFailure.Length, accountLockFailure.TimeRemaining);
            }

            if (responseCode != HttpStatusCode.OK && responseCode != HttpStatusCode.NoContent) // 200 & 204
            {
                throw new NonSuccessfulResponseCodeException((int)responseCode,
                                                             $"Bad response: {(int)responseCode} {responseMessage}");
            }

            return(responseBody);
        }