A simple Swift wrapper for the Guild Wars 2 API (Version 2)
This library is made available as a Swift Package. To use it as a dependency in your project see this documentation from Apple.
The GW2API client is initialized as a singleton, so all you need to do is import the package (import GW2API) and you will then have full access to the API through the instance variable GW2Client.instance
By default, the client has the following settings:
- Base URL: https://api.guildwars2.com
- API Version:
v2 - Language: English (en)
- API Key: None (Set through
GW2Client.instance.setAPIKey([KEY HERE])) - Guild ID: None (Set through
GW2Client.instance.setGuildID([GUILD ID HERE]))
To print these settings, use the .settings() method like so:
* GW2Client.instance.settings()
For any unauthenticated endpoints, you can access data like this:
GW2Client.instance.[ENDPOINT HERE].[SUB-ENDPOINT HERE].get() { result in
switch result {
case .success(let (response, data))
// Do what you want with the data here
case .failure(let error):
// Put your error handling here
}
}An example:
GW2Client.instance.achievements.daily.get() { result in
switch result {
case .success(let (response, data)):
// Do what you want with the data here
case .failure(let error):
// Put your error handling here
}
}For any authenticated endpoint, you must do the following:
- Set your API key via the client instance:
GW2Client.instance.setAPIKey("Your API key here")- This will recursively set the API key to every endpoint that needs it in the client.
- Once the API key is set, you may access the data just like above:
GW2Client.instance.authenticated.account.pvp.games.get() { result in
switch result {
case .success(let (response, data)):
// Do what you want with the data here
case .failure(let error):
// Put your error handling here
}
}For some guild endpoints, you must specify a guild ID, and the authenticated guild endpoints require both a guild ID as well as a valid API key from that guild leader's account
- Set the Guild ID via the client instance:
GW2Client.instance.setGuildID("Your guild ID here") - Set the API key with a key from that guild leader's account via the client instance:
GW2Client.instance.setAPIKey("Guild leader's valid API key here")- Doing the above two steps will recursively set both the API key and the Guild ID to all endpoints that need either of them.
- After these two keys are set, you may access the data the same way as always:
GW2Client.instance.guild.authenticated.members.get() { result in
switch result {
case .success(let (response, data)):
// Do what you want with the data here
case .failure(let error):
// Put your error handling here
}
}Some data is addressable in the API via either a number or word ID. For each endpoint, if this type of search is available, there will be a .get() method with either an id or ids parameter which each take their respective data types (Int, String, or [Int], [String]). This string should contain the ID of the data you are looking for in the following formats:
id: 1, "Admin", etc...ids: [1, 2, 3], ["Admin", "EditingRights", "Roster"] etc...
Then to access this data, simply pass in the id or ids as a parameter to the .get() function of your endpoint like so:
GW2Client.instance.items.finishers.get(id: 1 OR ids: [1, 2, 3] etc.) { result in
switch result {
case .success(let (response, data)):
// Do what you want with the data here
case .failure(let error):
// Put your error handling here
}
}If you have any questions or problems, feel free to open an issue in this repository!
