| 1 | package tables |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 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 GetResult struct { |
| 15 | autorest.Response |
| 16 | |
| 17 | MetaData string `json:"odata.metadata,omitempty"` |
| 18 | Tables []GetResultItem `json:"value"` |
| 19 | } |
| 20 | |
| 21 | // Query returns a list of tables under the specified account. |
| 22 | func (client Client) Query(ctx context.Context, accountName string, metaDataLevel MetaDataLevel) (result GetResult, err error) { |
| 23 | if accountName == "" { |
| 24 | return result, validation.NewError("tables.Client", "Query", "`accountName` cannot be an empty string.") |
| 25 | } |
| 26 | |
| 27 | req, err := client.QueryPreparer(ctx, accountName, metaDataLevel) |
| 28 | if err != nil { |
| 29 | err = autorest.NewErrorWithError(err, "tables.Client", "Query", nil, "Failure preparing request") |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | resp, err := client.QuerySender(req) |
| 34 | if err != nil { |
| 35 | result.Response = autorest.Response{Response: resp} |
| 36 | err = autorest.NewErrorWithError(err, "tables.Client", "Query", resp, "Failure sending request") |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | result, err = client.QueryResponder(resp) |
| 41 | if err != nil { |
| 42 | err = autorest.NewErrorWithError(err, "tables.Client", "Query", resp, "Failure responding to request") |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | // QueryPreparer prepares the Query request. |
| 50 | func (client Client) QueryPreparer(ctx context.Context, accountName string, metaDataLevel MetaDataLevel) (*http.Request, error) { |
| 51 | // NOTE: whilst this supports ContinuationTokens and 'Top' |
| 52 | // it appears that 'Skip' returns a '501 Not Implemented' |
| 53 | // as such, we intentionally don't support those right now |
| 54 | |
| 55 | headers := map[string]interface{}{ |
| 56 | "x-ms-version": APIVersion, |
| 57 | "Accept": fmt.Sprintf("application/json;odata=%s", metaDataLevel), |
| 58 | } |
| 59 | |
| 60 | preparer := autorest.CreatePreparer( |
| 61 | autorest.AsGet(), |
| 62 | autorest.WithBaseURL(endpoints.GetTableEndpoint(client.BaseURI, accountName)), |
| 63 | autorest.WithPath("/Tables"), |
| 64 | autorest.WithHeaders(headers)) |
| 65 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 66 | } |
| 67 | |
| 68 | // QuerySender sends the Query request. The method will close the |
| 69 | // http.Response Body if it receives an error. |
| 70 | func (client Client) QuerySender(req *http.Request) (*http.Response, error) { |
| 71 | return autorest.SendWithSender(client, req, |
| 72 | azure.DoRetryWithRegistration(client.Client)) |
| 73 | } |
| 74 | |
| 75 | // QueryResponder handles the response to the Query request. The method always |
| 76 | // closes the http.Response Body. |
| 77 | func (client Client) QueryResponder(resp *http.Response) (result GetResult, err error) { |
| 78 | err = autorest.Respond( |
| 79 | resp, |
| 80 | client.ByInspecting(), |
| 81 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 82 | autorest.ByUnmarshallingJSON(&result), |
| 83 | autorest.ByClosing()) |
| 84 | result.Response = autorest.Response{Response: resp} |
| 85 | |
| 86 | return |
| 87 | } |