Skip to content

Commit 441d3d6

Browse files
authored
themes api (#4)
1 parent d612638 commit 441d3d6

3 files changed

Lines changed: 456 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ client := loops.NewClient("YOUR_API_KEY",
5959
- Email messages — `GetEmailMessage`, `UpdateEmailMessage`
6060
- Campaigns — `CreateCampaign`, `UpdateCampaign`, `GetCampaign`, `ListCampaigns`
6161
- Components — `GetComponent`, `ListComponents`
62+
- Themes — `GetTheme`, `ListThemes`
6263

6364
Full reference: [pkg.go.dev/github.com/loops-so/loops-go](https://pkg.go.dev/github.com/loops-so/loops-go).
6465

themes.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package loops
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
)
9+
10+
type ThemeStyles struct {
11+
BackgroundColor string `json:"backgroundColor"`
12+
BackgroundXPadding float64 `json:"backgroundXPadding"`
13+
BackgroundYPadding float64 `json:"backgroundYPadding"`
14+
BodyColor string `json:"bodyColor"`
15+
BodyXPadding float64 `json:"bodyXPadding"`
16+
BodyYPadding float64 `json:"bodyYPadding"`
17+
BodyFontFamily string `json:"bodyFontFamily"`
18+
BodyFontCategory string `json:"bodyFontCategory"`
19+
BorderColor string `json:"borderColor"`
20+
BorderWidth float64 `json:"borderWidth"`
21+
BorderRadius float64 `json:"borderRadius"`
22+
ButtonBodyColor string `json:"buttonBodyColor"`
23+
ButtonBodyXPadding float64 `json:"buttonBodyXPadding"`
24+
ButtonBodyYPadding float64 `json:"buttonBodyYPadding"`
25+
ButtonBorderColor string `json:"buttonBorderColor"`
26+
ButtonBorderWidth float64 `json:"buttonBorderWidth"`
27+
ButtonBorderRadius float64 `json:"buttonBorderRadius"`
28+
ButtonTextColor string `json:"buttonTextColor"`
29+
ButtonTextFormat float64 `json:"buttonTextFormat"`
30+
ButtonTextFontSize float64 `json:"buttonTextFontSize"`
31+
DividerColor string `json:"dividerColor"`
32+
DividerBorderWidth float64 `json:"dividerBorderWidth"`
33+
TextBaseColor string `json:"textBaseColor"`
34+
TextBaseFontSize float64 `json:"textBaseFontSize"`
35+
TextBaseLineHeight float64 `json:"textBaseLineHeight"`
36+
TextBaseLetterSpacing float64 `json:"textBaseLetterSpacing"`
37+
TextLinkColor string `json:"textLinkColor"`
38+
Heading1Color string `json:"heading1Color"`
39+
Heading1FontSize float64 `json:"heading1FontSize"`
40+
Heading1LineHeight float64 `json:"heading1LineHeight"`
41+
Heading1LetterSpacing float64 `json:"heading1LetterSpacing"`
42+
Heading2Color string `json:"heading2Color"`
43+
Heading2FontSize float64 `json:"heading2FontSize"`
44+
Heading2LineHeight float64 `json:"heading2LineHeight"`
45+
Heading2LetterSpacing float64 `json:"heading2LetterSpacing"`
46+
Heading3Color string `json:"heading3Color"`
47+
Heading3FontSize float64 `json:"heading3FontSize"`
48+
Heading3LineHeight float64 `json:"heading3LineHeight"`
49+
Heading3LetterSpacing float64 `json:"heading3LetterSpacing"`
50+
}
51+
52+
type Theme struct {
53+
ThemeID string `json:"themeId"`
54+
Name string `json:"name"`
55+
Styles ThemeStyles `json:"styles"`
56+
IsDefault bool `json:"isDefault"`
57+
CreatedAt string `json:"createdAt"`
58+
UpdatedAt string `json:"updatedAt"`
59+
}
60+
61+
func (c *Client) GetTheme(id string) (*Theme, error) {
62+
req, err := c.newRequest(http.MethodGet, "/themes/"+id, nil)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
resp, err := c.do(req)
68+
if err != nil {
69+
return nil, err
70+
}
71+
defer resp.Body.Close()
72+
73+
if resp.StatusCode != http.StatusOK {
74+
return nil, errorFromResponse(resp)
75+
}
76+
77+
var result Theme
78+
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
79+
return nil, fmt.Errorf("failed to decode response: %w", err)
80+
}
81+
82+
return &result, nil
83+
}
84+
85+
func (c *Client) ListThemes(params PaginationParams) ([]Theme, *Pagination, error) {
86+
q := url.Values{}
87+
if params.PerPage != "" {
88+
q.Set("perPage", params.PerPage)
89+
}
90+
if params.Cursor != "" {
91+
q.Set("cursor", params.Cursor)
92+
}
93+
94+
path := "/themes"
95+
if len(q) > 0 {
96+
path += "?" + q.Encode()
97+
}
98+
99+
req, err := c.newRequest(http.MethodGet, path, nil)
100+
if err != nil {
101+
return nil, nil, err
102+
}
103+
104+
resp, err := c.do(req)
105+
if err != nil {
106+
return nil, nil, err
107+
}
108+
defer resp.Body.Close()
109+
110+
if resp.StatusCode != http.StatusOK {
111+
return nil, nil, errorFromResponse(resp)
112+
}
113+
114+
var result struct {
115+
Pagination Pagination `json:"pagination"`
116+
Data []Theme `json:"data"`
117+
}
118+
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
119+
return nil, nil, fmt.Errorf("failed to decode response: %w", err)
120+
}
121+
122+
return result.Data, &result.Pagination, nil
123+
}

0 commit comments

Comments
 (0)