From c08d14a451a5714bcdabc4e79664587c5b7bf182 Mon Sep 17 00:00:00 2001 From: "mojo-machine[bot]" <111131124+mojo-machine[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 11:04:59 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=84=20Sync=20from=20monorepo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/wearemojo/mojo/commit/e913da3c2af5d6b8391a10446e34e9b9ec2ec2b2 --- lib/postmark/postmark.go | 71 ------------------------------------ lib/postmark/roundtripper.go | 29 --------------- 2 files changed, 100 deletions(-) delete mode 100644 lib/postmark/postmark.go delete mode 100644 lib/postmark/roundtripper.go diff --git a/lib/postmark/postmark.go b/lib/postmark/postmark.go deleted file mode 100644 index 92d31fa..0000000 --- a/lib/postmark/postmark.go +++ /dev/null @@ -1,71 +0,0 @@ -package postmark - -import ( - "context" - "fmt" - "time" - - "github.com/wearemojo/mojo-public-go/lib/cher" - "github.com/wearemojo/mojo-public-go/lib/gerrors" - "github.com/wearemojo/mojo-public-go/lib/httpclient" - "github.com/wearemojo/mojo-public-go/lib/jsonclient" - "github.com/wearemojo/mojo-public-go/lib/merr" - "github.com/wearemojo/mojo-public-go/lib/secret" -) - -const baseURL = "https://api.postmarkapp.com" - -//nolint:tagliatelle // postmark uses title case -type Response struct { - To string `json:"To"` - SubmittedAt time.Time `json:"SubmittedAt"` - MessageID string `json:"MessageID"` - ErrorCode int `json:"ErrorCode"` - Message string `json:"Message"` -} - -//nolint:tagliatelle // postmark uses title case -type EmailWithTemplate struct { - MessageStream string `json:"MessageStream"` - From string `json:"From"` - To string `json:"To"` - TemplateAlias string `json:"TemplateAlias"` - TemplateModel map[string]string `json:"TemplateModel"` -} - -type Client struct { - client *jsonclient.Client -} - -func NewClient(ctx context.Context, serverTokenSecretID string) (*Client, error) { - if _, err := secret.Get(ctx, serverTokenSecretID); err != nil { - return nil, err - } - - return &Client{ - client: jsonclient.NewClient( - baseURL, - httpclient.NewClient(5*time.Second, roundTripper{serverTokenSecretID}), - ), - }, nil -} - -func (c *Client) SendWithTemplate(ctx context.Context, req *EmailWithTemplate) (res *Response, err error) { - err = c.client.Do(ctx, "POST", "/email/withTemplate", nil, req, &res) - if cerr, ok := gerrors.As[cher.E](err); ok { - cerr.Code = fmt.Sprintf("postmark_%s", cerr.Code) - return nil, cerr - } else if err != nil { - return - } - - if res.ErrorCode != 0 { - err = merr.New(ctx, "postmark_error", merr.M{ - "error_code": res.ErrorCode, - "message": res.Message, - "message_id": res.MessageID, - }) - } - - return -} diff --git a/lib/postmark/roundtripper.go b/lib/postmark/roundtripper.go deleted file mode 100644 index f920fb8..0000000 --- a/lib/postmark/roundtripper.go +++ /dev/null @@ -1,29 +0,0 @@ -package postmark - -import ( - "net/http" - - "github.com/wearemojo/mojo-public-go/lib/secret" -) - -type roundTripper struct { - SecretID string -} - -func (r roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { - ctx := req.Context() - req = req.Clone(ctx) - - serverToken, err := secret.Get(ctx, r.SecretID) - if err != nil { - return nil, err - } - - if req.Header == nil { - req.Header = http.Header{} - } - - req.Header.Set("X-Postmark-Server-Token", serverToken) - - return http.DefaultTransport.RoundTrip(req) -}