1package tables
2
3import (
4	"context"
5	"net/http"
6
7	"github.com/Azure/go-autorest/autorest"
8	"github.com/Azure/go-autorest/autorest/azure"
9	"github.com/Azure/go-autorest/autorest/validation"
10	"github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
11)
12
13// Exists checks that the specified table exists
14func (client Client) Exists(ctx context.Context, accountName, tableName string) (result autorest.Response, err error) {
15	if accountName == "" {
16		return result, validation.NewError("tables.Client", "Exists", "`accountName` cannot be an empty string.")
17	}
18	if tableName == "" {
19		return result, validation.NewError("tables.Client", "Exists", "`tableName` cannot be an empty string.")
20	}
21
22	req, err := client.ExistsPreparer(ctx, accountName, tableName)
23	if err != nil {
24		err = autorest.NewErrorWithError(err, "tables.Client", "Exists", nil, "Failure preparing request")
25		return
26	}
27
28	resp, err := client.ExistsSender(req)
29	if err != nil {
30		result = autorest.Response{Response: resp}
31		err = autorest.NewErrorWithError(err, "tables.Client", "Exists", resp, "Failure sending request")
32		return
33	}
34
35	result, err = client.ExistsResponder(resp)
36	if err != nil {
37		err = autorest.NewErrorWithError(err, "tables.Client", "Exists", resp, "Failure responding to request")
38		return
39	}
40
41	return
42}
43
44// ExistsPreparer prepares the Exists request.
45func (client Client) ExistsPreparer(ctx context.Context, accountName, tableName string) (*http.Request, error) {
46	pathParameters := map[string]interface{}{
47		"tableName": autorest.Encode("path", tableName),
48	}
49
50	// NOTE: whilst the API documentation says that API Version is Optional
51	// apparently specifying it causes an "invalid content type" to always be returned
52	// as such we omit it here :shrug:
53
54	preparer := autorest.CreatePreparer(
55		autorest.AsGet(),
56		autorest.AsContentType("application/xml"),
57		autorest.WithBaseURL(endpoints.GetTableEndpoint(client.BaseURI, accountName)),
58		autorest.WithPathParameters("/Tables('{tableName}')", pathParameters))
59	return preparer.Prepare((&http.Request{}).WithContext(ctx))
60}
61
62// ExistsSender sends the Exists request. The method will close the
63// http.Response Body if it receives an error.
64func (client Client) ExistsSender(req *http.Request) (*http.Response, error) {
65	return autorest.SendWithSender(client, req,
66		azure.DoRetryWithRegistration(client.Client))
67}
68
69// ExistsResponder handles the response to the Exists request. The method always
70// closes the http.Response Body.
71func (client Client) ExistsResponder(resp *http.Response) (result autorest.Response, err error) {
72	err = autorest.Respond(
73		resp,
74		client.ByInspecting(),
75		azure.WithErrorUnlessStatusCode(http.StatusOK),
76		autorest.ByClosing())
77	result = autorest.Response{Response: resp}
78
79	return
80}
81