2024-02-21 10:57:37 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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 {
|
|
|
|
Authentication config.Authentication
|
|
|
|
HTTPClient http.Client
|
|
|
|
UserAgent string
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2024-02-21 13:20:32 +00:00
|
|
|
func NewClientFromConfig() (*Client, error) {
|
|
|
|
config, err := config.NewAuthenticationConfigFromFile()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to get the authentication configuration; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentAuthentication := config.Authentications[config.CurrentAccount]
|
|
|
|
|
|
|
|
return NewClient(currentAuthentication), nil
|
|
|
|
}
|
|
|
|
|
2024-02-21 10:57:37 +00:00
|
|
|
func NewClient(authentication config.Authentication) *Client {
|
|
|
|
httpClient := http.Client{}
|
|
|
|
|
|
|
|
client := Client{
|
|
|
|
Authentication: authentication,
|
|
|
|
HTTPClient: httpClient,
|
|
|
|
UserAgent: internal.UserAgent,
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &client
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return model.Account{}, fmt.Errorf("received an error after sending the request to verify the credentials; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2024-02-21 13:20:32 +00:00
|
|
|
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 {
|
|
|
|
return model.InstanceV2{}, fmt.Errorf("received an error after sending the request to get the instance details; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance, nil
|
|
|
|
}
|
|
|
|
|
2024-02-21 10:57:37 +00:00
|
|
|
func (g *Client) sendRequest(request *http.Request, object any) error {
|
|
|
|
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)
|
|
|
|
|
|
|
|
if len(g.Authentication.AccessToken) > 0 {
|
|
|
|
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", g.Authentication.AccessToken))
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := g.HTTPClient.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("received an error after sending the request; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusBadRequest {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"did not receive an OK response from the GoToSocial server; got %d",
|
|
|
|
response.StatusCode,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(response.Body).Decode(object); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"unable to decode the response from the GoToSocial server; %w",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|