private async Task <AblyResponse> CallAuthUrl(AuthOptions mergedOptions, TokenParams @params) { var url = mergedOptions.AuthUrl; var protocol = Options.UseBinaryProtocol == false ? Protocol.Json : Protocol.MsgPack; var authRequest = new AblyRequest(url.ToString(), mergedOptions.AuthMethod, protocol); if (mergedOptions.AuthMethod == HttpMethod.Get) { authRequest.AddQueryParameters(@params.ToRequestParams(mergedOptions.AuthParams)); } else { authRequest.PostParameters = @params.ToRequestParams(mergedOptions.AuthParams); } authRequest.Headers = authRequest.Headers.Merge(mergedOptions.AuthHeaders); authRequest.SkipAuthentication = true; AblyResponse response = await _rest.ExecuteRequest(authRequest); if (response.Type == ResponseType.Binary) { throw new AblyException( new ErrorInfo( string.Format("Content Type {0} is not supported by this client library", response.ContentType), 500)); } return(response); }
internal static ErrorInfo Parse(AblyResponse response) { // RSA4d, if we have 403 response default to code 40300, this may be overwritten // if the response has a usable JSON body int errorCode = response.StatusCode == HttpStatusCode.Forbidden ? 40300 : 50000; string reason = string.Empty; if (response.Type == ResponseType.Json) { try { var json = JObject.Parse(response.TextResponse); if (json["error"] != null) { reason = (string)json["error"]["message"]; errorCode = (int)json["error"]["code"]; } } catch (Exception ex) { // If there is no json or there is something wrong we don't want to throw from here. Debug.WriteLine(ex.Message); } } return(new ErrorInfo(reason.IsEmpty() ? "Unknown error" : reason, errorCode, response.StatusCode)); }
private void LogResponse(AblyResponse ablyResponse, string url) { if (Logger.IsDebug == false) { return; } StringBuilder logMessage = new StringBuilder($"Response from: {url}"); logMessage.AppendLine($"Status code: {(int)ablyResponse.StatusCode} {ablyResponse.StatusCode}"); logMessage.AppendLine("---- Response Headers ----"); foreach (var header in ablyResponse.Headers) { logMessage.AppendLine($"{header.Key}: {header.Value.JoinStrings()}"); } logMessage.AppendLine($"Content Type: {ablyResponse.ContentType}"); logMessage.AppendLine($"Encoding: {ablyResponse.Encoding}"); logMessage.AppendLine($"Type: {ablyResponse.Type}"); logMessage.AppendLine("---- Response Body ----"); if (ablyResponse.Type != ResponseType.Binary) { logMessage.AppendLine(ablyResponse.TextResponse); } else if (ablyResponse.Body != null) { logMessage.AppendLine(ablyResponse.Body.GetText()); } Logger.Debug(logMessage.ToString()); }
internal PaginatedResult(AblyResponse response, int limit, Func <PaginatedRequestParams, Task <PaginatedResult <T> > > executeDataQueryFunc) { Response = response; Limit = limit; ExecuteDataQueryFunc = executeDataQueryFunc; if (response.Headers != null) { CurrentQueryParams = PaginatedRequestParams.GetLinkQuery(response.Headers, DataRequestLinkType.Current); NextQueryParams = PaginatedRequestParams.GetLinkQuery(response.Headers, DataRequestLinkType.Next); FirstQueryParams = PaginatedRequestParams.GetLinkQuery(response.Headers, DataRequestLinkType.First); } }
internal HttpPaginatedResponse(AblyResponse response, int limit, PaginatedRequestParams requestParams, Func <PaginatedRequestParams, Task <HttpPaginatedResponse> > executeDataQueryFunc) : base(response, limit, null) { ExecuteDataQueryFunc = executeDataQueryFunc; StatusCode = Response.StatusCode; Headers = response.Headers; if (Response.Headers.TryGetValues(AblyErrorCodeHeader, out var errorCodeHeaderValues)) { string errCodeStr = errorCodeHeaderValues.FirstOrDefault(); if (int.TryParse(errCodeStr, out var errCode)) { ErrorCode = errCode; } } if (Response.Headers.TryGetValues(AblyErrorCodeHeader, out var errorMessageHeaderValues)) { ErrorMessage = errorMessageHeaderValues.FirstOrDefault(); } ExecuteDataQueryFunc = executeDataQueryFunc; if (response.TextResponse.IsNotEmpty()) { var data = JToken.Parse(response.TextResponse); if (data is JArray arr) { foreach (var token in arr) { Items.Add(token); } } else { Items.Add(data); } } InitializeQuery(CurrentQueryParams, requestParams); InitializeQuery(NextQueryParams, requestParams); }
private static async Task <AblyResponse> GetAblyResponse(HttpResponseMessage response) { byte[] content = null; MediaTypeHeaderValue contentTypeHeader = null; if (response.Content != null) { content = await response.Content?.ReadAsByteArrayAsync(); contentTypeHeader = response.Content?.Headers.ContentType; } var ablyResponse = new AblyResponse(contentTypeHeader?.CharSet, contentTypeHeader?.MediaType, content) { StatusCode = response.StatusCode, Headers = response.Headers }; return(ablyResponse); }
internal static ErrorInfo Parse(AblyResponse response) { string reason = ""; int errorCode = 500; if (response.Type == ResponseType.Json) { try { var json = JObject.Parse(response.TextResponse); if (json["error"] != null) { reason = (string)json["error"]["message"]; errorCode = (int)json["error"]["code"]; } } catch (Exception ex) { Debug.WriteLine(ex.Message); //If there is no json or there is something wrong we don't want to throw from here. The } } return(new ErrorInfo(StringExtensions.IsEmpty(reason) ? "Unknown error" : reason, errorCode, response.StatusCode)); }
internal static AblyException FromResponse(AblyResponse response) { return(new AblyException(ErrorInfo.Parse(response))); }