You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.6 KiB
115 lines
2.6 KiB
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"git.wownero.com/onionltd/monero-multisig-broker/messagelog"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type Client struct {
|
|
host string
|
|
token string
|
|
offset int
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func (c *Client) SetToken(token string) {
|
|
c.token = token
|
|
}
|
|
|
|
func (c *Client) SetOffset(offset int) {
|
|
c.offset = offset
|
|
}
|
|
|
|
func (c *Client) PushMessage(ctx context.Context, topic string, message Message) error {
|
|
b, err := json.Marshal(convertToLogMessage(message))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
url := formatApiUri(c.host, topic)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewReader(b))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Authorization", formatAuthToken(c.token))
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("User-Agent", "monero-multisig-client/POC-0.1")
|
|
|
|
res, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusNoContent {
|
|
return fmt.Errorf("api: invalid response code: %d", res.StatusCode)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Client) ListMessages(ctx context.Context, topic string) ([]Message, error) {
|
|
url := formatApiUri(c.host, topic)
|
|
url = setOffset(url, c.offset)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Authorization", formatAuthToken(c.token))
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("User-Agent", "monero-multisig-client/POC-0.1")
|
|
|
|
res, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("api: invalid response code: %d", res.StatusCode)
|
|
}
|
|
|
|
messages := []messagelog.Message{}
|
|
if err := json.NewDecoder(res.Body).Decode(&messages); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
multisigMessages := make([]Message, 0, len(messages))
|
|
for i := range messages {
|
|
message, err := convertFromLogMessage(messages[i])
|
|
if err != nil {
|
|
continue
|
|
}
|
|
multisigMessages = append(multisigMessages, message)
|
|
}
|
|
|
|
return multisigMessages, nil
|
|
}
|
|
|
|
func formatApiUri(host, orderID string) string {
|
|
return fmt.Sprintf("%s/api/v1/multisig/%s", host, orderID)
|
|
}
|
|
|
|
func setOffset(url string, offset int) string {
|
|
return fmt.Sprintf("%s?offset=%d", url, offset)
|
|
}
|
|
|
|
func formatAuthToken(token string) string {
|
|
return fmt.Sprintf("Bearer %s", token)
|
|
}
|
|
|
|
func NewClient(host string, timeout time.Duration) *Client {
|
|
return &Client{
|
|
host: host,
|
|
httpClient: &http.Client{
|
|
Timeout: timeout,
|
|
},
|
|
}
|
|
}
|