Last active
November 5, 2023 20:35
-
-
Save anilkay/255787a26fb03810295428686e7cdeb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Nice Url Builder for Dotnet. | |
| using Flurl; | |
| using System.Text; | |
| string GetWeatherOfCityUrl(string cityName){ | |
| var weather = "https://wttr.in" | |
| .AppendPathSegment(cityName) | |
| .SetQueryParams(new | |
| { | |
| format = 1 | |
| }); | |
| return weather; | |
| }; | |
| async Task<string> GetWeatherOfCity(string cityUrl) | |
| { | |
| var weatherResponse = await client.GetAsync(cityUrl); | |
| if(weatherResponse.IsSuccessStatusCode) | |
| { | |
| var weatherContent = await weatherResponse.Content.ReadAsStringAsync(); | |
| return weatherContent; | |
| } | |
| else | |
| { | |
| throw new Exception("Error getting weather"); | |
| } | |
| }; | |
| var weatherOfSaltLakeUrl= GetWeatherOfCityUrl("Salt Lake City"); | |
| var weatherOfSaltLake = await GetWeatherOfCity(weatherOfSaltLakeUrl); | |
| Console.WriteLine(weatherOfSaltLake); | |
| var weatherOfIstanbulUrl= GetWeatherOfCityUrl("Istanbul"); | |
| var weatherOfIstanbul = await GetWeatherOfCity(weatherOfIstanbulUrl); | |
| Console.WriteLine(weatherOfIstanbul); | |
| var searchUrl="https://google.com.tr".AppendPathSegment("search"). | |
| SetQueryParams(new { q = "macbook pro" }); | |
| string GetIpInfoUrl(string ip) | |
| { | |
| var ipInfoUrl = "http://ipinfo.io".AppendPathSegments(ip, "json"); | |
| return ipInfoUrl; | |
| } | |
| async Task<string> GetIpInfo(string ipInfoUrl) | |
| { | |
| var ipInfoResponse = await client.GetAsync(ipInfoUrl); | |
| if (ipInfoResponse.IsSuccessStatusCode) | |
| { | |
| var ipInfoContent = await ipInfoResponse.Content.ReadAsStringAsync(); | |
| return ipInfoContent; | |
| } | |
| else | |
| { | |
| throw new Exception("Error getting ip info"); | |
| } | |
| } | |
| var ipInfoOfMyIp = await GetIpInfo(GetIpInfoUrl("70.14.44.4")); | |
| Console.WriteLine(ipInfoOfMyIp); | |
| string GetByAbbeUrl(int month, int day) | |
| { | |
| var byAbbeUrl = "https://byabbe.se".AppendPathSegments("on-this-day", month, day, "events.json"); | |
| return byAbbeUrl; | |
| } | |
| async Task<ByAbbeResponse> GetByAbbe(string byAbbeUrl) | |
| { | |
| var byAbbeResponse = await client.GetAsync(byAbbeUrl); | |
| if (byAbbeResponse.IsSuccessStatusCode) | |
| { | |
| var byAbbeContent = await byAbbeResponse.Content.ReadAsStringAsync(); | |
| return JsonSerializer.Deserialize<ByAbbeResponse>(byAbbeContent); | |
| } | |
| else | |
| { | |
| throw new Exception("Error getting by abbe"); | |
| } | |
| } | |
| List<string>? GetEventsDescriptionByByAbbeResponse(ByAbbeResponse byAbbeResponse) | |
| { | |
| return byAbbeResponse?.events?.Select(eventItem => eventItem.description).ToList(); | |
| } | |
| //GetEventYearByByAbbeResponse | |
| List<string> GetEventYearByByAbbeResponse(ByAbbeResponse byAbbeResponse) | |
| { | |
| List<string> eventsYear = new(); | |
| foreach (var item in byAbbeResponse.events) | |
| { | |
| eventsYear.Add(item.year); | |
| } | |
| return eventsYear; | |
| } | |
| var byAbbeResponse = await GetByAbbe(GetByAbbeUrl(11, 5)); | |
| var eventsDescription = GetEventsDescriptionByByAbbeResponse(byAbbeResponse); | |
| var eventsYear = GetEventYearByByAbbeResponse(byAbbeResponse); | |
| //Combine Description with events year without zip | |
| List<string>? descriptionAndYears = eventsDescription?.Zip(eventsYear, (description,year) => description+" - "+ year).ToList(); | |
| if(descriptionAndYears != null) | |
| foreach (var item in descriptionAndYears) | |
| { | |
| Console.WriteLine(item); | |
| } | |
| public class ByAbbeResponse | |
| { | |
| public string date { get; set; } | |
| public string wikipedia { get; set; } | |
| public Event[] events { get; set; } | |
| } | |
| public class Event | |
| { | |
| public string year { get; set; } | |
| public string description { get; set; } | |
| public Wikipedia[] wikipedia { get; set; } | |
| } | |
| public class Wikipedia | |
| { | |
| public string title { get; set; } | |
| public string wikipedia { get; set; } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment