123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using System;
- using System.Net.Http;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- namespace gameapi.Library
- {
- public class RestApiRequester
- {
- public async Task<dynamic> GetJson(string url)
- {
- var contentString = string.Empty;
- var client = new HttpClient();
- try
- {
- var response = await client.GetAsync(url);
- contentString = await response.Content.ReadAsStringAsync();
- return JsonSerializer.Deserialize<dynamic>(contentString);
- }
- catch (Exception e)
- {
- Console.WriteLine("Issue with making the curl; I tried " + url);
- Console.WriteLine(e.Message);
- contentString = $"Something went wrong: {e.Message}";
- }
- return contentString;
- }
- public async Task<dynamic> GetString(string url)
- {
- var client = new HttpClient();
- try
- {
- var response = await client.GetAsync(url);
- return await response.Content.ReadAsStringAsync();
- }
- catch (Exception e)
- {
- Console.WriteLine($"Issue with making the curl; I tried '{url}'.");
- Console.WriteLine(e.Message);
- return $"Something went wrong: {e.Message}";
- }
- }
- public async Task PostJson(string url, dynamic data)
- {
- var client = new HttpClient();
- try
- {
- var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
- await client.PostAsync(url, content);
- }
- catch (Exception e)
- {
- Console.WriteLine($"Issue with making the post; I tried '{url}'.");
- Console.WriteLine(e.Message);
- }
- }
- }
- }
|