2024-06-02 07:53:13 +01:00
|
|
|
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2024-02-23 09:44:57 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal"
|
|
|
|
"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-02-25 06:41:13 +00:00
|
|
|
params := registerRequest{
|
2024-02-23 09:44:57 +00:00
|
|
|
ClientName: internal.ApplicationName,
|
|
|
|
RedirectUris: internal.RedirectUri,
|
|
|
|
Scopes: "read write",
|
|
|
|
Website: internal.ApplicationWebsite,
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(params)
|
|
|
|
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
|
|
|
|
|
|
|
|
if err := g.sendRequest(http.MethodPost, url, requestBody, &app); 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
|
|
|
|
}
|