File: email.go

package info (click to toggle)
golang-github-packethost-packngo 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 440 kB
  • sloc: makefile: 2
file content (87 lines) | stat: -rw-r--r-- 2,230 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package packngo

import "fmt"

const emailBasePath = "/emails"

// EmailRequest type used to add an email address to the current user
type EmailRequest struct {
	Address string `json:"address,omitempty"`
	Default *bool  `json:"default,omitempty"`
}

// EmailService interface defines available email methods
type EmailService interface {
	Get(string, *GetOptions) (*Email, *Response, error)
	Create(*EmailRequest) (*Email, *Response, error)
	Update(string, *EmailRequest) (*Email, *Response, error)
	Delete(string) (*Response, error)
}

// Email represents a user's email address
type Email struct {
	ID      string `json:"id"`
	Address string `json:"address"`
	Default bool   `json:"default,omitempty"`
	URL     string `json:"href,omitempty"`
}

func (e Email) String() string {
	return Stringify(e)
}

// EmailServiceOp implements EmailService
type EmailServiceOp struct {
	client *Client
}

// Get retrieves an email by id
func (s *EmailServiceOp) Get(emailID string, getOpt *GetOptions) (*Email, *Response, error) {
	params := createGetOptionsURL(getOpt)
	path := fmt.Sprintf("%s/%s?%s", emailBasePath, emailID, params)
	email := new(Email)

	resp, err := s.client.DoRequest("GET", path, nil, email)
	if err != nil {
		return nil, resp, err
	}

	return email, resp, err
}

// Create adds a new email address to the current user.
func (s *EmailServiceOp) Create(request *EmailRequest) (*Email, *Response, error) {
	email := new(Email)

	resp, err := s.client.DoRequest("POST", emailBasePath, request, email)
	if err != nil {
		return nil, resp, err
	}

	return email, resp, err
}

// Delete removes the email addres from the current user account
func (s *EmailServiceOp) Delete(emailID string) (*Response, error) {
	path := fmt.Sprintf("%s/%s", emailBasePath, emailID)

	resp, err := s.client.DoRequest("DELETE", path, nil, nil)
	if err != nil {
		return resp, err
	}

	return resp, err
}

// Update email parameters
func (s *EmailServiceOp) Update(emailID string, request *EmailRequest) (*Email, *Response, error) {
	email := new(Email)
	path := fmt.Sprintf("%s/%s", emailBasePath, emailID)

	resp, err := s.client.DoRequest("PUT", path, request, email)
	if err != nil {
		return nil, resp, err
	}

	return email, resp, err
}