diff --git a/cmd/enbas/show.go b/cmd/enbas/show.go index a814ed6..87c6904 100644 --- a/cmd/enbas/show.go +++ b/cmd/enbas/show.go @@ -32,7 +32,7 @@ ACCOUNT ID: CREATED AT: %s - + STATS: Followers: %d Following: %d @@ -40,7 +40,7 @@ STATS: BIOGRAPHY: %s - + METADATA: %s ACCOUNT URL: diff --git a/internal/client/client.go b/internal/client/client.go index dd26194..672141c 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "time" @@ -47,17 +48,9 @@ func (g *Client) VerifyCredentials() (model.Account, error) { path := "/api/v1/accounts/verify_credentials" url := g.Authentication.Instance + path - ctx, cancel := context.WithTimeout(context.Background(), g.Timeout) - defer cancel() - - request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) - if err != nil { - return model.Account{}, fmt.Errorf("unable to create the HTTP request; %w", err) - } - var account model.Account - if err := g.sendRequest(request, &account); err != nil { + if err := g.sendRequest(http.MethodGet, url, nil, &account); err != nil { return model.Account{}, fmt.Errorf("received an error after sending the request to verify the credentials; %w", err) } @@ -68,17 +61,9 @@ func (g *Client) GetInstance() (model.InstanceV2, error) { path := "/api/v2/instance" url := g.Authentication.Instance + path - ctx, cancel := context.WithTimeout(context.Background(), g.Timeout) - defer cancel() - - request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) - if err != nil { - return model.InstanceV2{}, fmt.Errorf("unable to create the HTTP request, %w", err) - } - var instance model.InstanceV2 - if err := g.sendRequest(request, &instance); err != nil { + if err := g.sendRequest(http.MethodGet, url, nil, &instance); err != nil { return model.InstanceV2{}, fmt.Errorf("received an error after sending the request to get the instance details; %w", err) } @@ -89,24 +74,24 @@ func (g *Client) GetAccount(accountURI string) (model.Account, error) { path := "/api/v1/accounts/lookup" url := g.Authentication.Instance + path + "?acct=" + accountURI - ctx, cancel := context.WithTimeout(context.Background(), g.Timeout) - defer cancel() - - request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) - if err != nil { - return model.Account{}, fmt.Errorf("unable to create the HTTP request, %w", err) - } - var account model.Account - if err := g.sendRequest(request, &account); err != nil { + if err := g.sendRequest(http.MethodGet, url, nil, &account); err != nil { return model.Account{}, fmt.Errorf("received an error after sending the request to get the account information; %w", err) } return account, nil } -func (g *Client) sendRequest(request *http.Request, object any) error { +func (g *Client) sendRequest(method string, url string, requestBody io.Reader, object any) error { + ctx, cancel := context.WithTimeout(context.Background(), g.Timeout) + defer cancel() + + request, err := http.NewRequestWithContext(ctx, method, url, requestBody) + if err != nil { + return fmt.Errorf("unable to create the HTTP request, %w", err) + } + request.Header.Set("Content-Type", "application/json; charset=utf-8") request.Header.Set("Accept", "application/json; charset=utf-8") request.Header.Set("User-Agent", g.UserAgent) diff --git a/internal/client/register.go b/internal/client/register.go index 99dc14c..d2e36d4 100644 --- a/internal/client/register.go +++ b/internal/client/register.go @@ -2,7 +2,6 @@ package client import ( "bytes" - "context" "encoding/json" "fmt" "net/http" @@ -36,17 +35,9 @@ func (g *Client) Register() error { path := "/api/v1/apps" url := g.Authentication.Instance + path - ctx, cancel := context.WithTimeout(context.Background(), g.Timeout) - defer cancel() - - request, err := http.NewRequestWithContext(ctx, http.MethodPost, url, requestBody) - if err != nil { - return fmt.Errorf("unable to create the HTTP request; %w", err) - } - var app model.Application - if err := g.sendRequest(request, &app); err != nil { + if err := g.sendRequest(http.MethodPost, url, requestBody, &app); err != nil { return fmt.Errorf("received an error after sending the registration request; %w", err) }