refactor(client): reduced duplication
Create the http request in the sendRequest method to reduce duplication.
This commit is contained in:
parent
310ecb0b39
commit
0471ac817f
3 changed files with 16 additions and 40 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue