refactor(client): reduced duplication

Create the http request in the sendRequest method to reduce duplication.
This commit is contained in:
Dan Anglin 2024-02-22 12:15:55 +00:00
parent 310ecb0b39
commit 0471ac817f
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 16 additions and 40 deletions

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
"time" "time"
@ -47,17 +48,9 @@ func (g *Client) VerifyCredentials() (model.Account, error) {
path := "/api/v1/accounts/verify_credentials" path := "/api/v1/accounts/verify_credentials"
url := g.Authentication.Instance + path 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 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) 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" path := "/api/v2/instance"
url := g.Authentication.Instance + path 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 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) 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" path := "/api/v1/accounts/lookup"
url := g.Authentication.Instance + path + "?acct=" + accountURI 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 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 model.Account{}, fmt.Errorf("received an error after sending the request to get the account information; %w", err)
} }
return account, nil 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("Content-Type", "application/json; charset=utf-8")
request.Header.Set("Accept", "application/json; charset=utf-8") request.Header.Set("Accept", "application/json; charset=utf-8")
request.Header.Set("User-Agent", g.UserAgent) request.Header.Set("User-Agent", g.UserAgent)

View file

@ -2,7 +2,6 @@ package client
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@ -36,17 +35,9 @@ func (g *Client) Register() error {
path := "/api/v1/apps" path := "/api/v1/apps"
url := g.Authentication.Instance + path 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 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) return fmt.Errorf("received an error after sending the registration request; %w", err)
} }