|
| 1 | +package loops |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | +) |
| 10 | + |
| 11 | +type Campaign struct { |
| 12 | + CampaignID string `json:"campaignId"` |
| 13 | + EmailMessageID *string `json:"emailMessageId"` |
| 14 | + Name string `json:"name"` |
| 15 | + Status string `json:"status"` |
| 16 | + CreatedAt string `json:"createdAt"` |
| 17 | + UpdatedAt string `json:"updatedAt"` |
| 18 | +} |
| 19 | + |
| 20 | +type CampaignListItem struct { |
| 21 | + CampaignID string `json:"campaignId"` |
| 22 | + EmailMessageID *string `json:"emailMessageId"` |
| 23 | + Name string `json:"name"` |
| 24 | + Subject string `json:"subject"` |
| 25 | + Status string `json:"status"` |
| 26 | + CreatedAt string `json:"createdAt"` |
| 27 | + UpdatedAt string `json:"updatedAt"` |
| 28 | +} |
| 29 | + |
| 30 | +type LmxWarning struct { |
| 31 | + Rule string `json:"rule"` |
| 32 | + Severity string `json:"severity"` |
| 33 | + Message string `json:"message"` |
| 34 | + Path string `json:"path,omitempty"` |
| 35 | +} |
| 36 | + |
| 37 | +type EmailMessageFields struct { |
| 38 | + Subject string `json:"subject,omitempty"` |
| 39 | + PreviewText string `json:"previewText,omitempty"` |
| 40 | + FromName string `json:"fromName,omitempty"` |
| 41 | + FromEmail string `json:"fromEmail,omitempty"` |
| 42 | + ReplyToEmail string `json:"replyToEmail,omitempty"` |
| 43 | + LMX string `json:"lmx,omitempty"` |
| 44 | +} |
| 45 | + |
| 46 | +type CreateCampaignRequest struct { |
| 47 | + Name string `json:"name"` |
| 48 | +} |
| 49 | + |
| 50 | +type UpdateCampaignRequest struct { |
| 51 | + Name string `json:"name"` |
| 52 | +} |
| 53 | + |
| 54 | +type CampaignCreateResponse struct { |
| 55 | + Campaign |
| 56 | + EmailMessageContentRevisionID *string `json:"emailMessageContentRevisionId"` |
| 57 | +} |
| 58 | + |
| 59 | +func (c *Client) CreateCampaign(req CreateCampaignRequest) (*CampaignCreateResponse, error) { |
| 60 | + b, err := json.Marshal(req) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("failed to encode request: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + httpReq, err := c.newRequest(http.MethodPost, "/campaigns", bytes.NewReader(b)) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + resp, err := c.do(httpReq) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + defer resp.Body.Close() |
| 75 | + |
| 76 | + if resp.StatusCode != http.StatusCreated { |
| 77 | + return nil, errorFromResponse(resp) |
| 78 | + } |
| 79 | + |
| 80 | + var result CampaignCreateResponse |
| 81 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 82 | + return nil, fmt.Errorf("failed to decode response: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + return &result, nil |
| 86 | +} |
| 87 | + |
| 88 | +func (c *Client) UpdateCampaign(id string, req UpdateCampaignRequest) (*Campaign, error) { |
| 89 | + b, err := json.Marshal(req) |
| 90 | + if err != nil { |
| 91 | + return nil, fmt.Errorf("failed to encode request: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + httpReq, err := c.newRequest(http.MethodPost, "/campaigns/"+id, bytes.NewReader(b)) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + resp, err := c.do(httpReq) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + defer resp.Body.Close() |
| 104 | + |
| 105 | + if resp.StatusCode != http.StatusOK { |
| 106 | + return nil, errorFromResponse(resp) |
| 107 | + } |
| 108 | + |
| 109 | + var result Campaign |
| 110 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 111 | + return nil, fmt.Errorf("failed to decode response: %w", err) |
| 112 | + } |
| 113 | + |
| 114 | + return &result, nil |
| 115 | +} |
| 116 | + |
| 117 | +func (c *Client) GetCampaign(id string) (*Campaign, error) { |
| 118 | + req, err := c.newRequest(http.MethodGet, "/campaigns/"+id, nil) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + |
| 123 | + resp, err := c.do(req) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + defer resp.Body.Close() |
| 128 | + |
| 129 | + if resp.StatusCode != http.StatusOK { |
| 130 | + return nil, errorFromResponse(resp) |
| 131 | + } |
| 132 | + |
| 133 | + var result Campaign |
| 134 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 135 | + return nil, fmt.Errorf("failed to decode response: %w", err) |
| 136 | + } |
| 137 | + |
| 138 | + return &result, nil |
| 139 | +} |
| 140 | + |
| 141 | +func (c *Client) ListCampaigns(params PaginationParams) ([]CampaignListItem, *Pagination, error) { |
| 142 | + q := url.Values{} |
| 143 | + if params.PerPage != "" { |
| 144 | + q.Set("perPage", params.PerPage) |
| 145 | + } |
| 146 | + if params.Cursor != "" { |
| 147 | + q.Set("cursor", params.Cursor) |
| 148 | + } |
| 149 | + |
| 150 | + path := "/campaigns" |
| 151 | + if len(q) > 0 { |
| 152 | + path += "?" + q.Encode() |
| 153 | + } |
| 154 | + |
| 155 | + req, err := c.newRequest(http.MethodGet, path, nil) |
| 156 | + if err != nil { |
| 157 | + return nil, nil, err |
| 158 | + } |
| 159 | + |
| 160 | + resp, err := c.do(req) |
| 161 | + if err != nil { |
| 162 | + return nil, nil, err |
| 163 | + } |
| 164 | + defer resp.Body.Close() |
| 165 | + |
| 166 | + if resp.StatusCode != http.StatusOK { |
| 167 | + return nil, nil, errorFromResponse(resp) |
| 168 | + } |
| 169 | + |
| 170 | + var result struct { |
| 171 | + Pagination Pagination `json:"pagination"` |
| 172 | + Data []CampaignListItem `json:"data"` |
| 173 | + } |
| 174 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 175 | + return nil, nil, fmt.Errorf("failed to decode response: %w", err) |
| 176 | + } |
| 177 | + |
| 178 | + return result.Data, &result.Pagination, nil |
| 179 | +} |
0 commit comments