2024-02-23 09:44:57 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2024-08-23 02:35:30 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/info"
|
2024-02-23 09:44:57 +00:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
|
|
|
)
|
|
|
|
|
2024-02-25 06:41:13 +00:00
|
|
|
type registerRequest struct {
|
2024-02-23 09:44:57 +00:00
|
|
|
ClientName string `json:"client_name"`
|
|
|
|
RedirectUris string `json:"redirect_uris"`
|
|
|
|
Scopes string `json:"scopes"`
|
|
|
|
Website string `json:"website"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Client) Register() error {
|
2024-08-02 08:15:15 +01:00
|
|
|
registerParams := registerRequest{
|
2024-08-23 02:35:30 +01:00
|
|
|
ClientName: info.ApplicationName,
|
|
|
|
RedirectUris: redirectURI,
|
2024-02-23 09:44:57 +00:00
|
|
|
Scopes: "read write",
|
2024-08-23 02:35:30 +01:00
|
|
|
Website: info.ApplicationWebsite,
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
data, err := json.Marshal(registerParams)
|
2024-02-23 09:44:57 +00:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to marshal the request body: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
requestBody := bytes.NewBuffer(data)
|
2024-02-25 06:41:13 +00:00
|
|
|
url := g.Authentication.Instance + "/api/v1/apps"
|
2024-02-23 09:44:57 +00:00
|
|
|
|
|
|
|
var app model.Application
|
|
|
|
|
2024-08-02 08:15:15 +01:00
|
|
|
requestParams := requestParameters{
|
2024-08-02 13:04:40 +01:00
|
|
|
httpMethod: http.MethodPost,
|
|
|
|
url: url,
|
2024-08-02 08:15:15 +01:00
|
|
|
requestBody: requestBody,
|
|
|
|
contentType: applicationJSON,
|
2024-08-02 13:04:40 +01:00
|
|
|
output: &app,
|
2024-08-02 08:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.sendRequest(requestParams); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("received an error after sending the registration request: %w", err)
|
2024-02-23 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g.Authentication.ClientID = app.ClientID
|
|
|
|
g.Authentication.ClientSecret = app.ClientSecret
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|