package data import ( "errors" "fmt" "net/http" "codeberg.org/vlbeaudoin/voki" "git.agecem.com/agecem/bottin/v5/models" "git.agecem.com/agecem/bottin/v5/responses" "github.com/spf13/viper" ) type ApiClient struct { Voki *voki.Voki } func NewApiClientFromViper(client *http.Client) *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(client, apiClientKey, apiClientHost, apiClientProtocol, apiClientPort) } func NewApiClient(client *http.Client, key, host, protocol string, port int) *ApiClient { return &ApiClient{ Voki: voki.New(client, host, key, port, protocol), } } // GetHealth allows checking for API server health func (a *ApiClient) GetHealth() (string, error) { var getHealthResponse responses.GetHealthResponse err := a.Voki.Unmarshal(http.MethodGet, "/v5/health", nil, true, &getHealthResponse) if err != nil { return getHealthResponse.Message, err } if getHealthResponse.Message == "" { return getHealthResponse.Message, errors.New("Could not confirm that API server is up, no response message") } return getHealthResponse.Message, nil } 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") } err := a.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v5/membres/%s", membreID), nil, true, &getMembreResponse) 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 }