-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWebAPIHelper.vb
More file actions
147 lines (125 loc) · 5.88 KB
/
Copy pathWebAPIHelper.vb
File metadata and controls
147 lines (125 loc) · 5.88 KB
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
Imports System
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Net
Imports Newtonsoft.Json
Imports System.IO
Imports Newtonsoft.Json.Linq
Imports System.Security
Imports System.Runtime.InteropServices
Public Class WebAPIHelper
Private Shared SecurityToken As String = String.Empty
Private Shared SecurityTokenExpires As Date = Now
Private Shared _Password As SecureString
Private Shared _Username As String
Private Shared Async Function GenerateSecurityToken() As Task
Dim AccountWindow As New AccountManagement
AccountWindow.ShowDialog()
_Username = AccountWindow.txtUserName.Text
_Password = AccountWindow.txtPassword.SecurePassword
Dim result = Await APIPOST("/token", String.Format("username={0}&password={1}&grant_type=password", _Username, ConvertToUnsecureString(_Password)), True)
Dim json = JObject.Parse(result)
SecurityToken = json("access_token").ToString()
Dim s = json(".expires").ToString
Date.TryParse(s, SecurityTokenExpires)
End Function
Public Shared Function APIPOST(target As String, content As String) As Task(Of String)
Return APIPOST(target, content, False)
End Function
Private Shared Async Function APIPOST(target As String, content As String, SkipValidation As Boolean) As Task(Of String)
If Not SkipValidation Then Await ValidateSecurityToken()
Dim sReturn As String = String.Empty
Using client As New HttpClient()
client.BaseAddress = New Uri(My.Settings.EndPoint)
client.DefaultRequestHeaders.Accept.Clear()
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
client.DefaultRequestHeaders.Add("Authorization", "Bearer " & SecurityToken)
Dim response = Await client.PostAsync(target, New StringContent(content))
If (response.IsSuccessStatusCode) Then sReturn = response.Content.ReadAsStringAsync.Result
End Using
Return sReturn
End Function
Public Shared Async Function APIGET(target As String) As Task(Of String)
Await ValidateSecurityToken()
Dim sReturn As String = String.Empty
Using client As New HttpClient()
client.BaseAddress = New Uri(My.Settings.EndPoint)
client.DefaultRequestHeaders.Accept.Clear()
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
client.DefaultRequestHeaders.Add("Authorization", "Bearer " & SecurityToken)
Dim response = Await client.GetAsync(target)
If response.IsSuccessStatusCode Then
Dim values = Await response.Content.ReadAsStreamAsync()
Using reader = New StreamReader(values)
sReturn = reader.ReadToEnd
End Using
End If
End Using
Return sReturn
End Function
Private Shared Async Function ValidateSecurityToken() As Task
Dim attempt As Integer = 0
While SecurityToken = String.Empty OrElse SecurityTokenExpires < Now
If attempt > 2 Then Throw New Exception("Too many login attempts!")
attempt += 1
Dim t = GenerateSecurityToken()
Await t
End While
End Function
Private Shared Function ConvertToUnsecureString(password As SecureString) As String
Dim sReturn As String = String.Empty
If password Is Nothing Then Throw New ArgumentNullException("securePassword")
Dim unmanagedString = IntPtr.Zero
Try
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(password)
sReturn = Marshal.PtrToStringUni(unmanagedString)
Catch
Finally
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString)
End Try
Return sReturn
End Function
Friend Shared Function UploadFile(FileName As String) As Boolean
If (CheckForInternetConnection()) Then
Dim FileInfo As New System.IO.FileInfo(FileName)
Dim FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri("ftp://ringdev.com/" & FileInfo.Name)), System.Net.FtpWebRequest)
FtpWebRequest.Credentials = New System.Net.NetworkCredential("ringdevc_" & My.Resources.String1, My.Resources.String2)
FtpWebRequest.KeepAlive = False
FtpWebRequest.Timeout = 20000
FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
FtpWebRequest.UseBinary = True
FtpWebRequest.ContentLength = FileInfo.Length
Dim buffLength As Integer = 2048
Dim buff(buffLength - 1) As Byte
Using FileStream As System.IO.FileStream = FileInfo.OpenRead()
Try
Using Stream As System.IO.Stream = FtpWebRequest.GetRequestStream()
Dim contentLen As Integer = FileStream.Read(buff, 0, buffLength)
Do While contentLen <> 0
Stream.Write(buff, 0, contentLen)
contentLen = FileStream.Read(buff, 0, buffLength)
Loop
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "Upload Error")
Return False
End Try
End Using
Else
MessageBox.Show("No connection available to upload standings or pairings.")
Return False
End If
Return True
End Function
Public Shared Function CheckForInternetConnection() As Boolean
Try
Using client = New WebClient()
Using stream = client.OpenRead("http://www.google.com")
Return True
End Using
End Using
Catch
Return False
End Try
End Function
End Class