2024-02-23 09:44:57 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2024-02-25 06:41:13 +00:00
|
|
|
"net/url"
|
2024-06-22 01:16:24 +01:00
|
|
|
"os"
|
2024-02-23 09:44:57 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
|
|
|
)
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
const (
|
|
|
|
applicationJSON string = "application/json; charset=utf-8"
|
2024-08-23 02:35:30 +01:00
|
|
|
redirectURI string = "urn:ietf:wg:oauth:2.0:oob"
|
2024-08-23 02:40:13 +01:00
|
|
|
userAgent string = "Enbas/0.2.0"
|
2024-08-02 08:15:15 +01:00
|
|
|
)
|
|
|
|
|
2024-02-23 09:44:57 +00:00
|
|
|
type Client struct {
|
2024-02-27 09:31:17 +00:00
|
|
|
Authentication config.Credentials
|
2024-02-23 09:44:57 +00:00
|
|
|
HTTPClient http.Client
|
|
|
|
UserAgent string
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
func NewClientFromFile(path string) (*Client, error) {
|
|
|
|
config, err := config.NewCredentialsConfigFromFile(path)
|
2024-02-23 09:44:57 +00:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return nil, fmt.Errorf("unable to get the authentication configuration: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 09:31:17 +00:00
|
|
|
currentAuthentication := config.Credentials[config.CurrentAccount]
|
2024-02-23 09:44:57 +00:00
|
|
|
|
|
|
|
return NewClient(currentAuthentication), nil
|
|
|
|
}
|
|
|
|
|
2024-02-27 09:31:17 +00:00
|
|
|
func NewClient(authentication config.Credentials) *Client {
|
2024-02-23 09:44:57 +00:00
|
|
|
httpClient := http.Client{}
|
|
|
|
|
2024-02-24 15:53:29 +00:00
|
|
|
gtsClient := Client{
|
2024-02-23 09:44:57 +00:00
|
|
|
Authentication: authentication,
|
|
|
|
HTTPClient: httpClient,
|
2024-08-23 02:35:30 +01:00
|
|
|
UserAgent: userAgent,
|
2024-02-23 09:44:57 +00:00
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
}
|
|
|
|
|
2024-02-24 15:53:29 +00:00
|
|
|
return >sClient
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 06:41:13 +00:00
|
|
|
func (g *Client) AuthCodeURL() string {
|
|
|
|
format := "%s/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code"
|
2024-08-23 02:35:30 +01:00
|
|
|
escapedRedirectURI := url.QueryEscape(redirectURI)
|
2024-02-25 06:41:13 +00:00
|
|
|
|
|
|
|
return fmt.Sprintf(
|
|
|
|
format,
|
|
|
|
g.Authentication.Instance,
|
|
|
|
g.Authentication.ClientID,
|
|
|
|
escapedRedirectURI,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-22 01:16:24 +01:00
|
|
|
func (g *Client) DownloadMedia(url, path string) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create the HTTP request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Set("User-Agent", g.UserAgent)
|
|
|
|
|
|
|
|
response, err := g.HTTPClient.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("received an error after attempting the download: %w", err)
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
2024-08-17 11:23:21 +01:00
|
|
|
return BadStatusCodeError{
|
|
|
|
statusCode: response.StatusCode,
|
|
|
|
}
|
2024-06-22 01:16:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create %s: %w", path, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if _, err = io.Copy(file, response.Body); err != nil {
|
|
|
|
return fmt.Errorf("unable to save the download to %s: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
type requestParameters struct {
|
|
|
|
httpMethod string
|
|
|
|
url string
|
|
|
|
contentType string
|
|
|
|
requestBody io.Reader
|
|
|
|
output any
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) sendRequest(params requestParameters) error {
|
2024-02-23 09:44:57 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), g.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
request, err := http.NewRequestWithContext(ctx, params.httpMethod, params.url, params.requestBody)
|
2024-02-23 09:44:57 +00:00
|
|
|
if err != nil {
|
2024-06-22 01:16:24 +01:00
|
|
|
return fmt.Errorf("unable to create the HTTP request: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
if params.contentType != "" {
|
|
|
|
request.Header.Set("Content-Type", params.contentType)
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Set("Accept", applicationJSON)
|
2024-02-23 09:44:57 +00:00
|
|
|
request.Header.Set("User-Agent", g.UserAgent)
|
|
|
|
|
|
|
|
if len(g.Authentication.AccessToken) > 0 {
|
2024-05-22 23:30:09 +01:00
|
|
|
request.Header.Set("Authorization", "Bearer "+g.Authentication.AccessToken)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err := g.HTTPClient.Do(request)
|
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("received an error after sending the request: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusBadRequest {
|
2024-07-04 11:27:00 +01:00
|
|
|
message := struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}{
|
|
|
|
Error: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(response.Body).Decode(&message); err != nil {
|
|
|
|
return ResponseError{
|
|
|
|
StatusCode: response.StatusCode,
|
|
|
|
Message: "",
|
|
|
|
MessageDecodeErr: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResponseError{
|
|
|
|
StatusCode: response.StatusCode,
|
|
|
|
Message: message.Error,
|
|
|
|
MessageDecodeErr: nil,
|
|
|
|
}
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
if params.output == nil {
|
2024-02-27 19:52:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
if err := json.NewDecoder(response.Body).Decode(params.output); err != nil {
|
2024-02-23 09:44:57 +00:00
|
|
|
return fmt.Errorf(
|
2024-06-02 11:35:43 +01:00
|
|
|
"unable to decode the response from the GoToSocial server: %w",
|
2024-02-23 09:44:57 +00:00
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|