internal static async ValueTask <OperationResult> GetOperationResultCodeAsync( this IFlurlResponse httpResponseMessage) { switch (httpResponseMessage.StatusCode) { case 200: return(OperationResult.Ok); case 404: return(OperationResult.RecordNotFound); case 409: var message = await httpResponseMessage.GetStringAsync(); return((OperationResult)int.Parse(message)); default: var messageUnknown = await httpResponseMessage.GetStringAsync(); throw new Exception( $"Unknown HTTP result Code{httpResponseMessage.StatusCode}. Message: {messageUnknown}"); } }
private async Task <IFlurlResponse> UpdateAsync(PropertyUpdate data) { IFlurlResponse responseHttp = await $"{_odata}({data.PropertyId})".WithOAuthBearerToken(token.Content).PatchJsonAsync(data); string responseBody = await responseHttp.GetStringAsync(); if (!responseHttp.ResponseMessage.IsSuccessStatusCode && responseHttp.StatusCode != 401) { _logger.Error($"Bad update in the database, Message: {responseBody}, StatusCode: {responseHttp.StatusCode}, Json for request: {JsonConvert.SerializeObject(data)}"); } else if (!responseHttp.ResponseMessage.IsSuccessStatusCode && responseHttp.StatusCode == 401) { await AuthorizeStart(); return(await UpdateAsync(data)); } return(responseHttp); }
private async Task <T> SelectAsync <T>(RealtyObjectsResponse data) { IFlurlResponse responseHttp = await $"{_odata}?$select=mtr_propertyid,statuscode&$filter=mtr_propertyid eq '{data.RealtyObjectId}'".WithOAuthBearerToken(token.Content).GetAsync(); string responseBody = await responseHttp.GetStringAsync(); if (!responseHttp.ResponseMessage.IsSuccessStatusCode && responseHttp.StatusCode != 401) { _logger.Error($"Bad select in the database, Message: {responseBody}, StatusCode: {responseHttp.StatusCode}, Json for request: {JsonConvert.SerializeObject(data)}"); } else if (!responseHttp.ResponseMessage.IsSuccessStatusCode && responseHttp.StatusCode == 401) { await AuthorizeStart(); return(await SelectAsync <T>(data)); } return(JsonConvert.DeserializeObject <T>(responseBody)); }
/// <summary> /// Extracts the errors from a http response /// </summary> /// <param name="response">A http response object</param> /// <returns>Returns a dictionary with the api call errors</returns> public static Dictionary <string, List <string> > GetDetails(this IFlurlResponse response) { Dictionary <string, List <string> > details = new Dictionary <string, List <string> >(); try { JObject json = JObject.Parse(response.GetStringAsync().Result); if (json.ContainsKey("errors")) { return(json["errors"].AsJEnumerable() .Cast <JProperty>() .ToDictionary( p => p.Name, p => p.Value.Children().Values <string>().ToList() )); } return(details); } catch (JsonReaderException) { return(null); } }
private async Task <bool> ReadResponseContentAsync(IFlurlResponse responseMessage) { string content = await responseMessage.GetStringAsync().ConfigureAwait(false); return(content == ""); }
/// <summary> /// extracts a list of transactions from the http response /// </summary> /// <param name="response">A http response object</param> /// <returns>A list of transactions</returns> public static List <Transaction> GetTransactions(this IFlurlResponse response) { return(JsonConvert.DeserializeObject <List <Transaction> >(response.GetStringAsync().Result)); }
public override async Task <ModuleResult> ProcessDiscordModule(IDiscordBotContext context) { // special case FAQ channel var message = context.Message; var messageCommand = context.Interaction as SocketMessageCommand; if (message != null && BotConfig.Instance.FaqEndpoints != null && BotConfig.Instance.FaqEndpoints.TryGetValue(message.Channel.Id, out var faq) && faq.Endpoint != null && (context.Reaction == faq.Reaction || messageCommand != null && messageCommand.CommandName.IEquals(faq.Command) || string.IsNullOrEmpty(context.Reaction) && !string.IsNullOrEmpty(faq.EndsWith) && message.Content.EndsWith(faq.EndsWith))) { if (messageCommand != null) { await messageCommand.DeferAsync(); } await message.AddReactionAsync(new Emoji(faq.Reaction)); string content = message.Content.Replace("<@85614143951892480>", "ub3r-b0t"); IFlurlResponse result = null; try { result = await faq.Endpoint.ToString().WithHeader("Authorization", BotConfig.Instance.FaqKey).PostJsonAsync(new { question = content, top = 2 }); } catch (Exception ex) { Log.Error(ex, "Failure in FAQ module"); } if (result != null && result.IsSuccessStatusCode()) { var response = await result.GetStringAsync(); var qnaData = JsonConvert.DeserializeObject <QnAMakerData>(response); var responses = new List <string>(); foreach (var answer in qnaData.Answers) { var score = Math.Floor(answer.Score); var answerText = WebUtility.HtmlDecode(answer.Answer); responses.Add($"{answerText} ({score}% match)"); } if (messageCommand != null) { await messageCommand.FollowupAsync(string.Join("\n\n", responses)); } else { await message.Channel.SendMessageAsync(string.Join("\n\n", responses)); } } else { if (messageCommand != null) { await messageCommand.FollowupAsync("An error occurred while fetching data"); } else { await message.Channel.SendMessageAsync("An error occurred while fetching data"); } } return(ModuleResult.Stop); } return(ModuleResult.Continue); }