From 2ccfdc433635821951cb681fc4baedb8e5c5c82a Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Fri, 2 Aug 2024 08:15:15 +0100 Subject: [PATCH] refactor: update client package - add a type for the parameters of the sendRequest method - contentType is now a parameter for the sendRequest method. --- internal/client/accounts.go | 180 +++++++++++++++++++++++++++++---- internal/client/client.go | 27 +++-- internal/client/instance.go | 10 +- internal/client/lists.go | 87 ++++++++++++++-- internal/client/media.go | 26 ++++- internal/client/poll.go | 20 +++- internal/client/preferences.go | 15 ++- internal/client/register.go | 14 ++- internal/client/statuses.go | 100 ++++++++++++++++-- internal/client/timelines.go | 10 +- internal/client/token.go | 14 ++- 11 files changed, 447 insertions(+), 56 deletions(-) diff --git a/internal/client/accounts.go b/internal/client/accounts.go index b63dc16..7436d9e 100644 --- a/internal/client/accounts.go +++ b/internal/client/accounts.go @@ -19,7 +19,15 @@ func (g *Client) VerifyCredentials() (model.Account, error) { var account model.Account - if err := g.sendRequest(http.MethodGet, url, nil, &account); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &account, + } + + if err := g.sendRequest(params); err != nil { return model.Account{}, fmt.Errorf("received an error after sending the request to verify the credentials: %w", err) } @@ -31,7 +39,15 @@ func (g *Client) GetAccount(accountURI string) (model.Account, error) { var account model.Account - if err := g.sendRequest(http.MethodGet, url, nil, &account); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &account, + } + + if err := g.sendRequest(params); err != nil { return model.Account{}, fmt.Errorf("received an error after sending the request to get the account information: %w", err) } @@ -43,7 +59,15 @@ func (g *Client) GetAccountRelationship(accountID string) (*model.AccountRelatio var relationships []model.AccountRelationship - if err := g.sendRequest(http.MethodGet, url, nil, &relationships); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &relationships, + } + + if err := g.sendRequest(params); err != nil { return nil, fmt.Errorf( "received an error after sending the request to get the account relationship: %w", err, @@ -75,7 +99,15 @@ func (g *Client) FollowAccount(form FollowAccountForm) error { requestBody := bytes.NewBuffer(data) url := g.Authentication.Instance + baseAccountsPath + "/" + form.AccountID + "/follow" - if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf("received an error after sending the follow request: %w", err) } @@ -85,7 +117,15 @@ func (g *Client) FollowAccount(form FollowAccountForm) error { func (g *Client) UnfollowAccount(accountID string) error { url := g.Authentication.Instance + baseAccountsPath + "/" + accountID + "/unfollow" - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf("received an error after sending the request to unfollow the account: %w", err) } @@ -97,7 +137,15 @@ func (g *Client) GetFollowers(accountID string, limit int) (model.AccountList, e accounts := make([]model.Account, limit) - if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &accounts, + } + + if err := g.sendRequest(params); err != nil { return model.AccountList{}, fmt.Errorf("received an error after sending the request to get the list of followers: %w", err) } @@ -114,7 +162,15 @@ func (g *Client) GetFollowing(accountID string, limit int) (model.AccountList, e accounts := make([]model.Account, limit) - if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &accounts, + } + + if err := g.sendRequest(params); err != nil { return model.AccountList{}, fmt.Errorf("received an error after sending the request to get the list of followed accounts: %w", err) } @@ -129,7 +185,15 @@ func (g *Client) GetFollowing(accountID string, limit int) (model.AccountList, e func (g *Client) BlockAccount(accountID string) error { url := g.Authentication.Instance + baseAccountsPath + "/" + accountID + "/block" - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf("received an error after sending the request to block the account: %w", err) } @@ -139,7 +203,15 @@ func (g *Client) BlockAccount(accountID string) error { func (g *Client) UnblockAccount(accountID string) error { url := g.Authentication.Instance + baseAccountsPath + "/" + accountID + "/unblock" - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf("received an error after sending the request to unblock the account: %w", err) } @@ -151,7 +223,15 @@ func (g *Client) GetBlockedAccounts(limit int) (model.AccountList, error) { var accounts []model.Account - if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &accounts, + } + + if err := g.sendRequest(params); err != nil { return model.AccountList{}, fmt.Errorf("received an error after sending the request to get the list of blocked accounts: %w", err) } @@ -178,7 +258,15 @@ func (g *Client) SetPrivateNote(accountID, note string) error { requestBody := bytes.NewBuffer(data) url := g.Authentication.Instance + baseAccountsPath + "/" + accountID + "/note" - if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf("received an error after sending the request to set the private note: %w", err) } @@ -190,7 +278,15 @@ func (g *Client) GetFollowRequests(limit int) (model.AccountList, error) { var accounts []model.Account - if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &accounts, + } + + if err := g.sendRequest(params); err != nil { return model.AccountList{}, fmt.Errorf("received an error after sending the request to get the list of follow requests: %w", err) } @@ -205,7 +301,15 @@ func (g *Client) GetFollowRequests(limit int) (model.AccountList, error) { func (g *Client) AcceptFollowRequest(accountID string) error { url := g.Authentication.Instance + baseFollowRequestsPath + "/" + accountID + "/authorize" - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf("received an error after sending the request to accept the follow request: %w", err) } @@ -215,7 +319,15 @@ func (g *Client) AcceptFollowRequest(accountID string) error { func (g *Client) RejectFollowRequest(accountID string) error { url := g.Authentication.Instance + baseFollowRequestsPath + "/" + accountID + "/reject" - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf("received an error after sending the request to reject the follow request: %w", err) } @@ -227,7 +339,15 @@ func (g *Client) GetMutedAccounts(limit int) (model.AccountList, error) { var accounts []model.Account - if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &accounts, + } + + if err := g.sendRequest(params); err != nil { return model.AccountList{}, fmt.Errorf("received an error after sending the request to get the list of muted accounts: %w", err) } @@ -253,7 +373,15 @@ func (g *Client) MuteAccount(accountID string, form MuteAccountForm) error { requestBody := bytes.NewBuffer(data) url := g.Authentication.Instance + baseAccountsPath + "/" + accountID + "/mute" - if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf("received an error after sending the request to mute the account: %w", err) } @@ -263,7 +391,15 @@ func (g *Client) MuteAccount(accountID string, form MuteAccountForm) error { func (g *Client) UnmuteAccount(accountID string) error { url := g.Authentication.Instance + baseAccountsPath + "/" + accountID + "/unmute" - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf("received an error after sending the request to unmute the account: %w", err) } @@ -295,7 +431,15 @@ func (g *Client) GetAccountStatuses(form GetAccountStatusesForm) (*model.StatusL var statuses []model.Status - if err := g.sendRequest(http.MethodGet, url, nil, &statuses); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &statuses, + } + + if err := g.sendRequest(params); err != nil { return nil, fmt.Errorf("received an error after sending the request to get the account's statuses: %w", err) } diff --git a/internal/client/client.go b/internal/client/client.go index a9ff5d1..b4ea7ff 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -14,6 +14,10 @@ import ( "codeflow.dananglin.me.uk/apollo/enbas/internal/config" ) +const ( + applicationJSON string = "application/json; charset=utf-8" +) + type Client struct { Authentication config.Credentials HTTPClient http.Client @@ -94,17 +98,28 @@ func (g *Client) DownloadMedia(url, path string) error { return nil } -func (g *Client) sendRequest(method string, url string, requestBody io.Reader, object any) error { +type requestParameters struct { + httpMethod string + url string + contentType string + requestBody io.Reader + output any +} + +func (g *Client) sendRequest(params requestParameters) error { ctx, cancel := context.WithTimeout(context.Background(), g.Timeout) defer cancel() - request, err := http.NewRequestWithContext(ctx, method, url, requestBody) + request, err := http.NewRequestWithContext(ctx, params.httpMethod, params.url, params.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") + if params.contentType != "" { + request.Header.Set("Content-Type", params.contentType) + } + + request.Header.Set("Accept", applicationJSON) request.Header.Set("User-Agent", g.UserAgent) if len(g.Authentication.AccessToken) > 0 { @@ -140,11 +155,11 @@ func (g *Client) sendRequest(method string, url string, requestBody io.Reader, o } } - if object == nil { + if params.output == nil { return nil } - if err := json.NewDecoder(response.Body).Decode(object); err != nil { + if err := json.NewDecoder(response.Body).Decode(params.output); err != nil { return fmt.Errorf( "unable to decode the response from the GoToSocial server: %w", err, diff --git a/internal/client/instance.go b/internal/client/instance.go index 4ea98f3..3a6dbf1 100644 --- a/internal/client/instance.go +++ b/internal/client/instance.go @@ -13,7 +13,15 @@ func (g *Client) GetInstance() (model.InstanceV2, error) { var instance model.InstanceV2 - if err := g.sendRequest(http.MethodGet, url, nil, &instance); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &instance, + } + + if err := g.sendRequest(params); err != nil { return model.InstanceV2{}, fmt.Errorf("received an error after sending the request to get the instance details: %w", err) } diff --git a/internal/client/lists.go b/internal/client/lists.go index d7ae52f..bd6730a 100644 --- a/internal/client/lists.go +++ b/internal/client/lists.go @@ -18,7 +18,15 @@ func (g *Client) GetAllLists() ([]model.List, error) { var lists []model.List - if err := g.sendRequest(http.MethodGet, url, nil, &lists); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &lists, + } + + if err := g.sendRequest(params); err != nil { return nil, fmt.Errorf( "received an error after sending the request to get the list of lists: %w", err, @@ -33,7 +41,15 @@ func (g *Client) GetList(listID string) (model.List, error) { var list model.List - if err := g.sendRequest(http.MethodGet, url, nil, &list); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &list, + } + + if err := g.sendRequest(params); err != nil { return model.List{}, fmt.Errorf( "received an error after sending the request to get the list: %w", err, @@ -59,7 +75,15 @@ func (g *Client) CreateList(form CreateListForm) (model.List, error) { var list model.List - if err := g.sendRequest(http.MethodPost, url, requestBody, &list); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: &list, + } + + if err := g.sendRequest(params); err != nil { return model.List{}, fmt.Errorf( "received an error after sending the request to create the list: %w", err, @@ -88,7 +112,15 @@ func (g *Client) UpdateList(listToUpdate model.List) (model.List, error) { var updatedList model.List - if err := g.sendRequest(http.MethodPut, url, requestBody, &updatedList); err != nil { + params := requestParameters{ + httpMethod: http.MethodPut, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: &updatedList, + } + + if err := g.sendRequest(params); err != nil { return model.List{}, fmt.Errorf( "received an error after sending the request to update the list: %w", err, @@ -101,7 +133,22 @@ func (g *Client) UpdateList(listToUpdate model.List) (model.List, error) { func (g *Client) DeleteList(listID string) error { url := g.Authentication.Instance + baseListPath + "/" + listID - return g.sendRequest(http.MethodDelete, url, nil, nil) + params := requestParameters{ + httpMethod: http.MethodDelete, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { + return fmt.Errorf( + "received an error after sending the request to delete the list: %w", + err, + ) + } + + return nil } func (g *Client) AddAccountsToList(listID string, accountIDs []string) error { @@ -119,7 +166,15 @@ func (g *Client) AddAccountsToList(listID string, accountIDs []string) error { requestBody := bytes.NewBuffer(data) url := g.Authentication.Instance + baseListPath + "/" + listID + "/accounts" - if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf( "received an error after sending the request to add the accounts to the list: %w", err, @@ -144,7 +199,15 @@ func (g *Client) RemoveAccountsFromList(listID string, accountIDs []string) erro requestBody := bytes.NewBuffer(data) url := g.Authentication.Instance + baseListPath + "/" + listID + "/accounts" - if err := g.sendRequest(http.MethodDelete, url, requestBody, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodDelete, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf( "received an error after sending the request to remove the accounts from the list: %w", err, @@ -160,7 +223,15 @@ func (g *Client) GetAccountsFromList(listID string, limit int) ([]model.Account, var accounts []model.Account - if err := g.sendRequest(http.MethodGet, url, nil, &accounts); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &accounts, + } + + if err := g.sendRequest(params); err != nil { return nil, fmt.Errorf( "received an error after sending the request to get the accounts from the list: %w", err, diff --git a/internal/client/media.go b/internal/client/media.go index 3be293d..328d814 100644 --- a/internal/client/media.go +++ b/internal/client/media.go @@ -7,14 +7,36 @@ import ( "codeflow.dananglin.me.uk/apollo/enbas/internal/model" ) +const ( + baseMediaPath string = "/api/v1/media" +) + func (g *Client) GetMediaAttachment(mediaAttachmentID string) (model.Attachment, error) { - url := g.Authentication.Instance + "/api/v1/media/" + mediaAttachmentID + url := g.Authentication.Instance + baseMediaPath + "/" + mediaAttachmentID var attachment model.Attachment - if err := g.sendRequest(http.MethodGet, url, nil, &attachment); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &attachment, + } + + if err := g.sendRequest(params); err != nil { return model.Attachment{}, fmt.Errorf("received an error after sending the request to get the media attachment: %w", err) } return attachment, nil } + +//type CreateMediaAttachmentForm struct { +// Description string +// Focus string +// Filepath string +//} +// +//func (g *Client) CreateMediaAttachment(form CreateMediaAttachmentForm) (model.Attachment, error) { +// return model.Attachment{}, nil +//} diff --git a/internal/client/poll.go b/internal/client/poll.go index dfaea22..5d6baa5 100644 --- a/internal/client/poll.go +++ b/internal/client/poll.go @@ -18,7 +18,15 @@ func (g *Client) GetPoll(pollID string) (model.Poll, error) { var poll model.Poll - if err := g.sendRequest(http.MethodGet, url, nil, &poll); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &poll, + } + + if err := g.sendRequest(params); err != nil { return model.Poll{}, fmt.Errorf( "received an error after sending the request to get the poll: %w", err, @@ -43,7 +51,15 @@ func (g *Client) VoteInPoll(pollID string, choices []int) error { requestBody := bytes.NewBuffer(data) url := g.Authentication.Instance + pollPath + "/" + pollID + "/votes" - if err := g.sendRequest(http.MethodPost, url, requestBody, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf("received an error after sending the request to vote in the poll: %w", err) } diff --git a/internal/client/preferences.go b/internal/client/preferences.go index 12eba60..8419443 100644 --- a/internal/client/preferences.go +++ b/internal/client/preferences.go @@ -12,8 +12,19 @@ func (g *Client) GetUserPreferences() (*model.Preferences, error) { var preferences model.Preferences - if err := g.sendRequest(http.MethodGet, url, nil, &preferences); err != nil { - return nil, fmt.Errorf("received an error after sending the request to get the user preferences: %w", err) + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &preferences, + } + + if err := g.sendRequest(params); err != nil { + return nil, fmt.Errorf( + "received an error after sending the request to get the user preferences: %w", + err, + ) } return &preferences, nil diff --git a/internal/client/register.go b/internal/client/register.go index d664787..8ccb396 100644 --- a/internal/client/register.go +++ b/internal/client/register.go @@ -18,14 +18,14 @@ type registerRequest struct { } func (g *Client) Register() error { - params := registerRequest{ + registerParams := registerRequest{ ClientName: internal.ApplicationName, RedirectUris: internal.RedirectURI, Scopes: "read write", Website: internal.ApplicationWebsite, } - data, err := json.Marshal(params) + data, err := json.Marshal(registerParams) if err != nil { return fmt.Errorf("unable to marshal the request body: %w", err) } @@ -35,7 +35,15 @@ func (g *Client) Register() error { var app model.Application - if err := g.sendRequest(http.MethodPost, url, requestBody, &app); err != nil { + requestParams := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: &app, + } + + if err := g.sendRequest(requestParams); err != nil { return fmt.Errorf("received an error after sending the registration request: %w", err) } diff --git a/internal/client/statuses.go b/internal/client/statuses.go index 0befba3..6e1a59e 100644 --- a/internal/client/statuses.go +++ b/internal/client/statuses.go @@ -19,7 +19,15 @@ func (g *Client) GetStatus(statusID string) (model.Status, error) { var status model.Status - if err := g.sendRequest(http.MethodGet, url, nil, &status); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &status, + } + + if err := g.sendRequest(params); err != nil { return model.Status{}, fmt.Errorf( "received an error after sending the request to get the status information: %w", err, @@ -62,7 +70,15 @@ func (g *Client) CreateStatus(form CreateStatusForm) (model.Status, error) { var status model.Status - if err := g.sendRequest(http.MethodPost, url, requestBody, &status); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: &status, + } + + if err := g.sendRequest(params); err != nil { return model.Status{}, fmt.Errorf( "received an error after sending the request to create the status: %w", err, @@ -81,7 +97,15 @@ func (g *Client) GetBookmarks(limit int) (model.StatusList, error) { Statuses: nil, } - if err := g.sendRequest(http.MethodGet, url, nil, &bookmarks.Statuses); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &bookmarks.Statuses, + } + + if err := g.sendRequest(params); err != nil { return bookmarks, fmt.Errorf( "received an error after sending the request to get the bookmarks: %w", err, @@ -95,7 +119,15 @@ func (g *Client) AddStatusToBookmarks(statusID string) error { path := fmt.Sprintf("/api/v1/statuses/%s/bookmark", statusID) url := g.Authentication.Instance + path - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf( "received an error after sending the request to add the status to the list of bookmarks: %w", err, @@ -109,7 +141,15 @@ func (g *Client) RemoveStatusFromBookmarks(statusID string) error { path := fmt.Sprintf("/api/v1/statuses/%s/unbookmark", statusID) url := g.Authentication.Instance + path - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf( "received an error after sending the request to remove the status from the list of bookmarks: %w", err, @@ -122,7 +162,15 @@ func (g *Client) RemoveStatusFromBookmarks(statusID string) error { func (g *Client) LikeStatus(statusID string) error { url := g.Authentication.Instance + baseStatusesPath + "/" + statusID + "/favourite" - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf( "received an error after sending the request to like the status: %w", err, @@ -135,7 +183,15 @@ func (g *Client) LikeStatus(statusID string) error { func (g *Client) UnlikeStatus(statusID string) error { url := g.Authentication.Instance + baseStatusesPath + "/" + statusID + "/unfavourite" - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf( "received an error after sending the request to unlike the status: %w", err, @@ -153,7 +209,15 @@ func (g *Client) GetLikedStatuses(limit int, resourceName string) (model.StatusL Statuses: nil, } - if err := g.sendRequest(http.MethodGet, url, nil, &liked.Statuses); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &liked.Statuses, + } + + if err := g.sendRequest(params); err != nil { return model.StatusList{}, fmt.Errorf( "received an error after sending the request to get the list of statuses: %w", err, @@ -166,7 +230,15 @@ func (g *Client) GetLikedStatuses(limit int, resourceName string) (model.StatusL func (g *Client) ReblogStatus(statusID string) error { url := g.Authentication.Instance + baseStatusesPath + "/" + statusID + "/reblog" - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf( "received an error after sending the request to reblog the status; %w", err, @@ -179,7 +251,15 @@ func (g *Client) ReblogStatus(statusID string) error { func (g *Client) UnreblogStatus(statusID string) error { url := g.Authentication.Instance + baseStatusesPath + "/" + statusID + "/unreblog" - if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: nil, + contentType: "", + output: nil, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf( "received an error after sending the request to un-reblog the status; %w", err, diff --git a/internal/client/timelines.go b/internal/client/timelines.go index 9d4a0f9..f5571d8 100644 --- a/internal/client/timelines.go +++ b/internal/client/timelines.go @@ -56,7 +56,15 @@ func (g *Client) getTimeline(path string, timeline model.StatusList) (model.Stat var statuses []model.Status - if err := g.sendRequest(http.MethodGet, url, nil, &statuses); err != nil { + params := requestParameters{ + httpMethod: http.MethodGet, + url: url, + requestBody: nil, + contentType: "", + output: &statuses, + } + + if err := g.sendRequest(params); err != nil { return timeline, fmt.Errorf("received an error after sending the request to get the timeline: %w", err) } diff --git a/internal/client/token.go b/internal/client/token.go index a9df66d..b2902f4 100644 --- a/internal/client/token.go +++ b/internal/client/token.go @@ -28,7 +28,7 @@ type tokenResponse struct { } func (g *Client) UpdateToken(code string) error { - params := tokenRequest{ + tokenReq := tokenRequest{ RedirectURI: internal.RedirectURI, ClientID: g.Authentication.ClientID, ClientSecret: g.Authentication.ClientSecret, @@ -36,7 +36,7 @@ func (g *Client) UpdateToken(code string) error { Code: code, } - data, err := json.Marshal(params) + data, err := json.Marshal(tokenReq) if err != nil { return fmt.Errorf("unable to marshal the request body: %w", err) } @@ -46,7 +46,15 @@ func (g *Client) UpdateToken(code string) error { var response tokenResponse - if err := g.sendRequest(http.MethodPost, url, requestBody, &response); err != nil { + params := requestParameters{ + httpMethod: http.MethodPost, + url: url, + requestBody: requestBody, + contentType: applicationJSON, + output: &response, + } + + if err := g.sendRequest(params); err != nil { return fmt.Errorf("received an error after sending the token request: %w", err) }