Migrer client API à voki

This commit is contained in:
Victor Lacasse-Beaudoin 2023-09-18 18:34:53 -04:00
parent 1b046cc5e0
commit 78ea51e2c5
4 changed files with 14 additions and 79 deletions

View file

@ -2,92 +2,18 @@
package apiclient package apiclient
import ( import (
"encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
"codeberg.org/vlbeaudoin/voki"
"git.agecem.com/agecem/bottin-ag/apiresponse" "git.agecem.com/agecem/bottin-ag/apiresponse"
) )
type APIClient struct { type APIClient struct {
Key string Voki *voki.Voki
Host string
Port int
Protocol string
}
func New(key, host, protocol string, port int) *APIClient {
return &APIClient{
Key: key,
Host: host,
Port: port,
Protocol: protocol,
}
}
// Makes APIClient.Call() returning only an error. The Call data is put into the responder parameter's reference
func (a *APIClient) CallResponder(method, route string, requestBody io.Reader, useKey bool, responder apiresponse.Responder) error {
callResponse, err := a.Call(method, route, requestBody, useKey)
if err != nil {
return err
}
defer callResponse.Body.Close()
body, err := io.ReadAll(callResponse.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, responder)
if err != nil {
return err
}
return nil
}
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
} }
func (a *APIClient) Scan(membreID string) (response apiresponse.ScanPOST, err error) { func (a *APIClient) Scan(membreID string) (response apiresponse.ScanPOST, err error) {
//TODO implement api key //TODO implement api key
//err = a.CallResponder(http.MethodPost, "/v0/scan", &buf, true, &response) return response, a.Voki.Unmarshal(http.MethodPost, fmt.Sprintf("/v0/scan/%s", membreID), nil, false, &response)
err = a.CallResponder(http.MethodPost, fmt.Sprintf("/v0/scan/%s", membreID), nil, false, &response)
return
} }

View file

@ -8,6 +8,7 @@ import (
"log" "log"
"net/http" "net/http"
"codeberg.org/vlbeaudoin/voki"
"git.agecem.com/agecem/bottin-ag/apiclient" "git.agecem.com/agecem/bottin-ag/apiclient"
"git.agecem.com/agecem/bottin-ag/config" "git.agecem.com/agecem/bottin-ag/config"
"git.agecem.com/agecem/bottin-ag/webcontent" "git.agecem.com/agecem/bottin-ag/webcontent"
@ -33,7 +34,12 @@ var webCmd = &cobra.Command{
e.Pre(middleware.AddTrailingSlash()) e.Pre(middleware.AddTrailingSlash())
apiClient := apiclient.New(cfg.Web.API.Key, cfg.Web.API.Host, cfg.Web.API.Protocol, cfg.Web.API.Port) client := http.DefaultClient
defer client.CloseIdleConnections()
apiClient := &apiclient.APIClient{
Voki: voki.New(client, cfg.Web.API.Host, cfg.Web.API.Key, cfg.Web.API.Port, cfg.Web.API.Protocol),
}
handler := webhandler.New(apiClient) handler := webhandler.New(apiClient)

3
go.mod
View file

@ -3,7 +3,9 @@ module git.agecem.com/agecem/bottin-ag
go 1.21.0 go 1.21.0
require ( require (
codeberg.org/vlbeaudoin/voki v1.1.2
git.agecem.com/agecem/bottin/v5 v5.3.0 git.agecem.com/agecem/bottin/v5 v5.3.0
github.com/jmoiron/sqlx v1.3.5
github.com/labstack/echo/v4 v4.11.1 github.com/labstack/echo/v4 v4.11.1
github.com/spf13/cobra v1.7.0 github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0 github.com/spf13/viper v1.16.0
@ -15,7 +17,6 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgx v3.6.2+incompatible // indirect github.com/jackc/pgx v3.6.2+incompatible // indirect
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/labstack/gommon v0.4.0 // indirect github.com/labstack/gommon v0.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect

2
go.sum
View file

@ -35,6 +35,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
codeberg.org/vlbeaudoin/voki v1.1.2 h1:31xCFeJEGxEpe/o3q+l1np8DUbaWq+TVUf+JVyuUxEc=
codeberg.org/vlbeaudoin/voki v1.1.2/go.mod h1:5XTLx/KiW/OfiupF3o7PAAAU/UhsPdKSrVMmtHbmkPI=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
git.agecem.com/agecem/bottin/v5 v5.3.0 h1:7Fb0nJaGbWO2q//nTTZcVqLJoVSw0Ov0IoCs7/6ja+o= git.agecem.com/agecem/bottin/v5 v5.3.0 h1:7Fb0nJaGbWO2q//nTTZcVqLJoVSw0Ov0IoCs7/6ja+o=
git.agecem.com/agecem/bottin/v5 v5.3.0/go.mod h1:r4ZZB7P0XL4ZCatD99LHCS9fkpjl0UFCasalYPEW0Hw= git.agecem.com/agecem/bottin/v5 v5.3.0/go.mod h1:r4ZZB7P0XL4ZCatD99LHCS9fkpjl0UFCasalYPEW0Hw=