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	"github.com/Azure/go-autorest/tracing"
26	"net/http"
27)
28
29// NotificationClient is the apiManagement Client
30type NotificationClient struct {
31	BaseClient
32}
33
34// NewNotificationClient creates an instance of the NotificationClient client.
35func NewNotificationClient(subscriptionID string) NotificationClient {
36	return NewNotificationClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewNotificationClientWithBaseURI creates an instance of the NotificationClient client.
40func NewNotificationClientWithBaseURI(baseURI string, subscriptionID string) NotificationClient {
41	return NotificationClient{NewWithBaseURI(baseURI, subscriptionID)}
42}
43
44// CreateOrUpdate create or Update API Management publisher notification.
45// Parameters:
46// resourceGroupName - the name of the resource group.
47// serviceName - the name of the API Management service.
48// notificationName - notification Name Identifier.
49// ifMatch - eTag of the Entity. Not required when creating an entity, but required when updating an entity.
50func (client NotificationClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, ifMatch string) (result NotificationContract, err error) {
51	if tracing.IsEnabled() {
52		ctx = tracing.StartSpan(ctx, fqdn+"/NotificationClient.CreateOrUpdate")
53		defer func() {
54			sc := -1
55			if result.Response.Response != nil {
56				sc = result.Response.Response.StatusCode
57			}
58			tracing.EndSpan(ctx, sc, err)
59		}()
60	}
61	if err := validation.Validate([]validation.Validation{
62		{TargetValue: serviceName,
63			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
64				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
65				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
66		return result, validation.NewError("apimanagement.NotificationClient", "CreateOrUpdate", err.Error())
67	}
68
69	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serviceName, notificationName, ifMatch)
70	if err != nil {
71		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "CreateOrUpdate", nil, "Failure preparing request")
72		return
73	}
74
75	resp, err := client.CreateOrUpdateSender(req)
76	if err != nil {
77		result.Response = autorest.Response{Response: resp}
78		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "CreateOrUpdate", resp, "Failure sending request")
79		return
80	}
81
82	result, err = client.CreateOrUpdateResponder(resp)
83	if err != nil {
84		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "CreateOrUpdate", resp, "Failure responding to request")
85	}
86
87	return
88}
89
90// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
91func (client NotificationClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, ifMatch string) (*http.Request, error) {
92	pathParameters := map[string]interface{}{
93		"notificationName":  autorest.Encode("path", notificationName),
94		"resourceGroupName": autorest.Encode("path", resourceGroupName),
95		"serviceName":       autorest.Encode("path", serviceName),
96		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
97	}
98
99	const APIVersion = "2019-01-01"
100	queryParameters := map[string]interface{}{
101		"api-version": APIVersion,
102	}
103
104	preparer := autorest.CreatePreparer(
105		autorest.AsPut(),
106		autorest.WithBaseURL(client.BaseURI),
107		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}", pathParameters),
108		autorest.WithQueryParameters(queryParameters))
109	if len(ifMatch) > 0 {
110		preparer = autorest.DecoratePreparer(preparer,
111			autorest.WithHeader("If-Match", autorest.String(ifMatch)))
112	}
113	return preparer.Prepare((&http.Request{}).WithContext(ctx))
114}
115
116// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
117// http.Response Body if it receives an error.
118func (client NotificationClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
119	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
120	return autorest.SendWithSender(client, req, sd...)
121}
122
123// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
124// closes the http.Response Body.
125func (client NotificationClient) CreateOrUpdateResponder(resp *http.Response) (result NotificationContract, err error) {
126	err = autorest.Respond(
127		resp,
128		client.ByInspecting(),
129		azure.WithErrorUnlessStatusCode(http.StatusOK),
130		autorest.ByUnmarshallingJSON(&result),
131		autorest.ByClosing())
132	result.Response = autorest.Response{Response: resp}
133	return
134}
135
136// Get gets the details of the Notification specified by its identifier.
137// Parameters:
138// resourceGroupName - the name of the resource group.
139// serviceName - the name of the API Management service.
140// notificationName - notification Name Identifier.
141func (client NotificationClient) Get(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName) (result NotificationContract, err error) {
142	if tracing.IsEnabled() {
143		ctx = tracing.StartSpan(ctx, fqdn+"/NotificationClient.Get")
144		defer func() {
145			sc := -1
146			if result.Response.Response != nil {
147				sc = result.Response.Response.StatusCode
148			}
149			tracing.EndSpan(ctx, sc, err)
150		}()
151	}
152	if err := validation.Validate([]validation.Validation{
153		{TargetValue: serviceName,
154			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
155				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
156				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
157		return result, validation.NewError("apimanagement.NotificationClient", "Get", err.Error())
158	}
159
160	req, err := client.GetPreparer(ctx, resourceGroupName, serviceName, notificationName)
161	if err != nil {
162		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "Get", nil, "Failure preparing request")
163		return
164	}
165
166	resp, err := client.GetSender(req)
167	if err != nil {
168		result.Response = autorest.Response{Response: resp}
169		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "Get", resp, "Failure sending request")
170		return
171	}
172
173	result, err = client.GetResponder(resp)
174	if err != nil {
175		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "Get", resp, "Failure responding to request")
176	}
177
178	return
179}
180
181// GetPreparer prepares the Get request.
182func (client NotificationClient) GetPreparer(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName) (*http.Request, error) {
183	pathParameters := map[string]interface{}{
184		"notificationName":  autorest.Encode("path", notificationName),
185		"resourceGroupName": autorest.Encode("path", resourceGroupName),
186		"serviceName":       autorest.Encode("path", serviceName),
187		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
188	}
189
190	const APIVersion = "2019-01-01"
191	queryParameters := map[string]interface{}{
192		"api-version": APIVersion,
193	}
194
195	preparer := autorest.CreatePreparer(
196		autorest.AsGet(),
197		autorest.WithBaseURL(client.BaseURI),
198		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}", pathParameters),
199		autorest.WithQueryParameters(queryParameters))
200	return preparer.Prepare((&http.Request{}).WithContext(ctx))
201}
202
203// GetSender sends the Get request. The method will close the
204// http.Response Body if it receives an error.
205func (client NotificationClient) GetSender(req *http.Request) (*http.Response, error) {
206	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
207	return autorest.SendWithSender(client, req, sd...)
208}
209
210// GetResponder handles the response to the Get request. The method always
211// closes the http.Response Body.
212func (client NotificationClient) GetResponder(resp *http.Response) (result NotificationContract, err error) {
213	err = autorest.Respond(
214		resp,
215		client.ByInspecting(),
216		azure.WithErrorUnlessStatusCode(http.StatusOK),
217		autorest.ByUnmarshallingJSON(&result),
218		autorest.ByClosing())
219	result.Response = autorest.Response{Response: resp}
220	return
221}
222
223// ListByService lists a collection of properties defined within a service instance.
224// Parameters:
225// resourceGroupName - the name of the resource group.
226// serviceName - the name of the API Management service.
227// top - number of records to return.
228// skip - number of records to skip.
229func (client NotificationClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, top *int32, skip *int32) (result NotificationCollectionPage, err error) {
230	if tracing.IsEnabled() {
231		ctx = tracing.StartSpan(ctx, fqdn+"/NotificationClient.ListByService")
232		defer func() {
233			sc := -1
234			if result.nc.Response.Response != nil {
235				sc = result.nc.Response.Response.StatusCode
236			}
237			tracing.EndSpan(ctx, sc, err)
238		}()
239	}
240	if err := validation.Validate([]validation.Validation{
241		{TargetValue: serviceName,
242			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
243				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
244				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
245		{TargetValue: top,
246			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
247				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: 1, Chain: nil}}}}},
248		{TargetValue: skip,
249			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
250				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: 0, Chain: nil}}}}}}); err != nil {
251		return result, validation.NewError("apimanagement.NotificationClient", "ListByService", err.Error())
252	}
253
254	result.fn = client.listByServiceNextResults
255	req, err := client.ListByServicePreparer(ctx, resourceGroupName, serviceName, top, skip)
256	if err != nil {
257		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "ListByService", nil, "Failure preparing request")
258		return
259	}
260
261	resp, err := client.ListByServiceSender(req)
262	if err != nil {
263		result.nc.Response = autorest.Response{Response: resp}
264		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "ListByService", resp, "Failure sending request")
265		return
266	}
267
268	result.nc, err = client.ListByServiceResponder(resp)
269	if err != nil {
270		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "ListByService", resp, "Failure responding to request")
271	}
272
273	return
274}
275
276// ListByServicePreparer prepares the ListByService request.
277func (client NotificationClient) ListByServicePreparer(ctx context.Context, resourceGroupName string, serviceName string, top *int32, skip *int32) (*http.Request, error) {
278	pathParameters := map[string]interface{}{
279		"resourceGroupName": autorest.Encode("path", resourceGroupName),
280		"serviceName":       autorest.Encode("path", serviceName),
281		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
282	}
283
284	const APIVersion = "2019-01-01"
285	queryParameters := map[string]interface{}{
286		"api-version": APIVersion,
287	}
288	if top != nil {
289		queryParameters["$top"] = autorest.Encode("query", *top)
290	}
291	if skip != nil {
292		queryParameters["$skip"] = autorest.Encode("query", *skip)
293	}
294
295	preparer := autorest.CreatePreparer(
296		autorest.AsGet(),
297		autorest.WithBaseURL(client.BaseURI),
298		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications", pathParameters),
299		autorest.WithQueryParameters(queryParameters))
300	return preparer.Prepare((&http.Request{}).WithContext(ctx))
301}
302
303// ListByServiceSender sends the ListByService request. The method will close the
304// http.Response Body if it receives an error.
305func (client NotificationClient) ListByServiceSender(req *http.Request) (*http.Response, error) {
306	sd := autorest.GetSendDecorators(req.Context(), azure.DoRetryWithRegistration(client.Client))
307	return autorest.SendWithSender(client, req, sd...)
308}
309
310// ListByServiceResponder handles the response to the ListByService request. The method always
311// closes the http.Response Body.
312func (client NotificationClient) ListByServiceResponder(resp *http.Response) (result NotificationCollection, err error) {
313	err = autorest.Respond(
314		resp,
315		client.ByInspecting(),
316		azure.WithErrorUnlessStatusCode(http.StatusOK),
317		autorest.ByUnmarshallingJSON(&result),
318		autorest.ByClosing())
319	result.Response = autorest.Response{Response: resp}
320	return
321}
322
323// listByServiceNextResults retrieves the next set of results, if any.
324func (client NotificationClient) listByServiceNextResults(ctx context.Context, lastResults NotificationCollection) (result NotificationCollection, err error) {
325	req, err := lastResults.notificationCollectionPreparer(ctx)
326	if err != nil {
327		return result, autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "listByServiceNextResults", nil, "Failure preparing next results request")
328	}
329	if req == nil {
330		return
331	}
332	resp, err := client.ListByServiceSender(req)
333	if err != nil {
334		result.Response = autorest.Response{Response: resp}
335		return result, autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "listByServiceNextResults", resp, "Failure sending next results request")
336	}
337	result, err = client.ListByServiceResponder(resp)
338	if err != nil {
339		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "listByServiceNextResults", resp, "Failure responding to next results request")
340	}
341	return
342}
343
344// ListByServiceComplete enumerates all values, automatically crossing page boundaries as required.
345func (client NotificationClient) ListByServiceComplete(ctx context.Context, resourceGroupName string, serviceName string, top *int32, skip *int32) (result NotificationCollectionIterator, err error) {
346	if tracing.IsEnabled() {
347		ctx = tracing.StartSpan(ctx, fqdn+"/NotificationClient.ListByService")
348		defer func() {
349			sc := -1
350			if result.Response().Response.Response != nil {
351				sc = result.page.Response().Response.Response.StatusCode
352			}
353			tracing.EndSpan(ctx, sc, err)
354		}()
355	}
356	result.page, err = client.ListByService(ctx, resourceGroupName, serviceName, top, skip)
357	return
358}
359