github.com/gophercloud/gophercloud@v1.11.0/openstack/messaging/v2/messages/requests.go (about) 1 package messages 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to the 9 // List request. 10 type ListOptsBuilder interface { 11 ToMessageListQuery() (string, error) 12 } 13 14 // ListOpts params to be used with List. 15 type ListOpts struct { 16 // Limit instructs List to refrain from sending excessively large lists of queues 17 Limit int `q:"limit,omitempty"` 18 19 // Marker and Limit control paging. Marker instructs List where to start listing from. 20 Marker string `q:"marker,omitempty"` 21 22 // Indicate if the messages can be echoed back to the client that posted them. 23 Echo bool `q:"echo,omitempty"` 24 25 // Indicate if the messages list should include the claimed messages. 26 IncludeClaimed bool `q:"include_claimed,omitempty"` 27 28 //Indicate if the messages list should include the delayed messages. 29 IncludeDelayed bool `q:"include_delayed,omitempty"` 30 } 31 32 func (opts ListOpts) ToMessageListQuery() (string, error) { 33 q, err := gophercloud.BuildQueryString(opts) 34 return q.String(), err 35 } 36 37 // ListMessages lists messages on a specific queue based off queue name. 38 func List(client *gophercloud.ServiceClient, queueName string, opts ListOptsBuilder) pagination.Pager { 39 url := listURL(client, queueName) 40 if opts != nil { 41 query, err := opts.ToMessageListQuery() 42 if err != nil { 43 return pagination.Pager{Err: err} 44 } 45 url += query 46 } 47 48 pager := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 49 return MessagePage{pagination.LinkedPageBase{PageResult: r}} 50 }) 51 return pager 52 } 53 54 // CreateOptsBuilder Builder. 55 type CreateOptsBuilder interface { 56 ToMessageCreateMap() (map[string]interface{}, error) 57 } 58 59 // BatchCreateOpts is an array of CreateOpts. 60 type BatchCreateOpts []CreateOpts 61 62 // CreateOpts params to be used with Create. 63 type CreateOpts struct { 64 // TTL specifies how long the server waits before marking the message 65 // as expired and removing it from the queue. 66 TTL int `json:"ttl,omitempty"` 67 68 // Delay specifies how long the message can be claimed. 69 Delay int `json:"delay,omitempty"` 70 71 // Body specifies an arbitrary document that constitutes the body of the message being sent. 72 Body map[string]interface{} `json:"body" required:"true"` 73 } 74 75 // ToMessageCreateMap constructs a request body from BatchCreateOpts. 76 func (opts BatchCreateOpts) ToMessageCreateMap() (map[string]interface{}, error) { 77 messages := make([]map[string]interface{}, len(opts)) 78 for i, message := range opts { 79 messageMap, err := message.ToMap() 80 if err != nil { 81 return nil, err 82 } 83 messages[i] = messageMap 84 } 85 return map[string]interface{}{"messages": messages}, nil 86 } 87 88 // ToMap constructs a request body from UpdateOpts. 89 func (opts CreateOpts) ToMap() (map[string]interface{}, error) { 90 return gophercloud.BuildRequestBody(opts, "") 91 } 92 93 // Create creates a message on a specific queue based of off queue name. 94 func Create(client *gophercloud.ServiceClient, queueName string, opts CreateOptsBuilder) (r CreateResult) { 95 b, err := opts.ToMessageCreateMap() 96 if err != nil { 97 r.Err = err 98 return 99 } 100 101 resp, err := client.Post(createURL(client, queueName), b, &r.Body, &gophercloud.RequestOpts{ 102 OkCodes: []int{201}, 103 }) 104 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 105 return 106 } 107 108 // DeleteMessagesOptsBuilder allows extensions to add additional parameters to the 109 // DeleteMessages request. 110 type DeleteMessagesOptsBuilder interface { 111 ToMessagesDeleteQuery() (string, error) 112 } 113 114 // DeleteMessagesOpts params to be used with DeleteMessages. 115 type DeleteMessagesOpts struct { 116 IDs []string `q:"ids,omitempty"` 117 } 118 119 // ToMessagesDeleteQuery formats a DeleteMessagesOpts structure into a query string. 120 func (opts DeleteMessagesOpts) ToMessagesDeleteQuery() (string, error) { 121 q, err := gophercloud.BuildQueryString(opts) 122 return q.String(), err 123 } 124 125 // DeleteMessages deletes multiple messages based off of ID. 126 func DeleteMessages(client *gophercloud.ServiceClient, queueName string, opts DeleteMessagesOptsBuilder) (r DeleteResult) { 127 url := deleteURL(client, queueName) 128 if opts != nil { 129 query, err := opts.ToMessagesDeleteQuery() 130 if err != nil { 131 r.Err = err 132 return 133 } 134 url += query 135 } 136 resp, err := client.Delete(url, &gophercloud.RequestOpts{ 137 OkCodes: []int{200, 204}, 138 }) 139 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 140 return 141 } 142 143 // PopMessagesOptsBuilder allows extensions to add additional parameters to the 144 // DeleteMessages request. 145 type PopMessagesOptsBuilder interface { 146 ToMessagesPopQuery() (string, error) 147 } 148 149 // PopMessagesOpts params to be used with PopMessages. 150 type PopMessagesOpts struct { 151 Pop int `q:"pop,omitempty"` 152 } 153 154 // ToMessagesPopQuery formats a PopMessagesOpts structure into a query string. 155 func (opts PopMessagesOpts) ToMessagesPopQuery() (string, error) { 156 q, err := gophercloud.BuildQueryString(opts) 157 return q.String(), err 158 } 159 160 // PopMessages deletes and returns multiple messages based off of number of messages. 161 func PopMessages(client *gophercloud.ServiceClient, queueName string, opts PopMessagesOptsBuilder) (r PopResult) { 162 url := deleteURL(client, queueName) 163 if opts != nil { 164 query, err := opts.ToMessagesPopQuery() 165 if err != nil { 166 r.Err = err 167 return 168 } 169 url += query 170 } 171 resp, err := client.Delete(url, &gophercloud.RequestOpts{ 172 JSONResponse: &r.Body, 173 OkCodes: []int{200, 204}, 174 }) 175 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 176 return 177 } 178 179 // GetMessagesOptsBuilder allows extensions to add additional parameters to the 180 // GetMessages request. 181 type GetMessagesOptsBuilder interface { 182 ToGetMessagesListQuery() (string, error) 183 } 184 185 // GetMessagesOpts params to be used with GetMessages. 186 type GetMessagesOpts struct { 187 IDs []string `q:"ids,omitempty"` 188 } 189 190 // ToGetMessagesListQuery formats a GetMessagesOpts structure into a query string. 191 func (opts GetMessagesOpts) ToGetMessagesListQuery() (string, error) { 192 q, err := gophercloud.BuildQueryString(opts) 193 return q.String(), err 194 } 195 196 // GetMessages requests details on a multiple messages, by IDs. 197 func GetMessages(client *gophercloud.ServiceClient, queueName string, opts GetMessagesOptsBuilder) (r GetMessagesResult) { 198 url := getURL(client, queueName) 199 if opts != nil { 200 query, err := opts.ToGetMessagesListQuery() 201 if err != nil { 202 r.Err = err 203 return 204 } 205 url += query 206 } 207 resp, err := client.Get(url, &r.Body, &gophercloud.RequestOpts{ 208 OkCodes: []int{200}, 209 }) 210 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 211 return 212 } 213 214 // Get requests details on a single message, by ID. 215 func Get(client *gophercloud.ServiceClient, queueName string, messageID string) (r GetResult) { 216 resp, err := client.Get(messageURL(client, queueName, messageID), &r.Body, &gophercloud.RequestOpts{ 217 OkCodes: []int{200}, 218 }) 219 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 220 return 221 } 222 223 // DeleteOptsBuilder allows extensions to add additional parameters to the 224 // delete request. 225 type DeleteOptsBuilder interface { 226 ToMessageDeleteQuery() (string, error) 227 } 228 229 // DeleteOpts params to be used with Delete. 230 type DeleteOpts struct { 231 // ClaimID instructs Delete to delete a message that is associated with a claim ID 232 ClaimID string `q:"claim_id,omitempty"` 233 } 234 235 // ToMessageDeleteQuery formats a DeleteOpts structure into a query string. 236 func (opts DeleteOpts) ToMessageDeleteQuery() (string, error) { 237 q, err := gophercloud.BuildQueryString(opts) 238 return q.String(), err 239 } 240 241 // Delete deletes a specific message from the queue. 242 func Delete(client *gophercloud.ServiceClient, queueName string, messageID string, opts DeleteOptsBuilder) (r DeleteResult) { 243 url := DeleteMessageURL(client, queueName, messageID) 244 if opts != nil { 245 query, err := opts.ToMessageDeleteQuery() 246 if err != nil { 247 r.Err = err 248 return 249 } 250 url += query 251 } 252 resp, err := client.Delete(url, &gophercloud.RequestOpts{ 253 OkCodes: []int{204}, 254 }) 255 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 256 return 257 }