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/apiresponse/apiresponse.go b/apiresponse/apiresponse.go index 2114a9b..6f5afe5 100644 --- a/apiresponse/apiresponse.go +++ b/apiresponse/apiresponse.go @@ -1,24 +1,11 @@ // Package apiresponse provides response types for API routes package apiresponse -// Response defines the basic response types fields -type Response struct { - Error string - Message string - StatusCode int -} - -type Responder interface { - Respond() Responder -} - -func (r Response) Respond() Responder { - return r -} +import "codeberg.org/vlbeaudoin/voki/response" // HealthGET is the response type for `GET /v:version/health/ http/1.1` type HealthGET struct { - Response + response.ResponseWithError Data struct { BottinStatus string } @@ -26,7 +13,7 @@ type HealthGET struct { // ScanGET is the response type for `GET /v:version/scan/ http/1.1` type ScanGET struct { - Response + response.ResponseWithError Data struct { IsScanned bool } @@ -34,5 +21,5 @@ type ScanGET struct { // ScanPOST is the response type for `POST /v:version/scan/ http/1.1` type ScanPOST struct { - Response + response.ResponseWithError } 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..0d9f055 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.2.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/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..7419f20 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.2.0 h1:MpbZtKvSpkjnduSlNBVikn8OHQFwH9bEBBhUW9LQW34= +codeberg.org/vlbeaudoin/voki v1.2.0/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= diff --git a/webresponse/webresponse.go b/webresponse/webresponse.go index 8d7491a..b2672b2 100644 --- a/webresponse/webresponse.go +++ b/webresponse/webresponse.go @@ -1,11 +1,11 @@ package webresponse -import "git.agecem.com/agecem/bottin-ag/apiresponse" +import "codeberg.org/vlbeaudoin/voki/response" type IndexGET struct { - apiresponse.Response + response.ResponseWithError } type ScanPOST struct { - apiresponse.Response + response.ResponseWithError }