-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAetrustHttpClient.vb
45 lines (34 loc) · 1.75 KB
/
AetrustHttpClient.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
Imports System.Net.Http
Imports System.Text
Public Class AetrustHttpClient
Private ReadOnly _client As HttpClient
Private ReadOnly _secret As String
Private ReadOnly _apiKey As String
Private ReadOnly _origin As String
Public Sub New(ByVal client As HttpClient, ByVal secret As String, ByVal apiKey As String, ByVal origin As String)
_client = client
_secret = secret
_apiKey = apiKey
_origin = origin
End Sub
Public Overridable Function CreateRequest(ByVal url As String, ByVal method As HttpMethod, ByVal Optional body As String = Nothing, ByVal Optional serverCurrent As DateTime? = Nothing) As HttpRequestMessage
Dim message = New HttpRequestMessage With {
.RequestUri = New System.Uri(url),
.Method = method
}
Dim methodAsString = method.Method.ToUpper()
Dim signature = AmericanEstateTrust.GenerateSignature(_secret, message.RequestUri.AbsolutePath, If(serverCurrent, DateTime.UtcNow), methodAsString, body)
message.Headers.Add("Signature", signature.Signature)
message.Headers.TryAddWithoutValidation("Content-Type", "application/vnd.api+json")
message.Headers.Add("Timestamp", signature.Timestamp.ToString())
message.Headers.Add("ApiKey", _apiKey)
message.Headers.Add("Origin", _origin)
If (body IsNot Nothing) Then
message.Content = New StringContent(body, Encoding.UTF8, "application/vnd.api+json")
End If
Return message
End Function
Public Overridable Async Function SendRequestAsync(ByVal message As HttpRequestMessage) As Task(Of HttpResponseMessage)
Return Await _client.SendAsync(message)
End Function
End Class