1package apimanagement
2
3// Copyright (c) Microsoft Corporation. All rights reserved.
4// Licensed under the MIT License. See License.txt in the project root for license information.
5//
6// Code generated by Microsoft (R) AutoRest Code Generator.
7// Changes may cause incorrect behavior and will be lost if the code is regenerated.
8
9import (
10	"context"
11	"github.com/Azure/go-autorest/autorest"
12	"github.com/Azure/go-autorest/autorest/azure"
13	"github.com/Azure/go-autorest/autorest/validation"
14	"github.com/Azure/go-autorest/tracing"
15	"net/http"
16)
17
18// ReportsClient is the apiManagement Client
19type ReportsClient struct {
20	BaseClient
21}
22
23// NewReportsClient creates an instance of the ReportsClient client.
24func NewReportsClient(subscriptionID string) ReportsClient {
25	return NewReportsClientWithBaseURI(DefaultBaseURI, subscriptionID)
26}
27
28// NewReportsClientWithBaseURI creates an instance of the ReportsClient client using a custom endpoint.  Use this when
29// interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
30func NewReportsClientWithBaseURI(baseURI string, subscriptionID string) ReportsClient {
31	return ReportsClient{NewWithBaseURI(baseURI, subscriptionID)}
32}
33
34// ListByService lists report records.
35// Parameters:
36// resourceGroupName - the name of the resource group.
37// serviceName - the name of the API Management service.
38// aggregation - report aggregation.
39// filter - the filter to apply on the operation.
40// top - number of records to return.
41// skip - number of records to skip.
42// interval - by time interval. This value is only applicable to ByTime aggregation. Interval must be multiple
43// of 15 minutes and may not be zero. The value should be in ISO  8601 format
44// (http://en.wikipedia.org/wiki/ISO_8601#Durations).This code can be used to convert TimSpan to a valid
45// interval string: XmlConvert.ToString(new TimeSpan(hours, minutes, seconds))
46func (client ReportsClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, aggregation ReportsAggregation, filter string, top *int32, skip *int32, interval *string) (result ReportCollectionPage, err error) {
47	if tracing.IsEnabled() {
48		ctx = tracing.StartSpan(ctx, fqdn+"/ReportsClient.ListByService")
49		defer func() {
50			sc := -1
51			if result.rc.Response.Response != nil {
52				sc = result.rc.Response.Response.StatusCode
53			}
54			tracing.EndSpan(ctx, sc, err)
55		}()
56	}
57	if err := validation.Validate([]validation.Validation{
58		{TargetValue: serviceName,
59			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
60				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
61				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
62		{TargetValue: top,
63			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
64				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil}}}}},
65		{TargetValue: skip,
66			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
67				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: int64(0), Chain: nil}}}}}}); err != nil {
68		return result, validation.NewError("apimanagement.ReportsClient", "ListByService", err.Error())
69	}
70
71	result.fn = client.listByServiceNextResults
72	req, err := client.ListByServicePreparer(ctx, resourceGroupName, serviceName, aggregation, filter, top, skip, interval)
73	if err != nil {
74		err = autorest.NewErrorWithError(err, "apimanagement.ReportsClient", "ListByService", nil, "Failure preparing request")
75		return
76	}
77
78	resp, err := client.ListByServiceSender(req)
79	if err != nil {
80		result.rc.Response = autorest.Response{Response: resp}
81		err = autorest.NewErrorWithError(err, "apimanagement.ReportsClient", "ListByService", resp, "Failure sending request")
82		return
83	}
84
85	result.rc, err = client.ListByServiceResponder(resp)
86	if err != nil {
87		err = autorest.NewErrorWithError(err, "apimanagement.ReportsClient", "ListByService", resp, "Failure responding to request")
88		return
89	}
90	if result.rc.hasNextLink() && result.rc.IsEmpty() {
91		err = result.NextWithContext(ctx)
92		return
93	}
94
95	return
96}
97
98// ListByServicePreparer prepares the ListByService request.
99func (client ReportsClient) ListByServicePreparer(ctx context.Context, resourceGroupName string, serviceName string, aggregation ReportsAggregation, filter string, top *int32, skip *int32, interval *string) (*http.Request, error) {
100	pathParameters := map[string]interface{}{
101		"aggregation":       autorest.Encode("path", aggregation),
102		"resourceGroupName": autorest.Encode("path", resourceGroupName),
103		"serviceName":       autorest.Encode("path", serviceName),
104		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
105	}
106
107	const APIVersion = "2016-10-10"
108	queryParameters := map[string]interface{}{
109		"api-version": APIVersion,
110	}
111	if len(filter) > 0 {
112		queryParameters["$filter"] = autorest.Encode("query", filter)
113	}
114	if top != nil {
115		queryParameters["$top"] = autorest.Encode("query", *top)
116	}
117	if skip != nil {
118		queryParameters["$skip"] = autorest.Encode("query", *skip)
119	}
120	if interval != nil {
121		queryParameters["interval"] = autorest.Encode("query", *interval)
122	}
123
124	preparer := autorest.CreatePreparer(
125		autorest.AsGet(),
126		autorest.WithBaseURL(client.BaseURI),
127		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/reports/{aggregation}", pathParameters),
128		autorest.WithQueryParameters(queryParameters))
129	return preparer.Prepare((&http.Request{}).WithContext(ctx))
130}
131
132// ListByServiceSender sends the ListByService request. The method will close the
133// http.Response Body if it receives an error.
134func (client ReportsClient) ListByServiceSender(req *http.Request) (*http.Response, error) {
135	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
136}
137
138// ListByServiceResponder handles the response to the ListByService request. The method always
139// closes the http.Response Body.
140func (client ReportsClient) ListByServiceResponder(resp *http.Response) (result ReportCollection, err error) {
141	err = autorest.Respond(
142		resp,
143		azure.WithErrorUnlessStatusCode(http.StatusOK),
144		autorest.ByUnmarshallingJSON(&result),
145		autorest.ByClosing())
146	result.Response = autorest.Response{Response: resp}
147	return
148}
149
150// listByServiceNextResults retrieves the next set of results, if any.
151func (client ReportsClient) listByServiceNextResults(ctx context.Context, lastResults ReportCollection) (result ReportCollection, err error) {
152	req, err := lastResults.reportCollectionPreparer(ctx)
153	if err != nil {
154		return result, autorest.NewErrorWithError(err, "apimanagement.ReportsClient", "listByServiceNextResults", nil, "Failure preparing next results request")
155	}
156	if req == nil {
157		return
158	}
159	resp, err := client.ListByServiceSender(req)
160	if err != nil {
161		result.Response = autorest.Response{Response: resp}
162		return result, autorest.NewErrorWithError(err, "apimanagement.ReportsClient", "listByServiceNextResults", resp, "Failure sending next results request")
163	}
164	result, err = client.ListByServiceResponder(resp)
165	if err != nil {
166		err = autorest.NewErrorWithError(err, "apimanagement.ReportsClient", "listByServiceNextResults", resp, "Failure responding to next results request")
167	}
168	return
169}
170
171// ListByServiceComplete enumerates all values, automatically crossing page boundaries as required.
172func (client ReportsClient) ListByServiceComplete(ctx context.Context, resourceGroupName string, serviceName string, aggregation ReportsAggregation, filter string, top *int32, skip *int32, interval *string) (result ReportCollectionIterator, err error) {
173	if tracing.IsEnabled() {
174		ctx = tracing.StartSpan(ctx, fqdn+"/ReportsClient.ListByService")
175		defer func() {
176			sc := -1
177			if result.Response().Response.Response != nil {
178				sc = result.page.Response().Response.Response.StatusCode
179			}
180			tracing.EndSpan(ctx, sc, err)
181		}()
182	}
183	result.page, err = client.ListByService(ctx, resourceGroupName, serviceName, aggregation, filter, top, skip, interval)
184	return
185}
186