151 lines
3.5 KiB
Go
151 lines
3.5 KiB
Go
// This file was autogenerated from a .proto file, DO NOT EDIT!
|
|
|
|
package lightning_pub
|
|
|
|
import (
|
|
"net/url"
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func doPostRequest(url string, body []byte, auth string) ([]byte, error) {
|
|
bodyReader := bytes.NewReader(body)
|
|
req, err := http.NewRequest(http.MethodPost, url, bodyReader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("authorization", auth)
|
|
return doRequest(req)
|
|
}
|
|
|
|
func doGetRequest(url string, auth string) ([]byte, error) {
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("authorization", auth)
|
|
return doRequest(req)
|
|
}
|
|
|
|
func doRequest(req *http.Request) ([]byte, error) {
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resBody, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return resBody, nil
|
|
}
|
|
|
|
type ClientParams struct {
|
|
BaseURL string
|
|
RetrieveGuestAuth func() (string, error)
|
|
}
|
|
type Client struct {
|
|
GetAdminConnectInfo func() (*AdminConnectInfoResponse, error)
|
|
GetServiceState func() (*ServiceStateResponse, error)
|
|
WizardConfig func(req ConfigRequest) error
|
|
WizardState func() (*StateResponse, error)
|
|
}
|
|
|
|
func NewClient(params ClientParams) *Client {
|
|
return &Client{
|
|
GetAdminConnectInfo: func() (*AdminConnectInfoResponse, error) {
|
|
auth, err := params.RetrieveGuestAuth()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
finalRoute := "/wizard/admin_connect_info"
|
|
resBody, err := doGetRequest(params.BaseURL+finalRoute, auth)
|
|
result := ResultError{}
|
|
err = json.Unmarshal(resBody, &result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if result.Status == "ERROR" {
|
|
return nil, fmt.Errorf(result.Reason)
|
|
}
|
|
res := AdminConnectInfoResponse{}
|
|
err = json.Unmarshal(resBody, &res)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
},
|
|
GetServiceState: func() (*ServiceStateResponse, error) {
|
|
auth, err := params.RetrieveGuestAuth()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
finalRoute := "/wizard/service_state"
|
|
resBody, err := doGetRequest(params.BaseURL+finalRoute, auth)
|
|
result := ResultError{}
|
|
err = json.Unmarshal(resBody, &result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if result.Status == "ERROR" {
|
|
return nil, fmt.Errorf(result.Reason)
|
|
}
|
|
res := ServiceStateResponse{}
|
|
err = json.Unmarshal(resBody, &res)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
},
|
|
WizardConfig: func(req ConfigRequest) error {
|
|
auth, err := params.RetrieveGuestAuth()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
finalRoute := "/wizard/config"
|
|
body, err := json.Marshal(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resBody, err := doPostRequest(params.BaseURL+finalRoute, body, auth)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result := ResultError{}
|
|
err = json.Unmarshal(resBody, &result)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if result.Status == "ERROR" {
|
|
return fmt.Errorf(result.Reason)
|
|
}
|
|
return nil
|
|
},
|
|
WizardState: func() (*StateResponse, error) {
|
|
auth, err := params.RetrieveGuestAuth()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
finalRoute := "/wizard/state"
|
|
resBody, err := doGetRequest(params.BaseURL+finalRoute, auth)
|
|
result := ResultError{}
|
|
err = json.Unmarshal(resBody, &result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if result.Status == "ERROR" {
|
|
return nil, fmt.Errorf(result.Reason)
|
|
}
|
|
res := StateResponse{}
|
|
err = json.Unmarshal(resBody, &res)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
},
|
|
}
|
|
}
|