public DebugRequestEvents(CompositeDisposable compositeDisposable, Request <T> request, int id) : base() { #if UNITY_EDITOR OnSuccess.Subscribe(x => DebugFormat.Log <Request <T> >($"[{request.RESTMethod}] {id} - OnSuccess\n\n{JsonConvert.SerializeObject(x, Formatting.Indented)}\n", deep: DEEP)).AddTo(compositeDisposable); OnSuccessRaw.Subscribe(x => DebugFormat.Log <Request <T> >($"[{request.RESTMethod}] {id} - OnSuccessRaw\n\n{Request<T>.JsonPrettify(x)}\n", deep: DEEP)).AddTo(compositeDisposable); #endif OnSerializationError.Subscribe(x => DebugFormat.LogError <Request <T> >($"[{request.RESTMethod}] {id} - OnSerializationError: {x}", deep: DEEP)).AddTo(compositeDisposable); OnHttpError.Subscribe(x => DebugFormat.LogError <Request <T> >($"[{request.RESTMethod}] {id} - OnHttpError\n\n {JsonConvert.SerializeObject(x, Formatting.Indented)}\n", deep: DEEP)).AddTo(compositeDisposable); OnHttpErrorRaw.Subscribe(x => DebugFormat.LogError <Request <T> >($"[{request.RESTMethod}] {id} - OnHttpErrorRaw\n\n{x}\n", deep: DEEP)).AddTo(compositeDisposable); OnNetworkError.Subscribe(x => DebugFormat.LogError <Request <T> >($"[{request.RESTMethod}] {id} - OnNetworkError\n\n {JsonConvert.SerializeObject(x, Formatting.Indented)}\n", deep: DEEP)).AddTo(compositeDisposable); }
private T fetch <T>(string url, OnHttpError handler, T proto, Func <Task <HttpResponseMessage> > method) { _log.Trace($" fetching: '{url}'"); var response = method().Result; var json = response.Content.ReadAsStringAsync().Result; _log.Trace($" got: '{json}'"); throwOnErrorStatus(handler, response, json); if (proto != null) { return(JsonConvert.DeserializeAnonymousType(json, proto)); } return (JsonConvert.DeserializeObject <T>(json)); }
/// <summary> /// in case of a failure will enqueue the failed request again /// </summary> /// <param name="task">the task that handled the request</param> /// <param name="state">the original request object</param> private void HandleResponse(Task <HttpResponseMessage> task, object state) { Interlocked.Decrement(ref pending); if (!task.IsCompleted || task.Status != TaskStatus.RanToCompletion) { Retry(state as QueueRequest); OnRequestError?.Invoke(task.Exception, task.Status == TaskStatus.Canceled); } else if ((int)task.Result.StatusCode >= 300) { Retry(state as QueueRequest); OnHttpError?.Invoke(task.Result.StatusCode); } else { OnRequestSent?.Invoke(); } }
private void throwOnErrorStatus(OnHttpError handler, HttpResponseMessage response, string reply) { if (response.IsSuccessStatusCode) { return; } var request = response.RequestMessage; var statusCode = (int)response.StatusCode; var requestHeaders = from header in request.Headers let values = header.Value.Select(value => $"'{value}'") select $"{header.Key}: {string.Join(";", values)}"; var responseHeaders = from header in response.Headers let values = header.Value.Select(value => $"'{value}'") select $"{header.Key}: {string.Join(";", values)}"; var detail = new WebApiExceptionDetail { statusCode = response.StatusCode, isSuccessfull = response.IsSuccessStatusCode, reason = response.ReasonPhrase, request = new WebApiExceptionDetail.Request { method = request.Method.Method, url = request.RequestUri.ToString(), headers = requestHeaders.ToArray() }, response = new WebApiExceptionDetail.Response { headers = responseHeaders.ToArray(), text = reply } }; handler?.Invoke(detail); throw new WebApiException($"Bad http status code {statusCode}", detail); }
T IWebApi.Post <T>(string url, OnHttpError handler, HttpContent content, T proto) { return (fetch(url, handler, proto, () => _http.PostAsync(url, content))); }
T IWebApi.Get <T>(string url, OnHttpError handler, T proto) { return (fetch(url, handler, proto, () => _http.GetAsync(url))); }
/// <summary> /// 触请求过程中发生错误事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void HttpError(object sender, Exception e) { OnHttpError?.Invoke(sender, e); }