Debian Codegraph @

storage/2017-07-29/queue/messages/put.go

raw
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 type PutInput struct {
15 // A message must be in a format that can be included in an XML request with UTF-8 encoding.
16 // The encoded message can be up to 64 KB in size.
17 Message string
18
19 // The maximum time-to-live can be any positive number,
20 // as well as -1 indicating that the message does not expire.
21 // If this parameter is omitted, the default time-to-live is 7 days.
22 MessageTtl *int
23
24 // Specifies the new visibility timeout value, in seconds, relative to server time.
25 // The new value must be larger than or equal to 0, and cannot be larger than 7 days.
26 // The visibility timeout of a message cannot be set to a value later than the expiry time.
27 // visibilitytimeout should be set to a value smaller than the time-to-live value.
28 // If not specified, the default value is 0.
29 VisibilityTimeout *int
30 }
31
32 // Put adds a new message to the back of the message queue
33 func (client Client) Put(ctx context.Context, accountName, queueName string, input PutInput) (result QueueMessagesListResult, err error) {
34 if accountName == "" {
35 return result, validation.NewError("messages.Client", "Put", "`accountName` cannot be an empty string.")
36 }
37 if queueName == "" {
38 return result, validation.NewError("messages.Client", "Put", "`queueName` cannot be an empty string.")
39 }
40 if strings.ToLower(queueName) != queueName {
41 return result, validation.NewError("messages.Client", "Put", "`queueName` must be a lower-cased string.")
42 }
43
44 req, err := client.PutPreparer(ctx, accountName, queueName, input)
45 if err != nil {
46 err = autorest.NewErrorWithError(err, "messages.Client", "Put", nil, "Failure preparing request")
47 return
48 }
49
50 resp, err := client.PutSender(req)
51 if err != nil {
52 result.Response = autorest.Response{Response: resp}
53 err = autorest.NewErrorWithError(err, "messages.Client", "Put", resp, "Failure sending request")
54 return
55 }
56
57 result, err = client.PutResponder(resp)
58 if err != nil {
59 err = autorest.NewErrorWithError(err, "messages.Client", "Put", resp, "Failure responding to request")
60 return
61 }
62
63 return
64 }
65
66 // PutPreparer prepares the Put request.
67 func (client Client) PutPreparer(ctx context.Context, accountName, queueName string, input PutInput) (*http.Request, error) {
68 pathParameters := map[string]interface{}{
69 "queueName": autorest.Encode("path", queueName),
70 }
71
72 queryParameters := map[string]interface{}{}
73
74 if input.MessageTtl != nil {
75 queryParameters["messagettl"] = autorest.Encode("path", *input.MessageTtl)
76 }
77
78 if input.VisibilityTimeout != nil {
79 queryParameters["visibilitytimeout"] = autorest.Encode("path", *input.VisibilityTimeout)
80 }
81
82 headers := map[string]interface{}{
83 "x-ms-version": APIVersion,
84 }
85
86 body := QueueMessage{
87 MessageText: input.Message,
88 }
89
90 preparer := autorest.CreatePreparer(
91 autorest.AsContentType("application/xml; charset=utf-8"),
92 autorest.AsPost(),
93 autorest.WithBaseURL(endpoints.GetQueueEndpoint(client.BaseURI, accountName)),
94 autorest.WithPathParameters("/{queueName}/messages", pathParameters),
95 autorest.WithQueryParameters(queryParameters),
96 autorest.WithXML(body),
97 autorest.WithHeaders(headers))
98 return preparer.Prepare((&http.Request{}).WithContext(ctx))
99 }
100
101 // PutSender sends the Put request. The method will close the
102 // http.Response Body if it receives an error.
103 func (client Client) PutSender(req *http.Request) (*http.Response, error) {
104 return autorest.SendWithSender(client, req,
105 azure.DoRetryWithRegistration(client.Client))
106 }
107
108 // PutResponder handles the response to the Put request. The method always
109 // closes the http.Response Body.
110 func (client Client) PutResponder(resp *http.Response) (result QueueMessagesListResult, err error) {
111 err = autorest.Respond(
112 resp,
113 client.ByInspecting(),
114 autorest.ByUnmarshallingXML(&result),
115 azure.WithErrorUnlessStatusCode(http.StatusCreated),
116 autorest.ByClosing())
117 result.Response = autorest.Response{Response: resp}
118
119 return
120 }