-
Notifications
You must be signed in to change notification settings - Fork 1
The WowAPIData Class
In addition to being the class where you setup your application's initialization, the WowAPIData class serves as the base class to all API calls in the library. As such, all of the classes share some functionality.
The Load function is a public function that is implemented by each class that inherits from WowAPIData. This function is responsible for making sure the library calls the API and translating the returned JSON into usable classes. The Load function is automatically called by every constructor overload with one or more arguments. If you call the class's default constructor, you must call the Load function yourself.
void Main() {
Character.CharacterProfile character = new Character.CharacterProfile();
character.Options.Realm = "Lightbringer";
character.Options.Name = "Roncli";
character.Load();
Character.CharacterProfile alt = new Character.CharacterProfile("Korialstrasz", "Albarith");
}
Sub Main()
Dim character As New Character.CharacterProfile()
character.Options.Realm = "Lightbringer"
character.Options.Name = "Roncli"
character.Load()
Dim alt As New Character.CharacterProfile("Korialstrasz", "Albarith")
End Sub
After you load the data, you can see the JSON string that was returned by the API by accessing the Data property.
void Main() {
Character.CharacterProfile character = new Character.CharacterProfile("Lightbringer", "Roncli");
string JSON = character.Data;
}
Sub Main()
Dim character As New Character.CharacterProfile("Lightbringer", "Roncli")
Dim JSON As String = character.Data
End Sub
Caching is a very important aspect of LibWowAPI. By default, all requests are cached by the library in order to save on unnecessary API calls for commonly called requests.
The default cache length varies depending on the type of data being returned:
- Data that changes at most once per major patch is cached for 30 days.
- Data that changes on a daily basis is cached once every 24 hours.
- Data that is time-sensitive is cached for 15 minutes.
You can change this default by assigning a new TimeSpan to the CacheLength.
void Main() {
Character.CharacterProfile character = new Character.CharacterProfile();
character.CacheLength = new TimeSpan(12, 0, 0);
}
Sub Main()
Dim character As New Character.CharacterProfile()
character.CacheLength = New TimeSpan(12, 0, 0)
End Sub
To determine if your request was retrieved from the cache instead of from Blizzard, check the CacheHit property.
void Main() {
CacheHitTest();
CacheHitTest();
}
private void CacheHitTest() {
Character.CharacterProfile character = new Character.CharacterProfile();
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue);
if (character.CacheHit.HasValue) {
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value);
}
character.Options.Realm = "Lightbringer";
character.Options.Realm = "Roncli";
character.Load();
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue);
if (character.CacheHit.HasValue) {
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value);
}
}
Sub Main()
CacheHitTest()
CacheHitTest()
End Sub
Private Sub CacheHitTest()
Dim character As New Character.CharacterProfile()
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue)
If character.CacheHit.HasValue Then
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value)
End If
character.Options.Realm = "Lightbringer"
character.Options.Realm = "Roncli"
character.Load()
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue)
If character.CacheHit.HasValue Then
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value)
End If
End Sub
CacheHit has a value: False
CacheHit has a value: True
Cache hit occurred: False
CacheHit has a value: False
CacheHit has a value: True
Cache hit occurred: True
To clear the cache, call the Clear function just prior to calling the Load function.
void Main() {
CacheHitTest();
CacheHitTest();
}
private void CacheHitTest() {
Character.CharacterProfile character = new Character.CharacterProfile();
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue);
if (character.CacheHit.HasValue) {
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value);
}
character.Options.Realm = "Lightbringer";
character.Options.Realm = "Roncli";
character.Clear();
character.Load();
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue);
if (character.CacheHit.HasValue) {
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value);
}
}
Sub Main()
CacheHitTest()
CacheHitTest()
End Sub
Private Sub CacheHitTest()
Dim character As New Character.CharacterProfile()
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue)
If character.CacheHit.HasValue Then
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value)
End If
character.Options.Realm = "Lightbringer"
character.Options.Realm = "Roncli"
character.Clear()
character.Load()
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue)
If character.CacheHit.HasValue Then
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value)
End If
End Sub
CacheHit has a value: False
CacheHit has a value: True
Cache hit occurred: False
CacheHit has a value: False
CacheHit has a value: True
Cache hit occurred: False
If you do not wish to cache the results, set the CacheResults property to false just prior to calling the Load function.
void Main() {
CacheHitTest();
CacheHitTest();
}
private void CacheHitTest() {
Character.CharacterProfile character = new Character.CharacterProfile();
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue);
if (character.CacheHit.HasValue) {
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value);
}
character.Options.Realm = "Lightbringer";
character.Options.Realm = "Roncli";
character.CacheResults = false;
character.Load();
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue);
if (character.CacheHit.HasValue) {
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value);
}
}
Sub Main()
CacheHitTest()
CacheHitTest()
End Sub
Private Sub CacheHitTest()
Dim character As New Character.CharacterProfile()
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue)
If character.CacheHit.HasValue Then
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value)
End If
character.Options.Realm = "Lightbringer"
character.Options.Realm = "Roncli"
character.CacheResults = False
character.Load()
Console.WriteLine("CacheHit has a value: {0}", character.CacheHit.HasValue)
If character.CacheHit.HasValue Then
Console.WriteLine("Cache hit occurred: {0}", character.CacheHit.Value)
End If
End Sub
CacheHit has a value: False
CacheHit has a value: True
Cache hit occurred: False
CacheHit has a value: False
CacheHit has a value: True
Cache hit occurred: False
The IfModifiedSince property can be set prior to send the if-modified-since header to the API. This is ignored if the API call has previously been cached.
void Main() {
Character.CharacterProfile character = new Character.CharacterProfile();
character.IfModifiedSince = new Date(2011, 8, 23, 14, 22, 0);
}
Sub Main()
Dim character As New Character.CharacterProfile()
character.IfModifiedSince = New Date(2011, 8, 23, 14, 22, 0)
End Sub
If you used the IfModifiedSince property, the Modified property will be set to True if the data has modified since. If it is false, then there will be no data returned with the class.