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