﻿B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=9.8
@EndOfDesignText@
#IgnoreWarnings:12
'https://www.scrapingbee.com/curl-converter/javascript-fetch/

Private Sub Class_Globals
	Private BANano As BANano 'ignore
	Private mError As String
	Public Success As Boolean
	Public Response As Map
	Private parameters As Map
	Private data As Map
	Private baseURL As String
	Private headers As Map
	Public OK As Boolean
	Public Status As String
	Public StatusText As String
	Private bNoCors As Boolean
	Private sReferrerPolicy As String
	Public fetchOptions As BANanoFetchOptions
	Public fetch As BANanoFetch
	Public fetchResponse As BANanoFetchResponse
	Private obaseURL As String
End Sub


'<code>
''execute the fetch
'Dim j As SDUIFetch
''initialize the fetch with the base url
'j.Initialize("https://api.funtranslations.com/translate/minion.json")
''add a parameters
'j.AddParameter("text", txtMessage.value)
''set content type
'j.SetContentTypeApplicationJSON
''add header
'j.AddHeader("X-Funtranslations-Api-Secret", "")
''execute the post
'BANano.Await(J.PostWait)
'If j.Success Then
'	Dim Response As Map = j.response
'	If Response.ContainsKey("contents") Then
'		Dim minionTaal As String = SDUIShared.GetRecursive(Response, "contents.translated")
'		txtConvert.Value = minionTaal
'	End If
'Else	
'	txtConvert.Value = j.ErrorMessage
'End If
'</code>
Public Sub Initialize(url As String) As SDUIFetch
	' TargetModule is not used here
	baseURL = url
	obaseURL = url
	parameters.Initialize
	data.Initialize
	headers.Initialize 
	OK = False
	Status = ""
	StatusText = ""
	bNoCors = False
	sReferrerPolicy = ""
	fetchOptions.Initialize
	Return Me
End Sub

'https://www.b4x.com/android/forum/threads/sithasodaisy-tailwindcss-ui-toolkit-q-a.144271/post-921236
Sub setReferrerPolicy(s As String)
	sReferrerPolicy = s
End Sub

'set mode.no-cors to true/false
Sub setNoCors(b As Boolean)
	bNoCors = b
End Sub

Sub setBaseURL(s As String)
	baseURL = s
End Sub

'set basic authorization for this connection
Sub SetBasicAuthorization(username As String, password As String) As SDUIFetch
	Dim usernamepassword As Object = BANano.ToBase64($"${username}:${password}"$)
	Dim basic As String = $"Basic ${usernamepassword}"$
	AddHeader("Authorization", basic)
	Return Me
End Sub

'add a header value
Sub AddHeader(prop As String, value As String) As SDUIFetch
	headers.Put(prop, value)
	Return Me
End Sub

'add a parameter value
Sub AddParameters(mapOf As Map) As SDUIFetch
	For Each k As String In mapOf.Keys
		Dim v As Object = mapOf.Get(k)
		AddParameter(k, v)
	Next
	Return Me
End Sub

'set data from a map
Sub SetData(m As Map) As SDUIFetch
	For Each k As String In m.Keys
		Dim v As Object = m.Get(k)
		AddData(k, v)
	Next
	Return Me
End Sub

Sub AddParameter(k As String, v As String) As SDUIFetch
	parameters.Put(k, v)
	Return Me
End Sub

Sub AddData(k As String, v As String) As SDUIFetch
	data.Put(k, v)
	Return Me
End Sub

'<code>
''execute the post
'BANano.Await(J.PostWait)
'If j.Success Then
'	Dim Response As Map = j.response
'	If Response.ContainsKey("contents") Then
'		Dim minionTaal As String = SDUIShared.GetRecursive(Response, "contents.translated")
'		txtConvert.Value = minionTaal
'	End If
'Else	
'	txtConvert.Value = j.ErrorMessage
'End If
'</code>
Public Sub PostWait
	BANano.Await(fetchit("POST"))
End Sub

'set the url to process, this is set on initialize
'if there us a base start with /
Sub SetURL(url As String)
	baseURL = $"${obaseURL}${url}"$
End Sub

