1package apimanagement
2
3// Copyright (c) Microsoft and contributors.  All rights reserved.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// Code generated by Microsoft (R) AutoRest Code Generator.
18// Changes may cause incorrect behavior and will be lost if the code is regenerated.
19
20import (
21	"context"
22	"github.com/Azure/go-autorest/autorest"
23	"github.com/Azure/go-autorest/autorest/azure"
24	"github.com/Azure/go-autorest/autorest/validation"
25	"net/http"
26)
27
28// NotificationClient is the apiManagement Client
29type NotificationClient struct {
30	BaseClient
31}
32
33// NewNotificationClient creates an instance of the NotificationClient client.
34func NewNotificationClient(subscriptionID string) NotificationClient {
35	return NewNotificationClientWithBaseURI(DefaultBaseURI, subscriptionID)
36}
37
38// NewNotificationClientWithBaseURI creates an instance of the NotificationClient client.
39func NewNotificationClientWithBaseURI(baseURI string, subscriptionID string) NotificationClient {
40	return NotificationClient{NewWithBaseURI(baseURI, subscriptionID)}
41}
42
43// CreateOrUpdate updates an Notification.
44// Parameters:
45// resourceGroupName - the name of the resource group.
46// serviceName - the name of the API Management service.
47// notificationName - notification Name Identifier.
48// ifMatch - eTag of the Entity. Not required when creating an entity, but required when updating an entity.
49func (client NotificationClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, ifMatch string) (result NotificationContract, err error) {
50	if err := validation.Validate([]validation.Validation{
51		{TargetValue: serviceName,
52			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
53				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
54				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
55		return result, validation.NewError("apimanagement.NotificationClient", "CreateOrUpdate", err.Error())
56	}
57
58	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serviceName, notificationName, ifMatch)
59	if err != nil {
60		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "CreateOrUpdate", nil, "Failure preparing request")
61		return
62	}
63
64	resp, err := client.CreateOrUpdateSender(req)
65	if err != nil {
66		result.Response = autorest.Response{Response: resp}
67		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "CreateOrUpdate", resp, "Failure sending request")
68		return
69	}
70
71	result, err = client.CreateOrUpdateResponder(resp)
72	if err != nil {
73		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "CreateOrUpdate", resp, "Failure responding to request")
74	}
75
76	return
77}
78
79// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
80func (client NotificationClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, ifMatch string) (*http.Request, error) {
81	pathParameters := map[string]interface{}{
82		"notificationName":  autorest.Encode("path", notificationName),
83		"resourceGroupName": autorest.Encode("path", resourceGroupName),
84		"serviceName":       autorest.Encode("path", serviceName),
85		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
86	}
87
88	const APIVersion = "2018-06-01-preview"
89	queryParameters := map[string]interface{}{
90		"api-version": APIVersion,
91	}
92
93	preparer := autorest.CreatePreparer(
94		autorest.AsPut(),
95		autorest.WithBaseURL(client.BaseURI),
96		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}", pathParameters),
97		autorest.WithQueryParameters(queryParameters))
98	if len(ifMatch) > 0 {
99		preparer = autorest.DecoratePreparer(preparer,
100			autorest.WithHeader("If-Match", autorest.String(ifMatch)))
101	}
102	return preparer.Prepare((&http.Request{}).WithContext(ctx))
103}
104
105// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
106// http.Response Body if it receives an error.
107func (client NotificationClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
108	return autorest.SendWithSender(client, req,
109		azure.DoRetryWithRegistration(client.Client))
110}
111
112// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
113// closes the http.Response Body.
114func (client NotificationClient) CreateOrUpdateResponder(resp *http.Response) (result NotificationContract, err error) {
115	err = autorest.Respond(
116		resp,
117		client.ByInspecting(),
118		azure.WithErrorUnlessStatusCode(http.StatusOK),
119		autorest.ByUnmarshallingJSON(&result),
120		autorest.ByClosing())
121	result.Response = autorest.Response{Response: resp}
122	return
123}
124
125// Get gets the details of the Notification specified by its identifier.
126// Parameters:
127// resourceGroupName - the name of the resource group.
128// serviceName - the name of the API Management service.
129// notificationName - notification Name Identifier.
130func (client NotificationClient) Get(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName) (result NotificationContract, err error) {
131	if err := validation.Validate([]validation.Validation{
132		{TargetValue: serviceName,
133			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
134				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
135				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
136		return result, validation.NewError("apimanagement.NotificationClient", "Get", err.Error())
137	}
138
139	req, err := client.GetPreparer(ctx, resourceGroupName, serviceName, notificationName)
140	if err != nil {
141		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "Get", nil, "Failure preparing request")
142		return
143	}
144
145	resp, err := client.GetSender(req)
146	if err != nil {
147		result.Response = autorest.Response{Response: resp}
148		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "Get", resp, "Failure sending request")
149		return
150	}
151
152	result, err = client.GetResponder(resp)
153	if err != nil {
154		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "Get", resp, "Failure responding to request")
155	}
156
157	return
158}
159
160// GetPreparer prepares the Get request.
161func (client NotificationClient) GetPreparer(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName) (*http.Request, error) {
162	pathParameters := map[string]interface{}{
163		"notificationName":  autorest.Encode("path", notificationName),
164		"resourceGroupName": autorest.Encode("path", resourceGroupName),
165		"serviceName":       autorest.Encode("path", serviceName),
166		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
167	}
168
169	const APIVersion = "2018-06-01-preview"
170	queryParameters := map[string]interface{}{
171		"api-version": APIVersion,
172	}
173
174	preparer := autorest.CreatePreparer(
175		autorest.AsGet(),
176		autorest.WithBaseURL(client.BaseURI),
177		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}", pathParameters),
178		autorest.WithQueryParameters(queryParameters))
179	return preparer.Prepare((&http.Request{}).WithContext(ctx))
180}
181
182// GetSender sends the Get request. The method will close the
183// http.Response Body if it receives an error.
184func (client NotificationClient) GetSender(req *http.Request) (*http.Response, error) {
185	return autorest.SendWithSender(client, req,
186		azure.DoRetryWithRegistration(client.Client))
187}
188
189// GetResponder handles the response to the Get request. The method always
190// closes the http.Response Body.
191func (client NotificationClient) GetResponder(resp *http.Response) (result NotificationContract, err error) {
192	err = autorest.Respond(
193		resp,
194		client.ByInspecting(),
195		azure.WithErrorUnlessStatusCode(http.StatusOK),
196		autorest.ByUnmarshallingJSON(&result),
197		autorest.ByClosing())
198	result.Response = autorest.Response{Response: resp}
199	return
200}
201
202// ListByService lists a collection of properties defined within a service instance.
203// Parameters:
204// resourceGroupName - the name of the resource group.
205// serviceName - the name of the API Management service.
206// top - number of records to return.
207// skip - number of records to skip.
208func (client NotificationClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, top *int32, skip *int32) (result NotificationCollectionPage, err error) {
209	if err := validation.Validate([]validation.Validation{
210		{TargetValue: serviceName,
211			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
212				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
213				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
214		{TargetValue: top,
215			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
216				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: 1, Chain: nil}}}}},
217		{TargetValue: skip,
218			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
219				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: 0, Chain: nil}}}}}}); err != nil {
220		return result, validation.NewError("apimanagement.NotificationClient", "ListByService", err.Error())
221	}
222
223	result.fn = client.listByServiceNextResults
224	req, err := client.ListByServicePreparer(ctx, resourceGroupName, serviceName, top, skip)
225	if err != nil {
226		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "ListByService", nil, "Failure preparing request")
227		return
228	}
229
230	resp, err := client.ListByServiceSender(req)
231	if err != nil {
232		result.nc.Response = autorest.Response{Response: resp}
233		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "ListByService", resp, "Failure sending request")
234		return
235	}
236
237	result.nc, err = client.ListByServiceResponder(resp)
238	if err != nil {
239		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "ListByService", resp, "Failure responding to request")
240	}
241
242	return
243}
244
245// ListByServicePreparer prepares the ListByService request.
246func (client NotificationClient) ListByServicePreparer(ctx context.Context, resourceGroupName string, serviceName string, top *int32, skip *int32) (*http.Request, error) {
247	pathParameters := map[string]interface{}{
248		"resourceGroupName": autorest.Encode("path", resourceGroupName),
249		"serviceName":       autorest.Encode("path", serviceName),
250		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
251	}
252
253	const APIVersion = "2018-06-01-preview"
254	queryParameters := map[string]interface{}{
255		"api-version": APIVersion,
256	}
257	if top != nil {
258		queryParameters["$top"] = autorest.Encode("query", *top)
259	}
260	if skip != nil {
261		queryParameters["$skip"] = autorest.Encode("query", *skip)
262	}
263
264	preparer := autorest.CreatePreparer(
265		autorest.AsGet(),
266		autorest.WithBaseURL(client.BaseURI),
267		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications", pathParameters),
268		autorest.WithQueryParameters(queryParameters))
269	return preparer.Prepare((&http.Request{}).WithContext(ctx))
270}
271
272// ListByServiceSender sends the ListByService request. The method will close the
273// http.Response Body if it receives an error.
274func (client NotificationClient) ListByServiceSender(req *http.Request) (*http.Response, error) {
275	return autorest.SendWithSender(client, req,
276		azure.DoRetryWithRegistration(client.Client))
277}
278
279// ListByServiceResponder handles the response to the ListByService request. The method always
280// closes the http.Response Body.
281func (client NotificationClient) ListByServiceResponder(resp *http.Response) (result NotificationCollection, err error) {
282	err = autorest.Respond(
283		resp,
284		client.ByInspecting(),
285		azure.WithErrorUnlessStatusCode(http.StatusOK),
286		autorest.ByUnmarshallingJSON(&result),
287		autorest.ByClosing())
288	result.Response = autorest.Response{Response: resp}
289	return
290}
291
292// listByServiceNextResults retrieves the next set of results, if any.
293func (client NotificationClient) listByServiceNextResults(lastResults NotificationCollection) (result NotificationCollection, err error) {
294	req, err := lastResults.notificationCollectionPreparer()
295	if err != nil {
296		return result, autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "listByServiceNextResults", nil, "Failure preparing next results request")
297	}
298	if req == nil {
299		return
300	}
301	resp, err := client.ListByServiceSender(req)
302	if err != nil {
303		result.Response = autorest.Response{Response: resp}
304		return result, autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "listByServiceNextResults", resp, "Failure sending next results request")
305	}
306	result, err = client.ListByServiceResponder(resp)
307	if err != nil {
308		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "listByServiceNextResults", resp, "Failure responding to next results request")
309	}
310	return
311}
312
313// ListByServiceComplete enumerates all values, automatically crossing page boundaries as required.
314func (client NotificationClient) ListByServiceComplete(ctx context.Context, resourceGroupName string, serviceName string, top *int32, skip *int32) (result NotificationCollectionIterator, err error) {
315	result.page, err = client.ListByService(ctx, resourceGroupName, serviceName, top, skip)
316	return
317}
318