Move APIClient to voki

This commit is contained in:
Victor Lacasse-Beaudoin 2023-09-18 22:06:26 -04:00
parent 025f9d74ce
commit 6dff76d871

View file

@ -1,100 +1,43 @@
package data
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"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 {
Key string
Host string
Port int
Protocol string
Voki *voki.Voki
}
func NewApiClientFromViper() *ApiClient {
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(apiClientKey, apiClientHost, apiClientProtocol, apiClientPort)
return NewApiClient(client, apiClientKey, apiClientHost, apiClientProtocol, apiClientPort)
}
func NewApiClient(key, host, protocol string, port int) *ApiClient {
func NewApiClient(client *http.Client, key, host, protocol string, port int) *ApiClient {
return &ApiClient{
Key: key,
Host: host,
Port: port,
Protocol: protocol,
Voki: voki.New(client, host, key, port, 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,
)
// 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
}
// GetHealth allows checking for API server health
func (a *ApiClient) GetHealth() (string, error) {
var getHealthResponse responses.GetHealthResponse
response, err := a.Call(http.MethodGet, "/v5/health", nil, true)
err := a.Voki.Unmarshal(http.MethodGet, "/v5/health", nil, true, &getHealthResponse)
if err != nil {
return getHealthResponse.Message, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return getHealthResponse.Message, err
}
if err := json.Unmarshal(body, &getHealthResponse); 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")
}
@ -114,22 +57,11 @@ func (a *ApiClient) GetMembre(membreID string) (models.Membre, error) {
return getMembreResponse.Data.Membre, errors.New("Veuillez fournir un numéro étudiant à rechercher")
}
response, err := a.Call(http.MethodGet, fmt.Sprintf("/v5/membres/%s", membreID), nil, true)
err := a.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v5/membres/%s", membreID), nil, true, getMembreResponse)
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.Data.Membre == *new(models.Membre) {
return getMembreResponse.Data.Membre, fmt.Errorf("Ce numéro étudiant ne correspond à aucunE membre")
}