| 1 | package entities |
| 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 GetEntityInput struct { |
| 15 | PartitionKey string |
| 16 | RowKey string |
| 17 | |
| 18 | // The Level of MetaData which should be returned |
| 19 | MetaDataLevel MetaDataLevel |
| 20 | } |
| 21 | |
| 22 | type GetEntityResult struct { |
| 23 | autorest.Response |
| 24 | |
| 25 | Entity map[string]interface{} |
| 26 | } |
| 27 | |
| 28 | // Get queries entities in a table and includes the $filter and $select options. |
| 29 | func (client Client) Get(ctx context.Context, accountName, tableName string, input GetEntityInput) (result GetEntityResult, err error) { |
| 30 | if accountName == "" { |
| 31 | return result, validation.NewError("entities.Client", "Get", "`accountName` cannot be an empty string.") |
| 32 | } |
| 33 | if tableName == "" { |
| 34 | return result, validation.NewError("entities.Client", "Get", "`tableName` cannot be an empty string.") |
| 35 | } |
| 36 | if input.PartitionKey == "" { |
| 37 | return result, validation.NewError("entities.Client", "Get", "`input.PartitionKey` cannot be an empty string.") |
| 38 | } |
| 39 | if input.RowKey == "" { |
| 40 | return result, validation.NewError("entities.Client", "Get", "`input.RowKey` cannot be an empty string.") |
| 41 | } |
| 42 | |
| 43 | req, err := client.GetPreparer(ctx, accountName, tableName, input) |
| 44 | if err != nil { |
| 45 | err = autorest.NewErrorWithError(err, "entities.Client", "Get", nil, "Failure preparing request") |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | resp, err := client.GetSender(req) |
| 50 | if err != nil { |
| 51 | result.Response = autorest.Response{Response: resp} |
| 52 | err = autorest.NewErrorWithError(err, "entities.Client", "Get", resp, "Failure sending request") |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | result, err = client.GetResponder(resp) |
| 57 | if err != nil { |
| 58 | err = autorest.NewErrorWithError(err, "entities.Client", "Get", resp, "Failure responding to request") |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | // GetPreparer prepares the Get request. |
| 66 | func (client Client) GetPreparer(ctx context.Context, accountName, tableName string, input GetEntityInput) (*http.Request, error) { |
| 67 | |
| 68 | pathParameters := map[string]interface{}{ |
| 69 | "tableName": autorest.Encode("path", tableName), |
| 70 | "partitionKey": autorest.Encode("path", input.PartitionKey), |
| 71 | "rowKey": autorest.Encode("path", input.RowKey), |
| 72 | } |
| 73 | |
| 74 | headers := map[string]interface{}{ |
| 75 | "x-ms-version": APIVersion, |
| 76 | "Accept": fmt.Sprintf("application/json;odata=%s", input.MetaDataLevel), |
| 77 | "DataServiceVersion": "3.0;NetFx", |
| 78 | "MaxDataServiceVersion": "3.0;NetFx", |
| 79 | } |
| 80 | |
| 81 | preparer := autorest.CreatePreparer( |
| 82 | autorest.AsGet(), |
| 83 | autorest.WithBaseURL(endpoints.GetTableEndpoint(client.BaseURI, accountName)), |
| 84 | autorest.WithPathParameters("/{tableName}(PartitionKey='{partitionKey}',RowKey='{rowKey}')", pathParameters), |
| 85 | autorest.WithHeaders(headers)) |
| 86 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 87 | } |
| 88 | |
| 89 | // GetSender sends the Get request. The method will close the |
| 90 | // http.Response Body if it receives an error. |
| 91 | func (client Client) GetSender(req *http.Request) (*http.Response, error) { |
| 92 | return autorest.SendWithSender(client, req, |
| 93 | azure.DoRetryWithRegistration(client.Client)) |
| 94 | } |
| 95 | |
| 96 | // GetResponder handles the response to the Get request. The method always |
| 97 | // closes the http.Response Body. |
| 98 | func (client Client) GetResponder(resp *http.Response) (result GetEntityResult, err error) { |
| 99 | err = autorest.Respond( |
| 100 | resp, |
| 101 | client.ByInspecting(), |
| 102 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 103 | autorest.ByUnmarshallingJSON(&result.Entity), |
| 104 | autorest.ByClosing()) |
| 105 | result.Response = autorest.Response{Response: resp} |
| 106 | |
| 107 | return |
| 108 | } |