Imports System.IO
Imports System.Net
Imports Newtonsoft.Json.Linq

Module mesAPIMoudule

    Private apiURL As String = "http://20.249.208.89:3030/"
    Private loginIDAPI As String = "userid"
    Private loginPWAPI As String = "password"

    Public userAccess As String

    Public Function loginAPIRequest(userID As String, userPW As String, apiUrlType As String) As Boolean
        Dim jsonLogin As String = "{""" & loginIDAPI & """:""" & userID & """,""" & loginPWAPI & """:""" & userPW & """}"
        jsonLogin = apiRequest_Post(jsonLogin, apiUrlType)

        If jsonLogin = "-1" Then
            MsgBox("아이디 혹은 비밀번호를 잘못 입력하였습니다.", vbExclamation)
            Return False
        End If

        Dim jQuery As JObject = JObject.Parse(jsonLogin)

        If jQuery.SelectToken("code").ToString = "SUCCESS" Then
            jsonLogin = jQuery.SelectToken("data").ToString
            jQuery = JObject.Parse(jsonLogin)
            userAccess = jQuery.SelectToken("accessToken").ToString
            Return True
        Else
            MsgBox("로그인에 실패하였습니다. 입력된 정보를 확인하여 주십시오.", vbCritical)
            Return False
        End If

    End Function

    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

End Module
