|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + pkgerrors "github.com/pkg/errors" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + adminRequestTimeout = 5 * time.Second |
| 18 | +) |
| 19 | + |
| 20 | +type RegisterSubscriberRequest struct { |
| 21 | + Name string `json:"name"` |
| 22 | + Endpoint string `json:"endpoint"` |
| 23 | +} |
| 24 | + |
| 25 | +type RegisterSubscriberResponse struct { |
| 26 | + ID string `json:"id"` |
| 27 | +} |
| 28 | + |
| 29 | +type Client struct { |
| 30 | + httpClient *http.Client |
| 31 | + adminURL string |
| 32 | + grpcURL string |
| 33 | +} |
| 34 | + |
| 35 | +func New(ctx context.Context, adminURL, grpcURL string) (*Client, error) { |
| 36 | + c := &Client{ |
| 37 | + httpClient: &http.Client{Timeout: adminRequestTimeout}, |
| 38 | + adminURL: adminURL, |
| 39 | + grpcURL: grpcURL, |
| 40 | + } |
| 41 | + |
| 42 | + if !c.isHTTPReady(ctx) { |
| 43 | + return nil, fmt.Errorf("chip router admin endpoint is not reachable: %s", c.adminURL) |
| 44 | + } |
| 45 | + if !c.isTCPReady() { |
| 46 | + return nil, fmt.Errorf("chip router grpc endpoint is not reachable: %s", c.grpcURL) |
| 47 | + } |
| 48 | + return c, nil |
| 49 | +} |
| 50 | + |
| 51 | +func (c *Client) RegisterSubscriber(ctx context.Context, name, endpoint string) (string, error) { |
| 52 | + body, err := json.Marshal(RegisterSubscriberRequest{Name: name, Endpoint: endpoint}) |
| 53 | + if err != nil { |
| 54 | + return "", pkgerrors.Wrap(err, "marshal chip router register request") |
| 55 | + } |
| 56 | + |
| 57 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(c.adminURL, "/")+"/subscribers", bytes.NewReader(body)) |
| 58 | + if err != nil { |
| 59 | + return "", pkgerrors.Wrap(err, "create chip router register request") |
| 60 | + } |
| 61 | + req.Header.Set("Content-Type", "application/json") |
| 62 | + |
| 63 | + resp, err := c.httpClient.Do(req) |
| 64 | + if err != nil { |
| 65 | + return "", pkgerrors.Wrap(err, "perform chip router register request") |
| 66 | + } |
| 67 | + defer resp.Body.Close() |
| 68 | + |
| 69 | + if resp.StatusCode != http.StatusOK { |
| 70 | + return "", fmt.Errorf("chip router register request failed with status %s", resp.Status) |
| 71 | + } |
| 72 | + |
| 73 | + var out RegisterSubscriberResponse |
| 74 | + if err := json.NewDecoder(resp.Body).Decode(&out); err != nil { |
| 75 | + return "", pkgerrors.Wrap(err, "decode chip router register response") |
| 76 | + } |
| 77 | + if out.ID == "" { |
| 78 | + return "", pkgerrors.New("chip router register response missing subscriber id") |
| 79 | + } |
| 80 | + |
| 81 | + return out.ID, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (c *Client) UnregisterSubscriber(ctx context.Context, id string) error { |
| 85 | + if strings.TrimSpace(id) == "" { |
| 86 | + return nil |
| 87 | + } |
| 88 | + |
| 89 | + req, err := http.NewRequestWithContext(ctx, http.MethodDelete, strings.TrimRight(c.adminURL, "/")+"/subscribers/"+id, nil) |
| 90 | + if err != nil { |
| 91 | + return pkgerrors.Wrap(err, "create chip router unregister request") |
| 92 | + } |
| 93 | + |
| 94 | + resp, err := c.httpClient.Do(req) |
| 95 | + if err != nil { |
| 96 | + return pkgerrors.Wrap(err, "perform chip router unregister request") |
| 97 | + } |
| 98 | + defer resp.Body.Close() |
| 99 | + |
| 100 | + if resp.StatusCode != http.StatusNoContent { |
| 101 | + return fmt.Errorf("chip router unregister request failed with status %s", resp.Status) |
| 102 | + } |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func (c *Client) isHTTPReady(ctx context.Context) bool { |
| 107 | + if strings.TrimSpace(c.adminURL) == "" { |
| 108 | + return false |
| 109 | + } |
| 110 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.TrimRight(c.adminURL, "/")+"/health", nil) |
| 111 | + if err != nil { |
| 112 | + return false |
| 113 | + } |
| 114 | + resp, err := c.httpClient.Do(req) |
| 115 | + if err != nil { |
| 116 | + return false |
| 117 | + } |
| 118 | + defer resp.Body.Close() |
| 119 | + if resp.StatusCode != http.StatusOK { |
| 120 | + return false |
| 121 | + } |
| 122 | + |
| 123 | + return true |
| 124 | +} |
| 125 | + |
| 126 | +func (c *Client) isTCPReady() bool { |
| 127 | + dialer := &net.Dialer{Timeout: adminRequestTimeout} |
| 128 | + conn, err := dialer.Dial("tcp", c.grpcURL) |
| 129 | + if err != nil { |
| 130 | + return false |
| 131 | + } |
| 132 | + _ = conn.Close() |
| 133 | + return true |
| 134 | +} |
0 commit comments