Created
May 22, 2013 12:01
Guild Wars 2 API example using RestSharp
This file contains 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
using RestSharp; | |
using System; | |
using System.Collections.Generic; | |
namespace Gw2ApiExample | |
{ | |
class Program | |
{ | |
public static void Main() | |
{ | |
RestClient client = new RestClient("https://api.guildwars2.com/v1"); | |
// load event names | |
var eventNames = LoadEventNames(client); | |
// load all events | |
RestRequest req = new RestRequest("events.json", Method.GET); | |
req.AddParameter("world_id", 2203); | |
IRestResponse<EventList> resp = client.Execute<EventList>(req); | |
// filter active events | |
Console.Write("The following events are active:"); | |
foreach (var ev in resp.Data.Events) | |
{ | |
if (ev.State == EventState.Active) | |
Console.WriteLine(eventNames[ev.EventId]); | |
} | |
Console.ReadLine(); | |
} | |
private static Dictionary<Guid, string> LoadEventNames(IRestClient client) | |
{ | |
Dictionary<Guid, string> eventNames = new Dictionary<Guid, string>(); | |
IRestRequest req = new RestRequest("event_names.json"); | |
IRestResponse<List<EventName>> resp = client.Execute<List<EventName>>(req); | |
foreach (EventName item in resp.Data) | |
{ | |
eventNames.Add(item.Id, item.Name); | |
} | |
return eventNames; | |
} | |
} | |
public class EventName | |
{ | |
public Guid Id | |
{ get; set; } | |
public string Name | |
{ get; set; } | |
} | |
public class EventList | |
{ | |
public List<Event> Events | |
{ get; set; } | |
} | |
public class Event | |
{ | |
public int WorldId | |
{ get; set; } | |
public int MapId | |
{ get; set; } | |
public Guid EventId | |
{ get; set; } | |
public EventState State | |
{ get; set; } | |
} | |
public enum EventState | |
{ | |
Active, | |
Success, | |
Fail, | |
Warmup, | |
Preparation | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment