Skip to content

Instantly share code, notes, and snippets.

@poke
Created May 22, 2013 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poke/5627036 to your computer and use it in GitHub Desktop.
Save poke/5627036 to your computer and use it in GitHub Desktop.
Guild Wars 2 API example using RestSharp
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