Imports System.Net
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.IO
Imports System.Text
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.Windows.Forms.VisualStyles.VisualStyleElement

Module mesAPIMoudule
    Private apiURL As String = "http://20.249.208.89:3030/"
    Public userAccess As String
    Public useUserID As String

    Public Function apiRequest_Post(JsonData As String, apiUrlType As String) As String
        Try
            Dim apiURLSet As String = apiURL & apiUrlType
            Dim apiRequest As HttpWebRequest = CType(HttpWebRequest.Create(apiURLSet), HttpWebRequest)
            apiRequest.ContentType = "application/json"
            apiRequest.Method = "POST"

            Using apiStreamW As StreamWriter = New StreamWriter(apiRequest.GetRequestStream)
                apiStreamW.Write(JsonData)
                apiStreamW.Flush()
                apiStreamW.Close()

                Using apiResponse As HttpWebResponse = apiRequest.GetResponse
                    Using apiStreamR As StreamReader = New StreamReader(apiResponse.GetResponseStream)
                        Dim result As String = apiStreamR.ReadToEnd

                        Return result
                    End Using
                End Using
            End Using

            Return "-1"
        Catch ex As Exception
            Return "-1"
        End Try
    End Function

    Public Function apiRequest_GET(apiUrlType As String, sqlData As String) As String
        Try

            Dim apiURLSet As String = apiURL & apiUrlType & "?" & sqlData

            Dim apiRequest As HttpWebRequest = CType(HttpWebRequest.Create(apiURLSet), HttpWebRequest)
            apiRequest.ContentType = "application/json"
            apiRequest.Method = "GET"
            apiRequest.Headers.Add("access-token", userAccess)

            Using apiResponse As HttpWebResponse = apiRequest.GetResponse
                Using apiStream As Stream = apiResponse.GetResponseStream
                    Using apiReader As StreamReader = New StreamReader(apiStream)
                        Return apiReader.ReadToEnd
                    End Using
                End Using
            End Using

            Return "-1"
        Catch ex As Exception
            Return "-1"
        End Try
    End Function

    Public Function apiRequest_PUT(jsonData As String, pkNum As String) As String

        Try
            Dim testUri As String = apiURL & "item-outflows/id/" & pkNum & ""

            Dim apiRequest As HttpWebRequest = CType(HttpWebRequest.Create(testUri), HttpWebRequest)
            apiRequest.Method = "PUT"
            apiRequest.ContentType = "application/json"
            apiRequest.Headers.Add("access-token", userAccess)


            Using apiStreamW As StreamWriter = New StreamWriter(apiRequest.GetRequestStream)
                apiStreamW.Write(jsonData)
                apiStreamW.Flush()
                apiStreamW.Close()

                Using apiResponse As HttpWebResponse = apiRequest.GetResponse
                    Using apiStreamR As StreamReader = New StreamReader(apiResponse.GetResponseStream)
                        Dim result As String = apiStreamR.ReadToEnd

                        Return result
                    End Using
                End Using
            End Using

            Return "-1"
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical)
            Return "-1"
        End Try

    End Function


    Private Function setUUID() As String
        Try
            Dim myUUID As Guid = Guid.NewGuid
            Return myUUID.ToString
        Catch ex As Exception
            MsgBox(ex.Message)
            Return "-1"
        End Try
    End Function


End Module
