Skip to content

Commit 7840f8b

Browse files
committed
Add CreateEmail
1 parent a3558bb commit 7840f8b

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

client.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package temp_mail_go
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"io"
@@ -43,7 +44,15 @@ func NewClient(apiKey string, client *http.Client) *Client {
4344
}
4445

4546
// newRequest creates a new HTTP request.
46-
func (c *Client) newRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
47+
func (c *Client) newRequest(ctx context.Context, method, path string, data interface{}) (*http.Request, error) {
48+
var body io.Reader
49+
if data != nil {
50+
b, err := json.Marshal(data)
51+
if err != nil {
52+
return nil, err
53+
}
54+
body = bytes.NewReader(b)
55+
}
4756
req, err := http.NewRequestWithContext(ctx, method, baseURL+path, body)
4857
if err != nil {
4958
return nil, err

create_email.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package temp_mail_go
2+
3+
import (
4+
"context"
5+
"time"
6+
)
7+
8+
const (
9+
// DomainTypePublic is a public domain.
10+
DomainTypePublic = "public"
11+
// DomainTypeCustom is user-provided domain.
12+
DomainTypeCustom = "custom"
13+
// DomainTypePremium is a premium domain.
14+
DomainTypePremium = "premium"
15+
)
16+
17+
// CreateEmailOptions represents the options to create an email.
18+
type CreateEmailOptions struct {
19+
// Email is the email address to create. If not provided, a random email address will be generated
20+
Email string
21+
// DomainType is the type of domain to use for the email address.
22+
// Possible values are: "public", "custom", "premium"
23+
DomainType string
24+
// Domain is the domain to use for the email address.
25+
Domain string
26+
}
27+
28+
// createEmailRequest represents the request to create an email
29+
type createEmailRequest struct {
30+
// Email is the email address to create. If not provided, a random email address will be generated
31+
Email string `json:"email"`
32+
// DomainType is the type of domain to use for the email address.
33+
// Possible values are: "public", "custom", "premium"
34+
DomainType string `json:"domain_type"`
35+
// Domain is the domain to use for the email address.
36+
Domain string `json:"domain"`
37+
}
38+
39+
// createEmailResponse represents the response to create an email.
40+
type createEmailResponse struct {
41+
// Email is the email address that was created.
42+
Email string `json:"email"`
43+
// TTL is the time to live of the email address in seconds.
44+
TTL int `json:"ttl"`
45+
}
46+
47+
type CreateEmailResponse struct {
48+
// Email is the email address that was created.
49+
Email string
50+
// TTL is the time to live of the email address in seconds.
51+
TTL time.Duration
52+
}
53+
54+
// CreateEmail creates an email address.
55+
// It returns the email address and the time to live of the email address.
56+
// You should use this method before getting messages for the email address.
57+
func (c *Client) CreateEmail(ctx context.Context, options CreateEmailOptions) (CreateEmailResponse, *Response, error) {
58+
req := createEmailRequest{
59+
Email: options.Email,
60+
DomainType: options.DomainType,
61+
Domain: options.Domain,
62+
}
63+
request, err := c.newRequest(ctx, "POST", "/v1/emails", req)
64+
if err != nil {
65+
return CreateEmailResponse{}, nil, err
66+
}
67+
68+
var resp createEmailResponse
69+
response, err := c.do(request, &resp)
70+
if err != nil {
71+
return CreateEmailResponse{}, nil, err
72+
}
73+
74+
return CreateEmailResponse{
75+
Email: resp.Email,
76+
TTL: time.Duration(resp.TTL) * time.Second,
77+
}, response, nil
78+
}

0 commit comments

Comments
 (0)