internal static Task <ApiResponse <T> > ExecuteRocketRequestJson <T>( string request, object data = null, string token = null, string userId = null, RequestType type = RequestType.Post, ApiEndPoint endPoint = null) { return(ExecuteRequestJsonInternal <T>( request, data, token, userId, type, endPoint)); }
static ApiEndPoint() { GeneralDev = new ApiEndPoint(nameof(GeneralDev), "http://dev.apianon.ru:3000/"); GeneralPublic = new ApiEndPoint(nameof(GeneralPublic), "http://public.apianon.ru:3000/"); GroupDev = new ApiEndPoint(nameof(GroupDev), "http://dev.apianon.ru:8080/"); GroupPublic = new ApiEndPoint(nameof(GroupPublic), "http://public.apianon.ru:8080/"); Chat = new ApiEndPoint(nameof(Chat), "https://chat.apianon.ru/api/v1/"); Photo = new ApiEndPoint(nameof(Photo), "http://fotoanon.ru/"); }
private static ApiResponse <T> GetApiResponse <T>(HttpResponseMessage httpResponse, string response, ApiEndPoint endPoint) { if (endPoint == ApiEndPoint.Chat) { return(JsonConvert.DeserializeObject <RocketApiResponse <T> >(response)? .GetApiResponse(httpResponse)); } return(JsonConvert.DeserializeObject <ApiResponse <T> >(response)); }
private static void PrepareRequest(HttpRequestMessage httpRequest, ApiEndPoint endPoint, string token = null, string userId = null) { if (endPoint == ApiEndPoint.Chat) { PrepareRequestRocket( httpRequest, token, userId); return; } PrepareRequestAnonym( httpRequest, token); }
private static async Task <ApiResponse> ExecuteRequestJsonInternal( string request, object data = null, string token = null, string userId = null, RequestType type = RequestType.Post, ApiEndPoint endPoint = null) { var response = await ExecuteRequestJsonInternal <object>( request, data, token, userId, type, endPoint) .ConfigureAwait(false); return(new ApiResponse { Code = response.Code, IsError = response.IsError, Message = response.Message }); }
private static async Task <ApiResponse <T> > ExecuteRequestJsonInternal <T>( string request, object data = null, string token = null, string userId = null, RequestType type = RequestType.Post, ApiEndPoint endPoint = null) { HttpRequestMessage httpRequest; HttpResponseMessage httpResponse; while (true) { try { httpRequest = new HttpRequestMessage(); if (endPoint == null) { endPoint = ApiEndPoint.GeneralPublic; } PrepareRequest( httpRequest, endPoint, token, userId); httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpRequest.RequestUri = new Uri(endPoint.Url + request); string jsonData = string.Empty; switch (type) { case RequestType.Get: { httpRequest.Method = HttpMethod.Get; httpRequest.Content = null; httpResponse = await HttpClient.SendAsync(httpRequest) .ConfigureAwait(false); break; } case RequestType.Post: default: { StringContent content = null; if (data != null) { jsonData = JsonConvert.SerializeObject(data); content = new StringContent(jsonData, Encoding.UTF8, "application/json"); } httpRequest.Method = HttpMethod.Post; httpRequest.Content = content; httpResponse = await HttpClient.SendAsync(httpRequest) .ConfigureAwait(false); break; } } var result = await httpResponse.Content.ReadAsStringAsync() .ConfigureAwait(false); //Pasha Lyubin, I love you ( no :)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ) //The best API in the world int resultCode; try { resultCode = (int)httpResponse.StatusCode; } catch (Exception) { resultCode = -1; } try { var resultResponse = GetApiResponse <T>( httpResponse, result, endPoint); OnConnectionStateChanged(ConnectionStateType.Connected); OnInformation(null, new CoreInformationEventArgs( $"Request[Uri={httpRequest?.RequestUri?.ToString() ?? "Unknown"}, Content={jsonData ?? "Unknown"}] response success. " + $"\nCode={resultCode}")); return(resultResponse); } catch (JsonSerializationException ex) { try { var resultResponse = JsonConvert.DeserializeObject <ApiResponse>(result); OnConnectionStateChanged(ConnectionStateType.Connected); OnError(null, new CoreErrorEventArgs( ex, $"Request[Uri={httpRequest?.RequestUri?.ToString() ?? "Unknown"}, Content={jsonData ?? "Unknown"}] response serialization error. " + $"\nCode={resultCode}, Response=\n{result ?? "Unknown"}\n")); return(new ApiResponse <T> { Code = resultResponse.Code, IsError = resultResponse.IsError, Data = default, Message = resultResponse.Message });