diff --git a/Modules.Interfaces/GrapQLInterface/LoginData.vb b/Modules.Interfaces/GrapQLInterface/LoginData.vb new file mode 100644 index 00000000..32ff79d5 --- /dev/null +++ b/Modules.Interfaces/GrapQLInterface/LoginData.vb @@ -0,0 +1,4 @@ +Public Class LoginData + Public email As String + Public token As String +End Class diff --git a/Modules.Interfaces/GrapQLInterface/LogoutData.vb b/Modules.Interfaces/GrapQLInterface/LogoutData.vb new file mode 100644 index 00000000..5dd0e0ff --- /dev/null +++ b/Modules.Interfaces/GrapQLInterface/LogoutData.vb @@ -0,0 +1,3 @@ +Public Class LogoutData + Public email As String +End Class diff --git a/Modules.Interfaces/GrapQLInterface/QueryData.vb b/Modules.Interfaces/GrapQLInterface/QueryData.vb new file mode 100644 index 00000000..6081f1c6 --- /dev/null +++ b/Modules.Interfaces/GrapQLInterface/QueryData.vb @@ -0,0 +1,5 @@ +Public Class QueryData + Public Query As String + Public OperationName As String + Public Variables As New Object +End Class diff --git a/Modules.Interfaces/GraphQLInterface.vb b/Modules.Interfaces/GraphQLInterface.vb new file mode 100644 index 00000000..8cb39dd9 --- /dev/null +++ b/Modules.Interfaces/GraphQLInterface.vb @@ -0,0 +1,119 @@ +Imports System.IO +Imports System.Net +Imports System.Security.Cryptography.X509Certificates +Imports System.Text +Imports DigitalData.Modules.Logging +Imports Newtonsoft.Json + +Public Class GraphQLInterface + Private _logConfig As LogConfig + Private _logger As Logger + Private _baseUrl As String + Private _userEmail As String + Private _userPassword As String + Private _certificate As X509Certificate + Private _cookieJar As CookieContainer + + Private _Encoding As New UTF8Encoding + + Public Sub New(LogConfig As LogConfig, BaseUrl As String, Email As String, Password As String, CertificateFile As String, CertificatePassword As String) + Try + _logConfig = LogConfig + _logger = LogConfig.GetLogger() + _baseUrl = BaseUrl + _userEmail = Email + _userPassword = Password + + _certificate = New X509Certificate(CertificateFile, CertificatePassword) + Catch ex As Exception + _logger.Error(ex) + End Try + End Sub + + Public Sub SaveCookies(Cookie As Cookie) + GetCookies().Add(Cookie) + End Sub + + Public Function Login() As HttpWebResponse + Try + Dim oLoginData As New LoginData() With {.email = _userEmail, .token = _userPassword} + Dim oBytes As Byte() = ToBytes(JsonConvert.SerializeObject(oLoginData)) + Dim oRequest As HttpWebRequest = GetRequest("/login", oBytes) + + Using oStream = oRequest.GetRequestStream() + oStream.Write(oBytes, 0, oBytes.Length) + End Using + + Return oRequest.GetResponse() + Catch ex As Exception + _logger.Error(ex) + Throw ex + End Try + End Function + + Public Function Logout() As HttpWebResponse + Try + Dim oLogoutData As New LogoutData() With {.email = _userEmail} + Dim oBytes As Byte() = ToBytes(JsonConvert.SerializeObject(oLogoutData)) + Dim oRequest As HttpWebRequest = GetRequest("/logout", oBytes) + + Using stream = oRequest.GetRequestStream() + stream.Write(oBytes, 0, oBytes.Length) + End Using + + Return oRequest.GetResponse() + Catch ex As Exception + _logger.Error(ex) + Throw ex + End Try + End Function + + Public Function GetData(Query As String, OperationName As String) As HttpWebResponse + Try + Dim oQueryData As New QueryData() With { + .OperationName = OperationName, + .Query = Query, + .Variables = New Object + } + Dim oBytes = ToBytes(JsonConvert.SerializeObject(oQueryData)) + Dim oRequest = GetRequest("/graphql", oBytes) + + Using stream = oRequest.GetRequestStream() + stream.Write(oBytes, 0, oBytes.Length) + End Using + + Return oRequest.GetResponse() + Catch ex As Exception + _logger.Error(ex) + Throw ex + End Try + End Function + + Private Function GetRequest(Url As String, PostData As Byte()) As HttpWebRequest + Try + Dim oRequest As HttpWebRequest = WebRequest.Create($"{_baseUrl}{Url}") + oRequest.Method = "POST" + oRequest.ContentType = "application/json" + oRequest.ContentLength = PostData.Length + oRequest.ClientCertificates.Add(_certificate) + oRequest.CookieContainer = GetCookies() + + Return oRequest + Catch ex As Exception + _logger.Error(ex) + Throw ex + End Try + End Function + + Private Function GetCookies() As CookieContainer + If _cookieJar Is Nothing Then + _cookieJar = New CookieContainer() + End If + + Return _cookieJar + End Function + + Private Function ToBytes(Str As String) As Byte() + Return _Encoding.GetBytes(Str) + End Function +End Class diff --git a/Modules.Interfaces/Interfaces.vbproj b/Modules.Interfaces/Interfaces.vbproj index 0b79cf85..82c333b1 100644 --- a/Modules.Interfaces/Interfaces.vbproj +++ b/Modules.Interfaces/Interfaces.vbproj @@ -44,6 +44,9 @@ + + ..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll + ..\packages\NLog.4.6.7\lib\net45\NLog.dll @@ -83,6 +86,10 @@ + + + + @@ -143,5 +150,6 @@ PreserveNewest + \ No newline at end of file diff --git a/Modules.Interfaces/packages.config b/Modules.Interfaces/packages.config index 99e34262..b96ef76e 100644 --- a/Modules.Interfaces/packages.config +++ b/Modules.Interfaces/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file