Je me suis amusé à utiliser l'API de randomuser pour créer un exemple d'appel API en PB.
Code : Tout sélectionner
; Utilisation de l'api randomuser
; Code : Ar-S - 2025/03/25
; Utilisation des appels API en purebasic
; Descriptif de l'api : https://randomuser.me/documentation#howto
Global randomUserApi$ = "https://randomuser.me/api/"
Declare CatchImage_Net(Adr$)
Declare.s FetchData(url$)
Declare ParseJSONResponse(response$)
Procedure CatchImage_Net(Adr$)
Protected EXT.s, image
; By Ar-S
EXT.s = LCase(GetExtensionPart(Adr$))
Select EXT
Case "jpg","jpeg"
UseJPEGImageDecoder()
Case "png"
UsePNGImageDecoder()
EndSelect
*Buffer = ReceiveHTTPMemory(Adr$)
If *Buffer
Image = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer) )
FreeMemory(*Buffer)
ProcedureReturn Image
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure.s FetchData(url$)
Protected httpRequest, response$
httpRequest = HTTPRequest(#PB_HTTP_Get, url$)
If httpRequest
If HTTPInfo(httpRequest, #PB_HTTP_StatusCode) = "200"
response$ = HTTPInfo(httpRequest, #PB_HTTP_Response)
Else
Debug "Erreur code : " + HTTPInfo(httpRequest, #PB_HTTP_StatusCode)
EndIf
FinishHTTP(httpRequest)
Else
Debug "Erreur lors de la requête HTTP."
EndIf
ProcedureReturn response$
EndProcedure
Procedure ParseJSONResponse(response$)
Shared json, results, user, name, title$, firstName$, lastName$, gender$, email$, pictureUrl$, MonImage
If ParseJSON(0, response$)
json = JSONValue(0)
results = GetJSONMember(json, "results")
If JSONArraySize(results) > 0
user = GetJSONElement(results, 0)
gender$ = GetJSONString(GetJSONMember(user, "gender"))
name = GetJSONMember(user, "name")
title$ = GetJSONString(GetJSONMember(name, "title"))
; je francise le titre et le genre pour la forme..
If gender$ = "male" : gender$ = "Homme" : Else : gender$ = "Femme" : EndIf
Select title$
Case "Miss"
title$ = "Mademoiselle"
Case "Ms", "Mrs"
title$ = "Madame"
Case "Mr"
title$ = "Monsieur"
EndSelect
firstName$ = GetJSONString(GetJSONMember(name, "first"))
lastName$ = GetJSONString(GetJSONMember(name, "last"))
email$ = GetJSONString(GetJSONMember(user, "email"))
pictureUrl$ = GetJSONString(GetJSONMember(GetJSONMember(user, "picture"), "medium"))
MonImage = CatchImage_Net( pictureUrl$)
Else
Debug "Aucun utilisateur trouvé."
End
EndIf
Else
Debug "Erreur JSON."
EndIf
EndProcedure
;--------- PROG --------------
jsonResponse$ = FetchData(randomUserApi$)
If jsonResponse$ <> ""
ParseJSONResponse(jsonResponse$)
EndIf
; Création de la fenêtre
OpenWindow(0, 100, 100, 400, 200, "API vers RandomUser by Ar-S", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(1, 10, 10, 380, 25, title$ + " " + firstName$ + " " + lastName$)
StringGadget(2, 10, 40, 380, 25, "Genre: " + gender$)
StringGadget(3, 10, 70, 380, 25, "Email: " + email$)
ImageGadget(4, 10, 100, 240, 240, ImageID(MonImage))
ButtonGadget(5,150,100,120,40,"Un autre ! ")
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 5
jsonResponse$ = FetchData(randomUserApi$)
If jsonResponse$ <> ""
ParseJSONResponse(jsonResponse$)
SetGadgetText(1, title$ + " " + firstName$ + " " + lastName$)
SetGadgetText(2, gender$)
SetGadgetText(3, email$)
MonImage = CatchImage_Net( pictureUrl$)
SetGadgetState(4, ImageID(MonImage))
EndIf
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow