| 1 | package messages |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/Azure/go-autorest/autorest" |
| 9 | "github.com/Azure/go-autorest/autorest/azure" |
| 10 | "github.com/Azure/go-autorest/autorest/validation" |
| 11 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 12 | ) |
| 13 | |
| 14 | // Peek retrieves one or more messages from the front of the queue, but doesn't alter the visibility of the messages |
| 15 | func (client Client) Peek(ctx context.Context, accountName, queueName string, numberOfMessages int) (result QueueMessagesListResult, err error) { |
| 16 | if accountName == "" { |
| 17 | return result, validation.NewError("messages.Client", "Peek", "`accountName` cannot be an empty string.") |
| 18 | } |
| 19 | if queueName == "" { |
| 20 | return result, validation.NewError("messages.Client", "Peek", "`queueName` cannot be an empty string.") |
| 21 | } |
| 22 | if strings.ToLower(queueName) != queueName { |
| 23 | return result, validation.NewError("messages.Client", "Peek", "`queueName` must be a lower-cased string.") |
| 24 | } |
| 25 | if numberOfMessages < 1 || numberOfMessages > 32 { |
| 26 | return result, validation.NewError("messages.Client", "Peek", "`numberOfMessages` must be between 1 and 32.") |
| 27 | } |
| 28 | |
| 29 | req, err := client.PeekPreparer(ctx, accountName, queueName, numberOfMessages) |
| 30 | if err != nil { |
| 31 | err = autorest.NewErrorWithError(err, "messages.Client", "Peek", nil, "Failure preparing request") |
| 32 | return |
| 33 | } |
| 34 | |
| 35 | resp, err := client.PeekSender(req) |
| 36 | if err != nil { |
| 37 | result.Response = autorest.Response{Response: resp} |
| 38 | err = autorest.NewErrorWithError(err, "messages.Client", "Peek", resp, "Failure sending request") |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | result, err = client.PeekResponder(resp) |
| 43 | if err != nil { |
| 44 | err = autorest.NewErrorWithError(err, "messages.Client", "Peek", resp, "Failure responding to request") |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | // PeekPreparer prepares the Peek request. |
| 52 | func (client Client) PeekPreparer(ctx context.Context, accountName, queueName string, numberOfMessages int) (*http.Request, error) { |
| 53 | pathParameters := map[string]interface{}{ |
| 54 | "queueName": autorest.Encode("path", queueName), |
| 55 | } |
| 56 | |
| 57 | queryParameters := map[string]interface{}{ |
| 58 | "numofmessages": autorest.Encode("query", numberOfMessages), |
| 59 | "peekonly": autorest.Encode("query", true), |
| 60 | } |
| 61 | |
| 62 | headers := map[string]interface{}{ |
| 63 | "x-ms-version": APIVersion, |
| 64 | } |
| 65 | |
| 66 | preparer := autorest.CreatePreparer( |
| 67 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 68 | autorest.AsGet(), |
| 69 | autorest.WithBaseURL(endpoints.GetQueueEndpoint(client.BaseURI, accountName)), |
| 70 | autorest.WithPathParameters("/{queueName}/messages", pathParameters), |
| 71 | autorest.WithQueryParameters(queryParameters), |
| 72 | autorest.WithHeaders(headers)) |
| 73 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 74 | } |
| 75 | |
| 76 | // PeekSender sends the Peek request. The method will close the |
| 77 | // http.Response Body if it receives an error. |
| 78 | func (client Client) PeekSender(req *http.Request) (*http.Response, error) { |
| 79 | return autorest.SendWithSender(client, req, |
| 80 | azure.DoRetryWithRegistration(client.Client)) |
| 81 | } |
| 82 | |
| 83 | // PeekResponder handles the response to the Peek request. The method always |
| 84 | // closes the http.Response Body. |
| 85 | func (client Client) PeekResponder(resp *http.Response) (result QueueMessagesListResult, err error) { |
| 86 | err = autorest.Respond( |
| 87 | resp, |
| 88 | client.ByInspecting(), |
| 89 | autorest.ByUnmarshallingXML(&result), |
| 90 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 91 | autorest.ByClosing()) |
| 92 | result.Response = autorest.Response{Response: resp} |
| 93 | |
| 94 | return |
| 95 | } |