108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
|
|
package client
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"repository.promas.id/prana/omnix-sopiga/dto"
|
||
|
|
)
|
||
|
|
|
||
|
|
type SopigaClient struct {
|
||
|
|
baseURL string
|
||
|
|
token string
|
||
|
|
httpClient *http.Client
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewSopigaClient(baseURL, token string) *SopigaClient {
|
||
|
|
return &SopigaClient{
|
||
|
|
baseURL: baseURL,
|
||
|
|
token: token,
|
||
|
|
httpClient: &http.Client{
|
||
|
|
Timeout: 30 * time.Second,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *SopigaClient) AddRecipient(ctx context.Context, req dto.CollarAddRecipientRequest) (*dto.CollarAPIResponse, error) {
|
||
|
|
body, err := json.Marshal(req)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("marshal request: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
httpReq, err := http.NewRequestWithContext(
|
||
|
|
ctx,
|
||
|
|
http.MethodPost,
|
||
|
|
fmt.Sprintf("%s/api/client/collar/add-recipient", c.baseURL),
|
||
|
|
bytes.NewReader(body),
|
||
|
|
)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("create request: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
|
||
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
||
|
|
|
||
|
|
httpResp, err := c.httpClient.Do(httpReq)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("http request: %w", err)
|
||
|
|
}
|
||
|
|
defer httpResp.Body.Close()
|
||
|
|
|
||
|
|
bodyBytes, err := io.ReadAll(httpResp.Body)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("read response body: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
var resp dto.CollarAPIResponse
|
||
|
|
if err := json.Unmarshal(bodyBytes, &resp); err != nil {
|
||
|
|
return nil, fmt.Errorf("unmarshal response: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if httpResp.StatusCode != http.StatusAccepted && httpResp.StatusCode != http.StatusOK {
|
||
|
|
return nil, fmt.Errorf("http status %d: %s", httpResp.StatusCode, resp.Message)
|
||
|
|
}
|
||
|
|
|
||
|
|
return &resp, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *SopigaClient) GetRecipientDetail(ctx context.Context, recipientDetailID int64) (*dto.CollarRecipientDetailResponse, error) {
|
||
|
|
httpReq, err := http.NewRequestWithContext(
|
||
|
|
ctx,
|
||
|
|
http.MethodGet,
|
||
|
|
fmt.Sprintf("%s/api/client/collar/add-recipient/%d/detail", c.baseURL, recipientDetailID),
|
||
|
|
nil,
|
||
|
|
)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("create request: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
|
||
|
|
|
||
|
|
httpResp, err := c.httpClient.Do(httpReq)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("http request: %w", err)
|
||
|
|
}
|
||
|
|
defer httpResp.Body.Close()
|
||
|
|
|
||
|
|
bodyBytes, err := io.ReadAll(httpResp.Body)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("read response body: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
var resp dto.CollarRecipientDetailResponse
|
||
|
|
if err := json.Unmarshal(bodyBytes, &resp); err != nil {
|
||
|
|
return nil, fmt.Errorf("unmarshal response: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if httpResp.StatusCode != http.StatusOK {
|
||
|
|
return nil, fmt.Errorf("http status %d: %s", httpResp.StatusCode, resp.Message)
|
||
|
|
}
|
||
|
|
|
||
|
|
return &resp, nil
|
||
|
|
}
|