2023-05-25 22:07:53 -04:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.agecem.com/agecem/bottin/v4/models"
|
2023-06-02 02:43:37 -04:00
|
|
|
"github.com/spf13/viper"
|
2023-05-25 22:07:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type ApiClient struct {
|
|
|
|
Key string
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
Protocol string
|
|
|
|
}
|
|
|
|
|
2023-06-02 02:43:37 -04:00
|
|
|
func NewApiClientFromViper() *ApiClient {
|
|
|
|
apiClientKey := viper.GetString("web.api.key")
|
|
|
|
apiClientHost := viper.GetString("web.api.host")
|
|
|
|
apiClientProtocol := viper.GetString("web.api.protocol")
|
|
|
|
apiClientPort := viper.GetInt("web.api.port")
|
|
|
|
|
|
|
|
return NewApiClient(apiClientKey, apiClientHost, apiClientProtocol, apiClientPort)
|
|
|
|
}
|
|
|
|
|
2023-05-25 22:07:53 -04:00
|
|
|
func NewApiClient(key, host, protocol string, port int) *ApiClient {
|
|
|
|
return &ApiClient{
|
|
|
|
Key: key,
|
|
|
|
Host: host,
|
|
|
|
Port: port,
|
|
|
|
Protocol: protocol,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ApiClient) Call(method, route string, requestBody io.Reader, useKey bool) (*http.Response, error) {
|
|
|
|
var response *http.Response
|
|
|
|
|
|
|
|
endpoint := fmt.Sprintf("%s://%s:%d%s",
|
|
|
|
a.Protocol, a.Host, a.Port, route,
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
//TODO
|
|
|
|
log.Println("endpoint: ", endpoint)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Create client
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
// Create request
|
|
|
|
request, err := http.NewRequest(method, endpoint, requestBody)
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if useKey {
|
|
|
|
if a.Key == "" {
|
|
|
|
return response, fmt.Errorf("Call to API required a key but none was provided. See --help for instructions on providing an API key.")
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Key))
|
|
|
|
}
|
|
|
|
|
|
|
|
if requestBody != nil {
|
|
|
|
request.Header.Add("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch Request
|
|
|
|
response, err = client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2023-05-26 01:43:58 -04:00
|
|
|
// GetV4 allows checking for API v4 server health
|
|
|
|
func (a *ApiClient) GetV4() (string, error) {
|
|
|
|
var getV4Response struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := a.Call(http.MethodGet, "/v4", nil, true)
|
|
|
|
if err != nil {
|
|
|
|
return getV4Response.Message, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return getV4Response.Message, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(body, &getV4Response); err != nil {
|
|
|
|
return getV4Response.Message, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if getV4Response.Message == "" {
|
|
|
|
return getV4Response.Message, errors.New("Could not confirm that API server is up, no response message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return getV4Response.Message, nil
|
|
|
|
}
|
|
|
|
|
2023-05-25 22:07:53 -04:00
|
|
|
func (a *ApiClient) GetMembre(membreID string) (models.Membre, error) {
|
|
|
|
var getMembreResponse struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Data struct {
|
|
|
|
Membre models.Membre `json:"membre"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if membreID == "" {
|
|
|
|
return getMembreResponse.Data.Membre, errors.New("Veuillez fournir un numéro étudiant à rechercher")
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
/*
|
|
|
|
log.Println("ApiClient.GetMembre received membreID: ", membreID)
|
|
|
|
*/
|
|
|
|
|
|
|
|
response, err := a.Call(http.MethodGet, fmt.Sprintf("/v4/membres/%s", membreID), nil, true)
|
|
|
|
if err != nil {
|
|
|
|
return getMembreResponse.Data.Membre, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return getMembreResponse.Data.Membre, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(body, &getMembreResponse); err != nil {
|
|
|
|
return getMembreResponse.Data.Membre, err
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if getMembreResponse.Message != "Read successful" {
|
|
|
|
return getMembreResponse.Data.Membre, errors.New(getMembreResponse.Message)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
if getMembreResponse.Data.Membre == *new(models.Membre) {
|
|
|
|
return getMembreResponse.Data.Membre, fmt.Errorf("Ce numéro étudiant ne correspond à aucunE membre")
|
|
|
|
}
|
|
|
|
|
|
|
|
return getMembreResponse.Data.Membre, nil
|
|
|
|
}
|