|
| 1 | +package customerio |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + IDTypeCIOID IDType = "cio_id" |
| 13 | + IDTypeUserID IDType = "id" |
| 14 | +) |
| 15 | + |
| 16 | +type IDType string |
| 17 | + |
| 18 | +type Customer struct { |
| 19 | + Identifiers `json:",inline"` |
| 20 | + Attributes `json:",inline"` |
| 21 | +} |
| 22 | + |
| 23 | +type Attributes struct { |
| 24 | + Phase1 string `json:"phase1,omitempty"` |
| 25 | + UserID string `json:"user_id"` |
| 26 | + |
| 27 | + OuraSizingKitDiscountCode string `json:"oura_sizing_kit_discount_code,omitempty"` |
| 28 | + OuraRingDiscountCode string `json:"oura_ring_discount_code,omitempty"` |
| 29 | + OuraParticipantID string `json:"oura_participant_id,omitempty"` |
| 30 | + |
| 31 | + Update bool `json:"_update,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +type customerResponse struct { |
| 35 | + Customer struct { |
| 36 | + ID string |
| 37 | + Identifiers Identifiers `json:"identifiers"` |
| 38 | + Attributes Attributes `json:"attributes"` |
| 39 | + } `json:"customer"` |
| 40 | +} |
| 41 | + |
| 42 | +type entityRequest struct { |
| 43 | + Type string `json:"type"` |
| 44 | + Identifiers map[string]string `json:"identifiers"` |
| 45 | + Action string `json:"action"` |
| 46 | + Attributes Attributes `json:"attributes,omitempty"` |
| 47 | +} |
| 48 | + |
| 49 | +type errorResponse struct { |
| 50 | + Errors []struct { |
| 51 | + Reason string `json:"reason,omitempty"` |
| 52 | + Field string `json:"field,omitempty"` |
| 53 | + Message string `json:"message,omitempty"` |
| 54 | + } `json:"errors,omitempty"` |
| 55 | +} |
| 56 | + |
| 57 | +func (c *Client) GetCustomer(ctx context.Context, cid string, typ IDType) (*Customer, error) { |
| 58 | + url := fmt.Sprintf("%s/v1/customers/%s/attributes", c.appAPIBaseURL, cid) |
| 59 | + |
| 60 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + // Add query parameter for id_type if using cio_id |
| 66 | + q := req.URL.Query() |
| 67 | + q.Add("id_type", string(typ)) |
| 68 | + req.URL.RawQuery = q.Encode() |
| 69 | + |
| 70 | + // Add authorization header |
| 71 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.appAPIKey)) |
| 72 | + req.Header.Set("Content-Type", "application/json") |
| 73 | + |
| 74 | + resp, err := http.DefaultClient.Do(req) |
| 75 | + if err != nil { |
| 76 | + return nil, fmt.Errorf("failed to execute request: %w", err) |
| 77 | + } |
| 78 | + defer resp.Body.Close() |
| 79 | + |
| 80 | + if resp.StatusCode == http.StatusNotFound { |
| 81 | + return nil, nil |
| 82 | + } else if resp.StatusCode != http.StatusOK { |
| 83 | + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) |
| 84 | + } |
| 85 | + |
| 86 | + var response customerResponse |
| 87 | + if err := json.NewDecoder(resp.Body).Decode(&response); err != nil { |
| 88 | + return nil, fmt.Errorf("failed to decode response: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + return &Customer{ |
| 92 | + Identifiers: response.Customer.Identifiers, |
| 93 | + Attributes: response.Customer.Attributes, |
| 94 | + }, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (c *Client) UpdateCustomer(ctx context.Context, customer Customer) error { |
| 98 | + url := fmt.Sprintf("%s/v2/entity", c.trackAPIBaseURL) |
| 99 | + |
| 100 | + // Prepare the request body |
| 101 | + reqBody := entityRequest{ |
| 102 | + Type: "person", |
| 103 | + Identifiers: map[string]string{"cio_id": customer.CID}, |
| 104 | + Action: "identify", |
| 105 | + Attributes: customer.Attributes, |
| 106 | + } |
| 107 | + reqBody.Attributes.Update = true |
| 108 | + |
| 109 | + jsonBody, err := json.Marshal(reqBody) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to marshal request body: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonBody)) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("failed to create request: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + // Add the authorization header (Basic Auth for Track API) |
| 120 | + req.SetBasicAuth(c.siteID, c.trackAPIKey) |
| 121 | + req.Header.Set("Content-Type", "application/json") |
| 122 | + |
| 123 | + resp, err := http.DefaultClient.Do(req) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("failed to execute request: %w", err) |
| 126 | + } |
| 127 | + defer resp.Body.Close() |
| 128 | + |
| 129 | + if resp.StatusCode != http.StatusOK { |
| 130 | + var errResp errorResponse |
| 131 | + if err := json.NewDecoder(resp.Body).Decode(&errResp); err == nil && len(errResp.Errors) > 0 { |
| 132 | + return fmt.Errorf("API error (status %d): %s", resp.StatusCode, errResp.Errors[0].Message) |
| 133 | + } |
| 134 | + return fmt.Errorf("unexpected status code: %d", resp.StatusCode) |
| 135 | + } |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
0 commit comments