2023-05-25 22:07:53 -04:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2023-09-18 22:06:26 -04:00
|
|
|
"codeberg.org/vlbeaudoin/voki"
|
2023-06-02 17:46:57 -04:00
|
|
|
"git.agecem.com/agecem/bottin/v5/models"
|
|
|
|
"git.agecem.com/agecem/bottin/v5/responses"
|
2023-06-02 02:43:37 -04:00
|
|
|
"github.com/spf13/viper"
|
2023-05-25 22:07:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type ApiClient struct {
|
2023-09-18 22:06:26 -04:00
|
|
|
Voki *voki.Voki
|
2023-05-25 22:07:53 -04:00
|
|
|
}
|
|
|
|
|
2023-09-18 22:06:26 -04:00
|
|
|
func NewApiClientFromViper(client *http.Client) *ApiClient {
|
2023-06-02 02:43:37 -04:00
|
|
|
apiClientKey := viper.GetString("web.api.key")
|
|
|
|
apiClientHost := viper.GetString("web.api.host")
|
|
|
|
apiClientProtocol := viper.GetString("web.api.protocol")
|
|
|
|
apiClientPort := viper.GetInt("web.api.port")
|
|
|
|
|
2023-09-18 22:06:26 -04:00
|
|
|
return NewApiClient(client, apiClientKey, apiClientHost, apiClientProtocol, apiClientPort)
|
2023-06-02 02:43:37 -04:00
|
|
|
}
|
|
|
|
|
2023-09-18 22:06:26 -04:00
|
|
|
func NewApiClient(client *http.Client, key, host, protocol string, port int) *ApiClient {
|
2023-05-25 22:07:53 -04:00
|
|
|
return &ApiClient{
|
2023-09-18 22:06:26 -04:00
|
|
|
Voki: voki.New(client, host, key, port, protocol),
|
2023-05-25 22:07:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 16:21:14 -04:00
|
|
|
// GetHealth allows checking for API server health
|
|
|
|
func (a *ApiClient) GetHealth() (string, error) {
|
2023-09-05 16:30:15 -04:00
|
|
|
var getHealthResponse responses.GetHealthResponse
|
2023-09-18 22:06:26 -04:00
|
|
|
err := a.Voki.Unmarshal(http.MethodGet, "/v5/health", nil, true, &getHealthResponse)
|
2023-05-26 01:43:58 -04:00
|
|
|
if err != nil {
|
2023-06-02 16:21:14 -04:00
|
|
|
return getHealthResponse.Message, err
|
2023-05-26 01:43:58 -04:00
|
|
|
}
|
|
|
|
|
2023-06-02 16:21:14 -04:00
|
|
|
if getHealthResponse.Message == "" {
|
|
|
|
return getHealthResponse.Message, errors.New("Could not confirm that API server is up, no response message")
|
2023-05-26 01:43:58 -04:00
|
|
|
}
|
|
|
|
|
2023-06-02 16:21:14 -04:00
|
|
|
return getHealthResponse.Message, nil
|
2023-05-26 01:43:58 -04:00
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-09-18 22:06:26 -04:00
|
|
|
err := a.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v5/membres/%s", membreID), nil, true, getMembreResponse)
|
2023-05-25 22:07:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return getMembreResponse.Data.Membre, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|