This repository has been archived by the owner on Jan 9, 2019. It is now read-only.
/
clsAuctionData.vb
223 lines (195 loc) · 9.4 KB
/
clsAuctionData.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
' LibWowAPI
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Lesser General Public License (LGPL) Version 3.0.
Imports System
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.IO
Imports System.Linq
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Json
Imports System.Text.Encoding
Imports roncliProductions.LibWowAPI.Enums
Imports roncliProductions.LibWowAPI.Extensions
Imports roncliProductions.LibWowAPI.Realm
Namespace roncliProductions.LibWowAPI.Auction
''' <summary>
''' A class that retrieves auction data from the Blizzard WoW API.
''' </summary>
''' <remarks>
''' <para>
''' The Blizzard WoW API provides snapshots of auction house data for each server approximately once per hour. This class can be used to retrieve that data.
''' <h5>Caching</h5>
''' Because this class has to make two calls to the API to retrieve the necessary data, caching works a little differently. The call that retreives the auction files is cached for 15 minutes, but the actual auction data itself is cached for 3 hours. The <see cref="AuctionData.CacheLength" /> property represents the former time, defaulting to 15 minutes. The latter time is not changable, nor can it be cleared since an auction snapshot for that particular point in time will never change.
''' </para>
''' </remarks>
''' <example>
''' The following example demonstrates how to make a call to the API to retrieve auction data.
''' <code>
''' using roncliProductions.LibWowAPI.Auction;
'''
''' namespace AuctionExample {
'''
''' public class AuctionClass {
'''
''' public Collection<Auctions> GetAuctionsForRealm(string realm) {
''' AuctionData auctions = new AuctionData();
''' auctions.Options.Realm = realm;
''' auctions.Load();
''' return auctions.Auctions;
''' }
'''
''' }
'''
''' }
''' </code>
''' <code lang="vbnet">
''' Imports roncliProductions.LibWowAPI.Auction
'''
''' Namespace AuctionExample
'''
''' Public Class AuctionClass
'''
''' Public Function GetAuctionsForRealm(realm As String) As Collection(Of Auctions)
''' Dim auctions As New AuctionData()
''' auctions.Options.Realm = realm
''' auctions.Load()
''' Return auctions.Auctions
''' End Function
'''
''' End Class
'''
''' End Namespace
''' </code>
''' </example>
Public NotInheritable Class AuctionData
Inherits WowAPIData
Private aAuctions As New Schema.auctions
#Region "WowAPIData Overrides"
''' <summary>
''' The length of time the data should be cached for, defaulting to 15 minutes for auction data.
''' </summary>
''' <value>This property gets or sets the CacheLength field.</value>
''' <returns>Returns the length of time the data should be cached for, defaulting to 15 minutes for auction data.</returns>
''' <remarks>The CacheLength is a <see cref="System.TimeSpan" /> that determines how long an API request should be stored in the cache. The default can be changed at any time before the <see cref="AuctionData.Load" /> method is called.</remarks>
Public Overrides Property CacheLength As New TimeSpan(0, 15, 0)
#Region "Protected Properties"
Protected Overrides ReadOnly Property CacheKey As String
Get
Return String.Format(CultureInfo.InvariantCulture, "LibWowAPI.AuctionData.{0}.{1:yyyyMMddHHmmss}", Options.Realm, Options.LastModified)
End Get
End Property
Protected Overrides ReadOnly Property URI As Uri
Get
Return Options.URI
End Get
End Property
#End Region
#Region "Methods"
''' <summary>
''' Loads the auction data.
''' </summary>
''' <remarks>This method calls the Blizzard WoW API, receives the JSON, and translates it into a <see cref="Collection(Of LibWowAPI.Auction.Auctions)" /> of <see cref="LibWowAPI.Auction.Auctions" />. Each item in the collection represents data received from one auction data file from the API.</remarks>
''' <exception cref="LibWowAPIException">If the JSON received from the API is invalid, the exception that caused it is packaged into the <see cref="LibWowAPIException.InnerException" /> of a <see cref="LibWowAPIException" />.</exception>
Public Overrides Sub Load()
Dim afFiles As New AuctionFiles()
afFiles.CacheLength = CacheLength
afFiles.IfModifiedSince = IfModifiedSince
afFiles.Options.Realm = Options.Realm
afFiles.Load()
colAuctions = New Collection(Of Auctions)
For Each fFile In afFiles.Files
Options.URI = fFile.URI
Options.LastModified = fFile.LastModified
Dim tsCacheLength As TimeSpan = CacheLength
CacheLength = New TimeSpan(3, 0, 0)
MyBase.Retrieve()
If Modified.HasValue AndAlso Not Modified Then
Return
End If
Try
Using msJSON As New MemoryStream(Unicode.GetBytes(Data))
aAuctions = CType(New DataContractJsonSerializer(GetType(Schema.auctions)).ReadObject(msJSON), Schema.auctions)
End Using
Catch sex As SerializationException
Throw New LibWowAPIException("The data returned by the Armory is invalid.", sex)
End Try
CacheLength = tsCacheLength
colAuctions.Add(
New Auctions(
(
From r In aAuctions.realms
Select New RealmName(
r.name,
r.slug
)
).ToCollection(),
fFile.LastModified,
New AuctionHouse(
(
From a In aAuctions.auctions
Select New Auction(
a.auc,
a.item,
a.owner,
a.ownerRealm,
a.bid,
a.buyout,
a.quantity,
a.timeLeft.GetAuctionTimeLeft(),
a.rand,
a.seed,
a.petSpeciesId,
CType(a.petBreedId, BattlePetBreed),
a.petLevel,
If(a.petSpeciesId = 0, Quality.Unknown, CType(a.petQualityId, Quality))
)
).ToCollection()
)
)
)
Next
End Sub
#End Region
#End Region
#Region "Properties"
''' <summary>
''' The options for looking up auction data.
''' </summary>
''' <value>This property gets or sets the Options field.</value>
''' <returns>Returns the options for looking up auction data.</returns>
''' <remarks>To set the options for the current request, set the appropriate properties on this object.</remarks>
Public Property Options As New AuctionDataOptions()
Private colAuctions As Collection(Of Auctions)
''' <summary>
''' A list of sets of auction data as returned from the Blizzard WoW API.
''' </summary>
''' <value>This property gets the Auctions field.</value>
''' <returns>Returns a list of sets of auction data as returned from the BLizzard WoW API.</returns>
''' <remarks>Each member of the collection represents a single <see cref="Auction" /> object. Each object represents data received from a single file from the API.</remarks>
Public ReadOnly Property Auctions As Collection(Of Auctions)
Get
Return colAuctions
End Get
End Property
#End Region
#Region "Constructors"
''' <summary>
''' A default constructor to retrieve auction information from the Blizzard WoW API.
''' </summary>
''' <remarks>This method creates a new instance of the <see cref="AuctionData" /> class. You must set the <see cref="AuctionDataOptions.Realm" /> property of the <see cref="AuctionData.Options" /> property, and then call the <see cref="AuctionData.Load" /> method to load the auction data.</remarks>
Public Sub New()
End Sub
''' <summary>
''' A constructor to retrieve auction information for a realm from the Blizzard WoW API.
''' </summary>
''' <param name="strRealm">The realm to load auction data for.</param>
''' <remarks>This method creates a new instance of the <see cref="AuctionData" /> class and automatically loads the data for the specified realm.</remarks>
Public Sub New(strRealm As String)
Options.Realm = strRealm
Load()
End Sub
#End Region
End Class
End Namespace