2024-02-20 14:09:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RegisterRequest struct {
|
|
|
|
ClientName string `json:"client_name"`
|
|
|
|
RedirectUris string `json:"redirect_uris"`
|
|
|
|
Scopes string `json:"scopes"`
|
|
|
|
Website string `json:"website"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RegisterResponse struct {
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
ClientSecret string `json:"client_secret"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
RedirectUri string `json:"redirect_uri"`
|
|
|
|
VapidKey string `json:"vapid_key"`
|
|
|
|
Website string `json:"website"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *gtsClient) register() error {
|
|
|
|
params := RegisterRequest{
|
|
|
|
ClientName: applicationName,
|
|
|
|
RedirectUris: redirectUri,
|
|
|
|
Scopes: "read write",
|
|
|
|
Website: applicationWebsite,
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshal the request body; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
|
|
|
|
|
|
|
path := "/api/v1/apps"
|
|
|
|
url := g.authentication.Instance + path
|
|
|
|
|
2024-02-20 17:54:23 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), g.timeout)
|
2024-02-20 14:09:26 +00:00
|
|
|
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 registerResponse RegisterResponse
|
|
|
|
|
|
|
|
if err := g.sendRequest(request, ®isterResponse); err != nil {
|
|
|
|
return fmt.Errorf("received an error after sending the registration request; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.authentication.ClientID = registerResponse.ClientID
|
|
|
|
g.authentication.ClientSecret = registerResponse.ClientSecret
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|