59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
|
package tiko
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
baseURL = "https://particuliers-tiko.fr/api/v3"
|
||
|
graphqlAPIURL = baseURL + "/graphql/"
|
||
|
)
|
||
|
|
||
|
type User struct {
|
||
|
ID int64 `json:"id"`
|
||
|
Properties []Property `json:"properties"`
|
||
|
}
|
||
|
|
||
|
type AuthResp struct {
|
||
|
LogIn struct {
|
||
|
User User `json:"user"`
|
||
|
Token string `json:"token"`
|
||
|
} `json:"logIn"`
|
||
|
}
|
||
|
|
||
|
type ApiResp[T any] struct {
|
||
|
Data T `json:"data"`
|
||
|
}
|
||
|
|
||
|
func (c *Client) Authenticate(username, password string) (*User, string, error) {
|
||
|
requestData := map[string]interface{}{
|
||
|
"operationName": "LogIn",
|
||
|
"variables": map[string]string{
|
||
|
"email": username,
|
||
|
"password": password,
|
||
|
},
|
||
|
"query": "mutation LogIn($email: String!, $password: String!, $langCode: String, $retainSession: Boolean) { logIn( input: { email: $email password: $password langCode: $langCode retainSession: $retainSession}) { user { id properties { id } } token }}",
|
||
|
}
|
||
|
|
||
|
// Convertir les données en JSON
|
||
|
requestDataJSON, err := json.Marshal(requestData)
|
||
|
if err != nil {
|
||
|
return nil, "", err
|
||
|
}
|
||
|
|
||
|
// Créer la requête HTTP
|
||
|
req, err := http.NewRequest(http.MethodPost, graphqlAPIURL, bytes.NewBuffer(requestDataJSON))
|
||
|
if err != nil {
|
||
|
return nil, "", err
|
||
|
}
|
||
|
|
||
|
resp, err := Do[ApiResp[AuthResp]](c, req)
|
||
|
if err != nil {
|
||
|
return nil, "", err
|
||
|
}
|
||
|
|
||
|
return &resp.Data.LogIn.User, resp.Data.LogIn.Token, nil
|
||
|
}
|