; Pocketbase support written from scratch

; Login

url.s = "https://pb.penguinpost.net/api/admins/auth-with-password"

; note this is a post with a small bit of json

;{
;    "identity": "yourloginemail",
;    "password": "yoursupersecretpassword"
;}

 If CreateJSON(0)
    login = SetJSONObject(JSONValue(0))
    SetJSONString(AddJSONMember(login, "identity"), "offal@pngpst.net")
    SetJSONString(AddJSONMember(login, "password"), "Perl1isgod!")
    Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
  EndIf
  
  NewMap Header$()
  Header$("Content-Type") = "application/json"
  Header$("User-Agent") = "Mozilla/5.0 (X11; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0"
  
  HttpRequest = HTTPRequest(#PB_HTTP_Post, url, ComposeJSON(0) , 0, Header$())
  If HttpRequest
    Debug "StatusCode: " + HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode)
    Debug "Response: " + HTTPInfo(HTTPRequest, #PB_HTTP_Response)
    
    ; Now we need to get the token
    
   ; CreateJSON(1)
    
    ;ParseJSON(1,HTTPInfo(HTTPRequest, #PB_HTTP_Response))    
    json$ = HTTPInfo(HTTPRequest, #PB_HTTP_Response) 
    
     If ParseJSON(1, json$) ; returns garbage without the PB ascii there not sure why
    ObjectValue = JSONValue(1)
    
    If ExamineJSONMembers(ObjectValue)
      While NextJSONMember(ObjectValue)
        
        Debug "Key: " + JSONMemberKey(ObjectValue)
      
        MemberValue = JSONMemberValue(ObjectValue)
                
        
        ; Now extract the actual value based on its type
        Select JSONType(MemberValue)
          Case #PB_JSON_String
            Debug "Value (String): " + GetJSONString(MemberValue)
         
          
          Case #PB_JSON_Number
            Debug "Value (Number): " + StrD(GetJSONDouble(MemberValue))
          
          
          Case #PB_JSON_Boolean
            Debug "Value (Boolean): " + Bool(GetJSONBoolean(MemberValue))
           
          
          Case #PB_JSON_Object
            Debug "Value (Object): (nested object)"
            ; You could further examine this nested object if needed.
           
            
          Case #PB_JSON_Array
            Debug "Value (Array): (nested array)"
            ; Handle array values if needed.
            
          
          Default
            Debug "Value (Unknown type)"
            
        EndSelect
     Wend
    EndIf

Else
  Debug JSONErrorMessage()
EndIf 
EndIf
; IDE Options = PureBasic 6.12 LTS (Linux - x64)
; CursorPosition = 43
; FirstLine = 33
; Folding = -
; EnableXP
; DPIAware