|
| 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