Private Sub fetchit(method As String)
	'reset some variables
	Success = False
	mError = ""
	OK = False
	Status = ""
	StatusText = ""
	'
	Dim xbaseURL As String = baseURL
	
	Response.Initialize
	Dim Error As String
  
	fetchOptions.Method = method
	If bNoCors Then
		fetchOptions.Mode = "no-cors"
	End If
	If headers.Size <> 0 Then
		fetchOptions.Headers = headers
	End If
	'
	If data.Size <> 0 Then
		Dim jsonData As String = BANano.ToJson(data)
		fetchOptions.Body = jsonData
	End If
	
	If parameters.Size <> 0 Then
		Dim sparameters As String = SDUIShared.URLQueryStringFromMap(parameters)
		xbaseURL = $"${baseURL}?${sparameters}"$
	End If
	'
	If sReferrerPolicy <> "" Then
		fetchOptions.ReferrerPolicy = sReferrerPolicy
	End If
	
	fetch.Initialize(xbaseURL, fetchOptions)
	Try
		' wait for the response
		fetchResponse = BANano.Await(fetch)
		Response = BANano.Await(fetchResponse.Json)
		OK = fetchResponse.OK
		Status = fetchResponse.Status
		StatusText = fetchResponse.StatusText
		Success = OK
	Catch(Error)
		mError = Error
	End Try
End Sub

'convert response to a list
Sub ToList As List
	Dim bo As Object = Response
	Return bo
End Sub

'convert response to a list
Sub ResponseList As List
	Dim bo As Object = Response
	Return bo
End Sub

public Sub ErrorMessage() As String
	Return mError
End Sub

'set the content type
Sub SetContentType(value As String) As SDUIFetch
	AddHeader("Content-Type", value)
	Return Me
End Sub

'set content type application json
Sub SetContentTypeApplicationJSON As SDUIFetch
	SetContentType("application/json; charset=UTF-8")
	Return Me
End Sub

Sub SetContentTypeApplication_X_WWW_Form_URLEncoded As SDUIFetch
	SetContentType("application/x-www-form-urlencoded")
	Return Me
End Sub

'<code>
''execute the post
'BANano.Await(J.GetWait)
'If j.Success Then
'	Dim Response As Map = j.response
'	If Response.ContainsKey("contents") Then
'		Dim minionTaal As String = SDUIShared.GetRecursive(Response, "contents.translated")
'		txtConvert.Value = minionTaal
'	End If
'Else	
'	txtConvert.Value = j.ErrorMessage
'End If
'</code>
Public Sub GetWait
	BANano.Await(fetchit("GET"))
End Sub

'<code>
''execute the post
'BANano.Await(J.PutWait)
'If j.Success Then
'	Dim Response As Map = j.response
'	If Response.ContainsKey("contents") Then
'		Dim minionTaal As String = SDUIShared.GetRecursive(Response, "contents.translated")
'		txtConvert.Value = minionTaal
'	End If
'Else	
'	txtConvert.Value = j.ErrorMessage
'End If
'</code>
Sub PutWait
	BANano.Await(fetchit("PUT"))
End Sub

Sub Patch
	BANano.Await(fetchit("PATCH"))
End Sub

'<code>
''execute the post
'BANano.Await(J.DeleteWait)
'If j.Success Then
'	Dim Response As Map = j.response
'	If Response.ContainsKey("contents") Then
'		Dim minionTaal As String = SDUIShared.GetRecursive(Response, "contents.translated")
'		txtConvert.Value = minionTaal
'	End If
'Else	
'	txtConvert.Value = j.ErrorMessage
'End If
'</code>
Sub DeleteWait
	BANano.Await(fetchit("DELETE"))
End Sub

'set access control allow origin
Sub SetAccessControlAllowOrigin(value As String) As SDUIFetch
	AddHeader("Access-Control-Allow-Origin", value)
	Return Me
End Sub
'set access control allow credentials
Sub SetAccessControlAllowCredentials(value As Boolean) As SDUIFetch
	AddHeader("Access-Control-Allow-Credentials", value)
	Return Me
End Sub
'set access control allow methods
Sub SetAccessControlAllowMethods(value As String) As SDUIFetch
	AddHeader("Access-Control-Allow-Methods", value)
	Return Me
End Sub
'set access control allow headers
Sub SetAccessControlAllowHeaders(value As String) As SDUIFetch
	AddHeader("Access-Control-Allow-Headers", value)
	Return Me
End Sub

'set origin
Sub SetOrigin(value As String) As SDUIFetch
	AddHeader("Origin", value)
	Return Me
End Sub