internal T HandleJsonResponse <T>(RequestContext context) where T : class, new() { T result = null; ApiError errorResult = null; string json; try { var reader = new StreamReader(context.ResponseStream, Encoding.UTF8); json = reader.ReadToEnd(); } catch (Exception ex) { var ex1 = FX.InternalException("ReadStreamAsText", ex, ex.Message); ex1.StatusCode = context.HttpStatusCode; throw ex1; } if (validHttpCodes.Contains(context.HttpStatusCode)) { // the HTTP code matches a success response try { result = JsonConvert.DeserializeObject <T>(json); } catch (Exception ex) { var ex1 = FX.InternalException("SerializerDeserialize", ex, ex.Message); TryAttachContextDetails(context, ex1); throw ex1; } } else if (errorHttpCodes.Contains(context.HttpStatusCode)) { // the HTTP code matches a error response ThrowJsonErrorResult(context, errorResult, json); } else if (context.Method == "DELETE" && context.HttpStatusCode == (int)HttpStatusCode.NoContent) { result = JsonConvert.DeserializeObject <T>("true"); } else { // unknown HTTP code var ex1 = FX.ApiException("ApiUnknownHttpCode", context.HttpStatusCode); TryAttachContextDetails(context, null); throw ex1; } if (result == null) { var ex1 = FX.ApiException("ApiEmptyResult"); TryAttachContextDetails(context, null); throw ex1; } return(result); }
private static void ThrowJsonErrorResult(RequestContext context, ApiError errorResult, string json) { try { errorResult = JsonConvert.DeserializeObject <ApiError>(json); } catch (Exception ex) { var ex1 = FX.InternalException("SerializerDeserializeError", ex, ex.Message); TryAttachContextDetails(context, ex1); throw ex1; } { var ex1 = FX.ApiException("ApiErrorResult", errorResult.Status, errorResult.Message, context.UrlPath); TryAttachContextDetails(context, ex1); ex1.Data.Add("ErrorResult", errorResult); if (errorResult != null) { if (errorResult.Status == 401) { ex1.Data["ShouldRenewToken"] = true; } } throw ex1; } }
internal void HandleXmlErrorResponse(RequestContext context) { var error = this.HandleXmlResponse <ApiError>(context); Exception ex1; if (error != null) { ex1 = FX.ApiException("ApiErrorResult", error.Status, error.Message); } else { ex1 = FX.ApiException("ApiEmptyErrorResult", (int)(context.HttpStatusCode)); } throw ex1; }
private static void ThrowXmlErrorResult(RequestContext context, ApiError errorResult, string json) { // create serializers // it may fail if attributes are wrong XmlSerializer errorSerializer; try { errorSerializer = new XmlSerializer(typeof(ApiError)); } catch (Exception ex) { throw FX.InternalException("SerializerCtor", ex, ex.Message); } ApiError result; try { result = (ApiError)errorSerializer.Deserialize(context.ResponseStream); } catch (Exception ex) { var ex1 = FX.InternalException("SerializerDeserialize", ex, ex.Message); TryAttachContextDetails(context, ex1); throw ex1; } { var ex1 = FX.ApiException("ApiErrorResult", errorResult.Status, errorResult.Message, context.UrlPath); TryAttachContextDetails(context, ex1); ex1.Data.Add("ErrorResult", errorResult); if (errorResult != null) { if (errorResult.Status == 401) { ex1.Data["ShouldRenewToken"] = true; } } throw ex1; } }
internal string HandleJsonRawResponse(RequestContext context) { ApiError errorResult = null; // create serializers // it may fail if attributes are wrong ////XmlSerializer serializer, errorSerializer; var settings = new JsonSerializerSettings { }; string json; try { var reader = new StreamReader(context.ResponseStream, Encoding.UTF8); json = reader.ReadToEnd(); } catch (Exception ex) { throw FX.InternalException("ReadStreamAsText", ex, ex.Message); } if (validHttpCodes.Contains(context.HttpStatusCode)) { // the HTTP code matches a success response // do nothing here } else if (errorHttpCodes.Contains(context.HttpStatusCode)) { // the HTTP code matches a error response ThrowJsonErrorResult(context, errorResult, json); } else { // unknown HTTP code var ex1 = FX.ApiException("ApiUnknownHttpCode", context.HttpStatusCode); TryAttachContextDetails(context, null); throw ex1; } return(json); }
internal string HandleRawResponse(RequestContext context, Encoding encoding) { string text; try { var reader = new StreamReader(context.ResponseStream, encoding); text = reader.ReadToEnd(); } catch (Exception ex) { throw FX.InternalException("ReadStreamAsText", ex, ex.Message); } if (validHttpCodes.Contains(context.HttpStatusCode)) { // the HTTP code matches a success response // do nothing here } else if (errorHttpCodes.Contains(context.HttpStatusCode)) { // the HTTP code matches a error response var ex1 = FX.ApiException("ApiRawErrorResult"); TryAttachContextDetails(context, ex1); throw ex1; } else { // unknown HTTP code var ex1 = FX.ApiException("ApiUnknownHttpCode", context.HttpStatusCode); TryAttachContextDetails(context, null); throw ex1; } return(text); }
internal T HandleXmlResponse <T>(RequestContext context) where T : class, new() { T result = null; ApiError errorResult = null; // create serializers // it may fail if attributes are wrong XmlSerializer serializer, errorSerializer; try { serializer = new XmlSerializer(typeof(T)); errorSerializer = new XmlSerializer(typeof(ApiError)); } catch (Exception ex) { throw FX.InternalException("SerializerCtor", ex, ex.Message); } if (validHttpCodes.Contains(context.HttpStatusCode)) { // the HTTP code matches a success response try { result = (T)serializer.Deserialize(context.ResponseStream); } catch (Exception ex) { var ex1 = FX.InternalException("SerializerDeserialize", ex, ex.Message); TryAttachContextDetails(context, ex1); throw ex1; } } else if (errorHttpCodes.Contains(context.HttpStatusCode)) { // the HTTP code matches a error response try { errorResult = (ApiError)serializer.Deserialize(context.ResponseStream); } catch (Exception ex) { var ex1 = FX.InternalException("SerializerDeserializeError", ex, ex.Message); TryAttachContextDetails(context, ex1); throw ex1; } { var ex1 = FX.ApiException("ApiErrorResult", errorResult.Status, errorResult.Message, context.UrlPath); TryAttachContextDetails(context, null); ex1.Data.Add("ErrorResult", errorResult); if (errorResult != null) { if (errorResult.Status == 401) { ex1.Data["ShouldRenewToken"] = true; } } throw ex1; } } else { // unknown HTTP code var ex1 = FX.ApiException("ApiUnknownHttpCode", context.HttpStatusCode); TryAttachContextDetails(context, null); throw ex1; } if (result == null) { var ex1 = FX.ApiException("ApiEmptyResult"); TryAttachContextDetails(context, null); throw ex1; } return(result); }
internal T HandleJsonResponse <T>(RequestContext context) where T : class, new() { T result = null; ApiError errorResult = null; // create serializers // it may fail if attributes are wrong ////XmlSerializer serializer, errorSerializer; var settings = new JsonSerializerSettings { }; string json; try { var reader = new StreamReader(context.ResponseStream, Encoding.UTF8); json = reader.ReadToEnd(); } catch (Exception ex) { throw FX.InternalException("ReadStreamAsText", ex, ex.Message); } if (validHttpCodes.Contains(context.HttpStatusCode)) { // the HTTP code matches a success response try { result = JsonConvert.DeserializeObject <T>(json); } catch (Exception ex) { var ex1 = FX.InternalException("SerializerDeserialize", ex, ex.Message); TryAttachContextDetails(context, ex1); throw ex1; } } else if (errorHttpCodes.Contains(context.HttpStatusCode)) { // the HTTP code matches a error response try { errorResult = JsonConvert.DeserializeObject <ApiError>(json); } catch (Exception ex) { var ex1 = FX.InternalException("SerializerDeserializeError", ex, ex.Message); TryAttachContextDetails(context, ex1); throw ex1; } { var ex1 = FX.ApiException("ApiErrorResult", errorResult.Status, errorResult.Message, context.UrlPath); TryAttachContextDetails(context, null); ex1.Data.Add("ErrorResult", errorResult); if (errorResult != null) { if (errorResult.Status == 401) { ex1.Data["ShouldRenewToken"] = true; } } throw ex1; } } else { // unknown HTTP code var ex1 = FX.ApiException("ApiUnknownHttpCode", context.HttpStatusCode); TryAttachContextDetails(context, null); throw ex1; } if (result == null) { var ex1 = FX.ApiException("ApiEmptyResult"); TryAttachContextDetails(context, null); throw ex1; } return(result); }