From acf36559b665e0c586402b1b50117bfa1aa904c5 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 27 Feb 2024 20:05:03 +0000 Subject: [PATCH] refactor: split files in internal client package --- internal/client/accounts.go | 34 +++++++++++ internal/client/client.go | 111 ----------------------------------- internal/client/instnace.go | 21 +++++++ internal/client/statuses.go | 24 ++++++++ internal/client/timelines.go | 66 +++++++++++++++++++++ 5 files changed, 145 insertions(+), 111 deletions(-) create mode 100644 internal/client/accounts.go create mode 100644 internal/client/instnace.go create mode 100644 internal/client/statuses.go create mode 100644 internal/client/timelines.go diff --git a/internal/client/accounts.go b/internal/client/accounts.go new file mode 100644 index 0000000..6344fdf --- /dev/null +++ b/internal/client/accounts.go @@ -0,0 +1,34 @@ +package client + +import ( + "fmt" + "net/http" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/model" +) + +func (g *Client) VerifyCredentials() (model.Account, error) { + path := "/api/v1/accounts/verify_credentials" + url := g.Authentication.Instance + path + + var account model.Account + + 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 account, nil +} + +func (g *Client) GetAccount(accountURI string) (model.Account, error) { + path := "/api/v1/accounts/lookup?acct=" + accountURI + url := g.Authentication.Instance + path + + var account model.Account + + 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 +} diff --git a/internal/client/client.go b/internal/client/client.go index 9e0b411..6512178 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -11,7 +11,6 @@ import ( "codeflow.dananglin.me.uk/apollo/enbas/internal" "codeflow.dananglin.me.uk/apollo/enbas/internal/config" - "codeflow.dananglin.me.uk/apollo/enbas/internal/model" ) type Client struct { @@ -57,116 +56,6 @@ func (g *Client) AuthCodeURL() string { ) } -func (g *Client) VerifyCredentials() (model.Account, error) { - path := "/api/v1/accounts/verify_credentials" - url := g.Authentication.Instance + path - - var account model.Account - - 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 account, nil -} - -func (g *Client) GetInstance() (model.InstanceV2, error) { - path := "/api/v2/instance" - url := g.Authentication.Instance + path - - var instance model.InstanceV2 - - 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 instance, nil -} - -func (g *Client) GetAccount(accountURI string) (model.Account, error) { - path := "/api/v1/accounts/lookup?acct=" + accountURI - url := g.Authentication.Instance + path - - var account model.Account - - 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) GetStatus(statusID string) (model.Status, error) { - path := "/api/v1/statuses/" + statusID - url := g.Authentication.Instance + path - - var status model.Status - - if err := g.sendRequest(http.MethodGet, url, nil, &status); err != nil { - return model.Status{}, fmt.Errorf("received an error after sending the request to get the status information; %w", err) - } - - return status, nil -} - -func (g *Client) GetHomeTimeline(limit int) (model.Timeline, error) { - path := fmt.Sprintf("/api/v1/timelines/home?limit=%d", limit) - - timeline := model.Timeline{ - Name: "HOME TIMELINE", - Statuses: nil, - } - - return g.getTimeline(path, timeline) -} - -func (g *Client) GetPublicTimeline(limit int) (model.Timeline, error) { - path := fmt.Sprintf("/api/v1/timelines/public?limit=%d", limit) - - timeline := model.Timeline{ - Name: "PUBLIC TIMELINE", - Statuses: nil, - } - - return g.getTimeline(path, timeline) -} - -func (g *Client) GetListTimeline(listID string, limit int) (model.Timeline, error) { - path := fmt.Sprintf("/api/v1/timelines/list/%s?limit=%d", listID, limit) - - timeline := model.Timeline{ - Name: "LIST: " + listID, - Statuses: nil, - } - - return g.getTimeline(path, timeline) -} - -func (g *Client) GetTagTimeline(tag string, limit int) (model.Timeline, error) { - path := fmt.Sprintf("/api/v1/timelines/tag/%s?limit=%d", tag, limit) - - timeline := model.Timeline{ - Name: "TAG: " + tag, - Statuses: nil, - } - - return g.getTimeline(path, timeline) -} - -func (g *Client) getTimeline(path string, timeline model.Timeline) (model.Timeline, error) { - url := g.Authentication.Instance + path - - var statuses []model.Status - - if err := g.sendRequest(http.MethodGet, url, nil, &statuses); err != nil { - return timeline, fmt.Errorf("received an error after sending the request to get the timeline; %w", err) - } - - timeline.Statuses = statuses - - return timeline, nil -} - func (g *Client) sendRequest(method string, url string, requestBody io.Reader, object any) error { ctx, cancel := context.WithTimeout(context.Background(), g.Timeout) defer cancel() diff --git a/internal/client/instnace.go b/internal/client/instnace.go new file mode 100644 index 0000000..f874d0c --- /dev/null +++ b/internal/client/instnace.go @@ -0,0 +1,21 @@ +package client + +import ( + "fmt" + "net/http" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/model" +) + +func (g *Client) GetInstance() (model.InstanceV2, error) { + path := "/api/v2/instance" + url := g.Authentication.Instance + path + + var instance model.InstanceV2 + + 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 instance, nil +} diff --git a/internal/client/statuses.go b/internal/client/statuses.go new file mode 100644 index 0000000..b6f3510 --- /dev/null +++ b/internal/client/statuses.go @@ -0,0 +1,24 @@ +package client + +import ( + "fmt" + "net/http" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/model" +) + +func (g *Client) GetStatus(statusID string) (model.Status, error) { + path := "/api/v1/statuses/" + statusID + url := g.Authentication.Instance + path + + var status model.Status + + if err := g.sendRequest(http.MethodGet, url, nil, &status); err != nil { + return model.Status{}, fmt.Errorf( + "received an error after sending the request to get the status information; %w", + err, + ) + } + + return status, nil +} diff --git a/internal/client/timelines.go b/internal/client/timelines.go new file mode 100644 index 0000000..9b24b5e --- /dev/null +++ b/internal/client/timelines.go @@ -0,0 +1,66 @@ +package client + +import ( + "fmt" + "net/http" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/model" +) + +func (g *Client) GetHomeTimeline(limit int) (model.Timeline, error) { + path := fmt.Sprintf("/api/v1/timelines/home?limit=%d", limit) + + timeline := model.Timeline{ + Name: "HOME TIMELINE", + Statuses: nil, + } + + return g.getTimeline(path, timeline) +} + +func (g *Client) GetPublicTimeline(limit int) (model.Timeline, error) { + path := fmt.Sprintf("/api/v1/timelines/public?limit=%d", limit) + + timeline := model.Timeline{ + Name: "PUBLIC TIMELINE", + Statuses: nil, + } + + return g.getTimeline(path, timeline) +} + +func (g *Client) GetListTimeline(listID string, limit int) (model.Timeline, error) { + path := fmt.Sprintf("/api/v1/timelines/list/%s?limit=%d", listID, limit) + + timeline := model.Timeline{ + Name: "LIST: " + listID, + Statuses: nil, + } + + return g.getTimeline(path, timeline) +} + +func (g *Client) GetTagTimeline(tag string, limit int) (model.Timeline, error) { + path := fmt.Sprintf("/api/v1/timelines/tag/%s?limit=%d", tag, limit) + + timeline := model.Timeline{ + Name: "TAG: " + tag, + Statuses: nil, + } + + return g.getTimeline(path, timeline) +} + +func (g *Client) getTimeline(path string, timeline model.Timeline) (model.Timeline, error) { + url := g.Authentication.Instance + path + + var statuses []model.Status + + if err := g.sendRequest(http.MethodGet, url, nil, &statuses); err != nil { + return timeline, fmt.Errorf("received an error after sending the request to get the timeline; %w", err) + } + + timeline.Statuses = statuses + + return timeline, nil +}