From 78ea51e2c5e041d2471279c0f4a607646fe386a5 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 18 Sep 2023 18:34:53 -0400 Subject: [PATCH] =?UTF-8?q?Migrer=20client=20API=20=C3=A0=20voki?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apiclient/apiclient.go | 80 ++---------------------------------------- cmd/web.go | 8 ++++- go.mod | 3 +- go.sum | 2 ++ 4 files changed, 14 insertions(+), 79 deletions(-) diff --git a/apiclient/apiclient.go b/apiclient/apiclient.go index d68b7ad..95992b8 100644 --- a/apiclient/apiclient.go +++ b/apiclient/apiclient.go @@ -2,92 +2,18 @@ package apiclient import ( - "encoding/json" "fmt" - "io" "net/http" + "codeberg.org/vlbeaudoin/voki" "git.agecem.com/agecem/bottin-ag/apiresponse" ) type APIClient struct { - Key string - 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 + Voki *voki.Voki } func (a *APIClient) Scan(membreID string) (response apiresponse.ScanPOST, err error) { //TODO implement api key - //err = a.CallResponder(http.MethodPost, "/v0/scan", &buf, true, &response) - err = a.CallResponder(http.MethodPost, fmt.Sprintf("/v0/scan/%s", membreID), nil, false, &response) - - return + return response, a.Voki.Unmarshal(http.MethodPost, fmt.Sprintf("/v0/scan/%s", membreID), nil, false, &response) } diff --git a/cmd/web.go b/cmd/web.go index 7f580f0..0706774 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -8,6 +8,7 @@ import ( "log" "net/http" + "codeberg.org/vlbeaudoin/voki" "git.agecem.com/agecem/bottin-ag/apiclient" "git.agecem.com/agecem/bottin-ag/config" "git.agecem.com/agecem/bottin-ag/webcontent" @@ -33,7 +34,12 @@ var webCmd = &cobra.Command{ 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) diff --git a/go.mod b/go.mod index d9a5233..946e796 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,9 @@ module git.agecem.com/agecem/bottin-ag go 1.21.0 require ( + codeberg.org/vlbeaudoin/voki v1.1.2 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/spf13/cobra v1.7.0 github.com/spf13/viper v1.16.0 @@ -15,7 +17,6 @@ require ( github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // 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/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/go.sum b/go.sum index ee41114..6c9a2ed 100644 --- a/go.sum +++ b/go.sum @@ -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.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= 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= 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=