first pass of graphql interface

This commit is contained in:
Jonathan Jenne 2019-11-05 11:18:27 +01:00
parent 5343e051e9
commit a61610a90a
6 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Public Class LoginData
Public email As String
Public token As String
End Class

View File

@ -0,0 +1,3 @@
Public Class LogoutData
Public email As String
End Class

View File

@ -0,0 +1,5 @@
Public Class QueryData
Public Query As String
Public OperationName As String
Public Variables As New Object
End Class

View File

@ -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

View File

@ -44,6 +44,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.6.7\lib\net45\NLog.dll</HintPath>
</Reference>
@ -83,6 +86,10 @@
<Compile Include="ActiveDirectoryInterface\SyncUsers.MSSQL.vb" />
<Compile Include="ActiveDirectoryInterface\UserEqualityComparer.vb" />
<Compile Include="ActiveDirectoryInterface\UserPrincipalEx.vb" />
<Compile Include="GraphQLInterface.vb" />
<Compile Include="GrapQLInterface\LoginData.vb" />
<Compile Include="GrapQLInterface\LogoutData.vb" />
<Compile Include="GrapQLInterface\QueryData.vb" />
<Compile Include="ZUGFeRDInterface\Exceptions.vb" />
<Compile Include="My Project\AssemblyInfo.vb" />
<Compile Include="My Project\Application.Designer.vb">
@ -143,5 +150,6 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
</Project>

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
<package id="NLog" version="4.6.7" targetFramework="net461" />
</packages>