Templates by BIGtheme NET
Anasayfa / Asp.MVC / Resmi Tatil ve Dini Bayram Günleri API (Holiday) C#

Resmi Tatil ve Dini Bayram Günleri API (Holiday) C#

Resmi tatil ve dini bayramları google üzerinden bir api yardımı ile çekebiliriz. https://www.googleapis.com/calendar/v3/calendars/turkish__tr%40holiday.calendar.google.com/events?key=<developersGoogleApiKey> adresi üzerinden tatil günlerine ait zamanlar gelmektedir. Aşağıdaki ekran görüntüsü örneğinde görebilirsiniz.


public class Holiday
{
public string kind { get; set; }
public string etag { get; set; }
public string summary { get; set; }
public DateTime updated { get; set; }
public string timeZone { get; set; }
public string accessRole { get; set; }
public object[] defaultReminders { get; set; }
public string nextSyncToken { get; set; }
public Item[] items { get; set; }

}
public class Item
{
public string kind { get; set; }
public string etag { get; set; }
public string id { get; set; }
public string status { get; set; }
public string htmlLink { get; set; }
public DateTime created { get; set; }
public DateTime updated { get; set; }
public string summary { get; set; }
public Creator creator { get; set; }
public Organizer organizer { get; set; }
public Start start { get; set; }
public End end { get; set; }
public string transparency { get; set; }
public string visibility { get; set; }
public string iCalUID { get; set; }
public int sequence { get; set; }
}

public class Creator
{
public string email { get; set; }
public string displayName { get; set; }
public bool self { get; set; }
}

public class Organizer
{
public string email { get; set; }
public string displayName { get; set; }
public bool self { get; set; }
}

public class Start
{
public string date { get; set; }
}

public class End
{
public string date { get; set; }
}
public class HolidayCostum
{
public string summary { get; set; }
public Start start { get; set; }
public End end { get; set; }
}

 


HttpResponseMessage httpResponse = await Startup.HttpClient.GetAsync(new Uri("https://www.googleapis.com/calendar/v3/calendars/turkish__tr%40holiday.calendar.google.com/events?key=developersGoogleApiKey")).ConfigureAwait(false);
string result = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
Holiday holiday = JsonConvert.DeserializeObject(result);

Yada

HttpResponseMessage httpResponse = await Startup.HttpClient.GetAsync(new Uri("https://www.googleapis.com/calendar/v3/calendars/turkish__tr%40holiday.calendar.google.com/events?key=developersGoogleApiKey")).ConfigureAwait(false);
string result = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
Holiday holiday = JsonConvert.DeserializeObject(result);

holiday.items = holiday.items.Aggregate(new List(), (x, y) =>
{
Item item = y;
int ii = item.summary.ToLower().IndexOf(” day “);
if (ii > 0)
item.summary = item.summary.Substring(0, ii);
else
item.summary = item.summary;

if (item.summary.IndexOf(“Bayrami”) != -1)
item.summary = item.summary.Replace(“Bayrami”, “Bayramı”);
x.Add(y);
return x;
}).ToArray();

IEnumerable holidayCostum = holiday.items.GroupBy(x => new
{
x.summary,
startDate = DateTime.ParseExact(x.start.date, “yyyy-MM-dd”, null).Year,
},
(key, g) => new
{
key.summary,
items = g,
}
).Aggregate(new List(), (x, y) =>
{
x.Add(new HolidayCostum
{
summary = y.summary,
start = new Start { date = y.items.First().start.date },
end = new End { date = y.items.Count() > 2 ? y.items.Last().end.date : y.items.First().start.date }
});
return x;
});