using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Globalization;
using System.Text.Json.Serialization;
private const string _fileContents =
private static readonly HttpClient client = new HttpClient();
static async Task Main(string[] args)
Console.WriteLine(@"Start time: " + DateTime.UtcNow);
await File.WriteAllTextAsync("ipaddresses.txt", _fileContents);
var addresses = await File.ReadAllLinesAsync("ipaddresses.txt");
var geoLocations = await GetLocationResults(addresses);
foreach(GeoLocation loc in geoLocations)
Console.WriteLine(loc.Country);
private static async Task<List<GeoLocation>>GetLocationResults(string[] addresses)
const string url = "https://cs-interview-geolocation.azurewebsites.net/api/GeolocationFunction?code=h/0av4gxkkTRz6d5HFIKr/U3b4ynT/CCNiKuHI7Wk7pVWYBKS8iZOw==";
var json = JsonSerializer.Serialize(new { addresses = addresses });
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, stringContent);
response.EnsureSuccessStatusCode();
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var responseStream = await response.Content.ReadAsStringAsync();
var geoLocations = JsonSerializer.Deserialize<GeoLocationResponse>(responseStream);
return geoLocations?.GeoLocations ?? new List<GeoLocation>() ;
private static async Task<List<User>>GetUserResults(string[] addresses)
const string url = "https://cs-interview-geolocation.azurewebsites.net/api/GeolocationFunction?code=h/0av4gxkkTRz6d5HFIKr/U3b4ynT/CCNiKuHI7Wk7pVWYBKS8iZOw==";
var json = JsonSerializer.Serialize(new { addresses = addresses });
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, stringContent);
response.EnsureSuccessStatusCode();
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var responseStream = await response.Content.ReadAsStringAsync();
var userResponse = JsonSerializer.Deserialize<UserResponse>(responseStream);
return userResponse?.Users ?? new List<User>() ;
private static async Task<List<Repository>> ProcessRepositories()
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");
var streamTask = client.GetStreamAsync("https://api.github.com/orgs/dotnet/repos");
var repositories = await JsonSerializer.DeserializeAsync<List<Repository>>(await streamTask);
public class GeoLocationResponse
[JsonPropertyName("geolocations")]
public List<GeoLocation> GeoLocations { get; set; }
[JsonPropertyName("address")]
public string Address { get; set; }
[JsonPropertyName("country")]
public string Country { get; set; }
public class UserResponse
[JsonPropertyName("users")]
public List<User> Users { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("address")]
public string Address { get; set; }
[JsonPropertyName("gender")]
public string Gennder { get; set; }
[JsonPropertyName("age")]
public string Age { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("description")]
public string Description { get; set; }
[JsonPropertyName("html_url")]
public Uri GitHubHomeUrl { get; set; }
[JsonPropertyName("homepage")]
public Uri Homepage { get; set; }
[JsonPropertyName("watchers")]
public int Watchers { get; set; }
[JsonPropertyName("pushed_at")]
public string JsonDate { get; set; }
public DateTime LastPush =>
DateTime.ParseExact(JsonDate, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);