GofileScrapper/api/guest.go
2024-03-19 17:20:55 +01:00

56 lines
1.2 KiB
Go

package api
import (
"GofileScrapper/misc"
"GofileScrapper/structs"
"bytes"
"encoding/json"
"fmt"
)
func GetGuest() (structs.Guest, error) {
body, err := misc.Fetch(
"POST",
misc.API_URL+"/accounts",
bytes.NewBuffer([]byte(`{}`)),
[]misc.HeaderType{},
)
if err != nil {
return structs.Guest{}, fmt.Errorf("error reading response body: %v", err)
}
var preGuest structs.CreatedGuest
err = json.Unmarshal(body, &preGuest)
if err != nil {
return structs.Guest{}, fmt.Errorf("error unmarshalling JSON: %v", err)
}
if preGuest.Status != "ok" {
return structs.Guest{}, fmt.Errorf("unknown response from api: %v", string(body))
}
body, err = misc.Fetch(
"GET",
misc.API_URL+"/accounts/"+preGuest.Data.Id,
bytes.NewBuffer([]byte(`{}`)),
[]misc.HeaderType{
{Key: "Authorization", Value: fmt.Sprintf("Bearer %s", preGuest.Data.Token)},
},
)
if err != nil {
return structs.Guest{}, fmt.Errorf("error reading response body: %v", err)
}
var guest structs.Guest
err = json.Unmarshal(body, &guest)
if err != nil {
return structs.Guest{}, fmt.Errorf("error unmarshalling JSON: %v", err)
}
if preGuest.Status != "ok" {
return structs.Guest{}, fmt.Errorf("unknown response from api: %v", string(body))
}
return guest, nil
}