github.com/mailgun/mailgun-go/v3@v3.6.4/credentials.go (about) 1 package mailgun 2 3 import ( 4 "context" 5 "fmt" 6 "strconv" 7 ) 8 9 // A Credential structure describes a principle allowed to send or receive mail at the domain. 10 type Credential struct { 11 CreatedAt RFC2822Time `json:"created_at"` 12 Login string `json:"login"` 13 Password string `json:"password"` 14 } 15 16 type credentialsListResponse struct { 17 // is -1 if Next() or First() have not been called 18 TotalCount int `json:"total_count"` 19 Items []Credential `json:"items"` 20 } 21 22 // Returned when a required parameter is missing. 23 var ErrEmptyParam = fmt.Errorf("empty or illegal parameter") 24 25 // ListCredentials returns the (possibly zero-length) list of credentials associated with your domain. 26 func (mg *MailgunImpl) ListCredentials(opts *ListOptions) *CredentialsIterator { 27 var limit int 28 if opts != nil { 29 limit = opts.Limit 30 } 31 32 if limit == 0 { 33 limit = 100 34 } 35 return &CredentialsIterator{ 36 mg: mg, 37 url: generateCredentialsUrl(mg, ""), 38 credentialsListResponse: credentialsListResponse{TotalCount: -1}, 39 limit: limit, 40 } 41 } 42 43 type CredentialsIterator struct { 44 credentialsListResponse 45 46 limit int 47 mg Mailgun 48 offset int 49 url string 50 err error 51 } 52 53 // If an error occurred during iteration `Err()` will return non nil 54 func (ri *CredentialsIterator) Err() error { 55 return ri.err 56 } 57 58 // Offset returns the current offset of the iterator 59 func (ri *CredentialsIterator) Offset() int { 60 return ri.offset 61 } 62 63 // Next retrieves the next page of items from the api. Returns false when there 64 // no more pages to retrieve or if there was an error. Use `.Err()` to retrieve 65 // the error 66 func (ri *CredentialsIterator) Next(ctx context.Context, items *[]Credential) bool { 67 if ri.err != nil { 68 return false 69 } 70 71 ri.err = ri.fetch(ctx, ri.offset, ri.limit) 72 if ri.err != nil { 73 return false 74 } 75 76 cpy := make([]Credential, len(ri.Items)) 77 copy(cpy, ri.Items) 78 *items = cpy 79 if len(ri.Items) == 0 { 80 return false 81 } 82 ri.offset = ri.offset + len(ri.Items) 83 return true 84 } 85 86 // First retrieves the first page of items from the api. Returns false if there 87 // was an error. It also sets the iterator object to the first page. 88 // Use `.Err()` to retrieve the error. 89 func (ri *CredentialsIterator) First(ctx context.Context, items *[]Credential) bool { 90 if ri.err != nil { 91 return false 92 } 93 ri.err = ri.fetch(ctx, 0, ri.limit) 94 if ri.err != nil { 95 return false 96 } 97 cpy := make([]Credential, len(ri.Items)) 98 copy(cpy, ri.Items) 99 *items = cpy 100 ri.offset = len(ri.Items) 101 return true 102 } 103 104 // Last retrieves the last page of items from the api. 105 // Calling Last() is invalid unless you first call First() or Next() 106 // Returns false if there was an error. It also sets the iterator object 107 // to the last page. Use `.Err()` to retrieve the error. 108 func (ri *CredentialsIterator) Last(ctx context.Context, items *[]Credential) bool { 109 if ri.err != nil { 110 return false 111 } 112 113 if ri.TotalCount == -1 { 114 return false 115 } 116 117 ri.offset = ri.TotalCount - ri.limit 118 if ri.offset < 0 { 119 ri.offset = 0 120 } 121 122 ri.err = ri.fetch(ctx, ri.offset, ri.limit) 123 if ri.err != nil { 124 return false 125 } 126 cpy := make([]Credential, len(ri.Items)) 127 copy(cpy, ri.Items) 128 *items = cpy 129 return true 130 } 131 132 // Previous retrieves the previous page of items from the api. Returns false when there 133 // no more pages to retrieve or if there was an error. Use `.Err()` to retrieve 134 // the error if any 135 func (ri *CredentialsIterator) Previous(ctx context.Context, items *[]Credential) bool { 136 if ri.err != nil { 137 return false 138 } 139 140 if ri.TotalCount == -1 { 141 return false 142 } 143 144 ri.offset = ri.offset - (ri.limit * 2) 145 if ri.offset < 0 { 146 ri.offset = 0 147 } 148 149 ri.err = ri.fetch(ctx, ri.offset, ri.limit) 150 if ri.err != nil { 151 return false 152 } 153 cpy := make([]Credential, len(ri.Items)) 154 copy(cpy, ri.Items) 155 *items = cpy 156 if len(ri.Items) == 0 { 157 return false 158 } 159 return true 160 } 161 162 func (ri *CredentialsIterator) fetch(ctx context.Context, skip, limit int) error { 163 r := newHTTPRequest(ri.url) 164 r.setBasicAuth(basicAuthUser, ri.mg.APIKey()) 165 r.setClient(ri.mg.Client()) 166 167 if skip != 0 { 168 r.addParameter("skip", strconv.Itoa(skip)) 169 } 170 if limit != 0 { 171 r.addParameter("limit", strconv.Itoa(limit)) 172 } 173 174 return getResponseFromJSON(ctx, r, &ri.credentialsListResponse) 175 } 176 177 // CreateCredential attempts to create associate a new principle with your domain. 178 func (mg *MailgunImpl) CreateCredential(ctx context.Context, login, password string) error { 179 if (login == "") || (password == "") { 180 return ErrEmptyParam 181 } 182 r := newHTTPRequest(generateCredentialsUrl(mg, "")) 183 r.setClient(mg.Client()) 184 r.setBasicAuth(basicAuthUser, mg.APIKey()) 185 p := newUrlEncodedPayload() 186 p.addValue("login", login) 187 p.addValue("password", password) 188 _, err := makePostRequest(ctx, r, p) 189 return err 190 } 191 192 // ChangeCredentialPassword attempts to alter the indicated credential's password. 193 func (mg *MailgunImpl) ChangeCredentialPassword(ctx context.Context, login, password string) error { 194 if (login == "") || (password == "") { 195 return ErrEmptyParam 196 } 197 r := newHTTPRequest(generateCredentialsUrl(mg, login)) 198 r.setClient(mg.Client()) 199 r.setBasicAuth(basicAuthUser, mg.APIKey()) 200 p := newUrlEncodedPayload() 201 p.addValue("password", password) 202 _, err := makePutRequest(ctx, r, p) 203 return err 204 } 205 206 // DeleteCredential attempts to remove the indicated principle from the domain. 207 func (mg *MailgunImpl) DeleteCredential(ctx context.Context, login string) error { 208 if login == "" { 209 return ErrEmptyParam 210 } 211 r := newHTTPRequest(generateCredentialsUrl(mg, login)) 212 r.setClient(mg.Client()) 213 r.setBasicAuth(basicAuthUser, mg.APIKey()) 214 _, err := makeDeleteRequest(ctx, r) 215 return err 216 }