I'm trying to develop an Api that Gets info from GitHub's API and then put that selected information on my page as a JSON. First I did only a Console App to request the info and it's working fine - I can get all the information I need. I never did work with APIs though, I'm not sure how to follow up. The main goal is to have this information consumed through a GET request.
My code right now is like this:
private static List<Info> infos = new List<Info>();
public List<Info> Get()
{
Post();
return infos;
}
public void Post()
{
try
{
GetData().Wait();
}
catch (Exception e)
{
Console.WriteLine("Error" + e);
}
}
public static async Task<string> GetData()
{
var url = "https://api.github.com/users/MatheusReimer/repos?sort=created&direction=desc";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (Windows NT 10; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0");
Console.WriteLine(client.BaseAddress);
HttpResponseMessage response = await client.GetAsync(url);
string strResult = await response.Content.ReadAsStringAsync();
JToken token = JArray.Parse(strResult);
dynamic ResultArray = Newtonsoft.Json.JsonConvert.DeserializeObject(strResult);
//TESTING -------- var description = ResultArray[0].description;
List<Info> jsonobj = new List<Info> { };
foreach (dynamic array in ResultArray)
{
Info a = new Info();
a.Description = array.description;
a.CreatedAt = array.created_at;
a.Language = array.language;
a.ImageLocatedAt = array.owner.avatar_url;
a.Title = array.name;
jsonobj.Add(a);
}
Console.WriteLine(jsonobj.Count);
int repoCounter = 0;
for (int i = (jsonobj.Count - 1); i > 0; i--)
{
if (jsonobj[i].Language == "C#" && repoCounter <= 2)
{
//Console.WriteLine($"\nData: {jsonobj[i].CreatedAt} \nDescricao: {jsonobj[i].Description} \nImage located at: {jsonobj[i].ImageLocatedAt}\nTitle: {jsonobj[i].Title}\n\n\n");
Info grabedInfo = new Info();
grabedInfo.Description = jsonobj[i].Description;
grabedInfo.CreatedAt = jsonobj[i].CreatedAt;
grabedInfo.Language = jsonobj[i].Language;
grabedInfo.ImageLocatedAt = jsonobj[i].ImageLocatedAt;
grabedInfo.Title = jsonobj[i].Title;
infos.Add(grabedInfo);
repoCounter++;
}
}
var json = JsonConvert.SerializeObject(jsonobj);
return strResult;
}
}
But this obviously does not work, what's the best way to go now?
Two server HTTP methods should not call each other. This violates the Single-Responsibility Principle, and goes against the pattern of MVVM/MVC frameworks, such as those implemented in .NET
That being said, you don't need a POST for the shown example.
First, make your method actually return the correct values
public static async List<Info> GetData() {
...
List<Info> jsonobj = new List<Info> { };
...
return jsonobj;
}
Then change your server to use that
public List<Info> Get()
{
return GetData();
}
I'd also suggest relocating your "business-logic" code away from your "controller", and instead inject it via a constructor. Refer here for ASP.NET