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 using a custom endpoint.  Use
40// this when interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
41func NewNotificationClientWithBaseURI(baseURI string, subscriptionID string) NotificationClient {
42	return NotificationClient{NewWithBaseURI(baseURI, subscriptionID)}
43}
44
45// CreateOrUpdate create or Update API Management publisher notification.
46// Parameters:
47// resourceGroupName - the name of the resource group.
48// serviceName - the name of the API Management service.
49// notificationName - notification Name Identifier.
50// ifMatch - eTag of the Entity. Not required when creating an entity, but required when updating an entity.
51func (client NotificationClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, ifMatch string) (result NotificationContract, err error) {
52	if tracing.IsEnabled() {
53		ctx = tracing.StartSpan(ctx, fqdn+"/NotificationClient.CreateOrUpdate")
54		defer func() {
55			sc := -1
56			if result.Response.Response != nil {
57				sc = result.Response.Response.StatusCode
58			}
59			tracing.EndSpan(ctx, sc, err)
60		}()
61	}
62	if err := validation.Validate([]validation.Validation{
63		{TargetValue: serviceName,
64			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
65				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
66				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}}}); err != nil {
67		return result, validation.NewError("apimanagement.NotificationClient", "CreateOrUpdate", err.Error())
68	}
69
70	req, err := client.CreateOrUpdatePreparer(ctx, resourceGroupName, serviceName, notificationName, ifMatch)
71	if err != nil {
72		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "CreateOrUpdate", nil, "Failure preparing request")
73		return
74	}
75
76	resp, err := client.CreateOrUpdateSender(req)
77	if err != nil {
78		result.Response = autorest.Response{Response: resp}
79		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "CreateOrUpdate", resp, "Failure sending request")
80		return
81	}
82
83	result, err = client.CreateOrUpdateResponder(resp)
84	if err != nil {
85		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "CreateOrUpdate", resp, "Failure responding to request")
86	}
87
88	return
89}
90
91// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
92func (client NotificationClient) CreateOrUpdatePreparer(ctx context.Context, resourceGroupName string, serviceName string, notificationName NotificationName, ifMatch string) (*http.Request, error) {
93	pathParameters := map[string]interface{}{
94		"notificationName":  autorest.Encode("path", notificationName),
95		"resourceGroupName": autorest.Encode("path", resourceGroupName),
96		"serviceName":       autorest.Encode("path", serviceName),
97		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
98	}
99
100	const APIVersion = "2018-06-01-preview"
101	queryParameters := map[string]interface{}{
102		"api-version": APIVersion,
103	}
104
105	preparer := autorest.CreatePreparer(
106		autorest.AsPut(),
107		autorest.WithBaseURL(client.BaseURI),
108		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications/{notificationName}", pathParameters),
109		autorest.WithQueryParameters(queryParameters))
110	if len(ifMatch) > 0 {
111		preparer = autorest.DecoratePreparer(preparer,
112			autorest.WithHeader("If-Match", autorest.String(ifMatch)))
113	}
114	return preparer.Prepare((&http.Request{}).WithContext(ctx))
115}
116
117// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
118// http.Response Body if it receives an error.
119func (client NotificationClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
120	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
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 = "2018-06-01-preview"
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	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
207}
208
209// GetResponder handles the response to the Get request. The method always
210// closes the http.Response Body.
211func (client NotificationClient) GetResponder(resp *http.Response) (result NotificationContract, err error) {
212	err = autorest.Respond(
213		resp,
214		client.ByInspecting(),
215		azure.WithErrorUnlessStatusCode(http.StatusOK),
216		autorest.ByUnmarshallingJSON(&result),
217		autorest.ByClosing())
218	result.Response = autorest.Response{Response: resp}
219	return
220}
221
222// ListByService lists a collection of properties defined within a service instance.
223// Parameters:
224// resourceGroupName - the name of the resource group.
225// serviceName - the name of the API Management service.
226// top - number of records to return.
227// skip - number of records to skip.
228func (client NotificationClient) ListByService(ctx context.Context, resourceGroupName string, serviceName string, top *int32, skip *int32) (result NotificationCollectionPage, err error) {
229	if tracing.IsEnabled() {
230		ctx = tracing.StartSpan(ctx, fqdn+"/NotificationClient.ListByService")
231		defer func() {
232			sc := -1
233			if result.nc.Response.Response != nil {
234				sc = result.nc.Response.Response.StatusCode
235			}
236			tracing.EndSpan(ctx, sc, err)
237		}()
238	}
239	if err := validation.Validate([]validation.Validation{
240		{TargetValue: serviceName,
241			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
242				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
243				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
244		{TargetValue: top,
245			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
246				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil}}}}},
247		{TargetValue: skip,
248			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
249				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: int64(0), Chain: nil}}}}}}); err != nil {
250		return result, validation.NewError("apimanagement.NotificationClient", "ListByService", err.Error())
251	}
252
253	result.fn = client.listByServiceNextResults
254	req, err := client.ListByServicePreparer(ctx, resourceGroupName, serviceName, top, skip)
255	if err != nil {
256		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "ListByService", nil, "Failure preparing request")
257		return
258	}
259
260	resp, err := client.ListByServiceSender(req)
261	if err != nil {
262		result.nc.Response = autorest.Response{Response: resp}
263		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "ListByService", resp, "Failure sending request")
264		return
265	}
266
267	result.nc, err = client.ListByServiceResponder(resp)
268	if err != nil {
269		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "ListByService", resp, "Failure responding to request")
270	}
271
272	return
273}
274
275// ListByServicePreparer prepares the ListByService request.
276func (client NotificationClient) ListByServicePreparer(ctx context.Context, resourceGroupName string, serviceName string, top *int32, skip *int32) (*http.Request, error) {
277	pathParameters := map[string]interface{}{
278		"resourceGroupName": autorest.Encode("path", resourceGroupName),
279		"serviceName":       autorest.Encode("path", serviceName),
280		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
281	}
282
283	const APIVersion = "2018-06-01-preview"
284	queryParameters := map[string]interface{}{
285		"api-version": APIVersion,
286	}
287	if top != nil {
288		queryParameters["$top"] = autorest.Encode("query", *top)
289	}
290	if skip != nil {
291		queryParameters["$skip"] = autorest.Encode("query", *skip)
292	}
293
294	preparer := autorest.CreatePreparer(
295		autorest.AsGet(),
296		autorest.WithBaseURL(client.BaseURI),
297		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/notifications", pathParameters),
298		autorest.WithQueryParameters(queryParameters))
299	return preparer.Prepare((&http.Request{}).WithContext(ctx))
300}
301
302// ListByServiceSender sends the ListByService request. The method will close the
303// http.Response Body if it receives an error.
304func (client NotificationClient) ListByServiceSender(req *http.Request) (*http.Response, error) {
305	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
306}
307
308// ListByServiceResponder handles the response to the ListByService request. The method always
309// closes the http.Response Body.
310func (client NotificationClient) ListByServiceResponder(resp *http.Response) (result NotificationCollection, err error) {
311	err = autorest.Respond(
312		resp,
313		client.ByInspecting(),
314		azure.WithErrorUnlessStatusCode(http.StatusOK),
315		autorest.ByUnmarshallingJSON(&result),
316		autorest.ByClosing())
317	result.Response = autorest.Response{Response: resp}
318	return
319}
320
321// listByServiceNextResults retrieves the next set of results, if any.
322func (client NotificationClient) listByServiceNextResults(ctx context.Context, lastResults NotificationCollection) (result NotificationCollection, err error) {
323	req, err := lastResults.notificationCollectionPreparer(ctx)
324	if err != nil {
325		return result, autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "listByServiceNextResults", nil, "Failure preparing next results request")
326	}
327	if req == nil {
328		return
329	}
330	resp, err := client.ListByServiceSender(req)
331	if err != nil {
332		result.Response = autorest.Response{Response: resp}
333		return result, autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "listByServiceNextResults", resp, "Failure sending next results request")
334	}
335	result, err = client.ListByServiceResponder(resp)
336	if err != nil {
337		err = autorest.NewErrorWithError(err, "apimanagement.NotificationClient", "listByServiceNextResults", resp, "Failure responding to next results request")
338	}
339	return
340}
341
342// ListByServiceComplete enumerates all values, automatically crossing page boundaries as required.
343func (client NotificationClient) ListByServiceComplete(ctx context.Context, resourceGroupName string, serviceName string, top *int32, skip *int32) (result NotificationCollectionIterator, err error) {
344	if tracing.IsEnabled() {
345		ctx = tracing.StartSpan(ctx, fqdn+"/NotificationClient.ListByService")
346		defer func() {
347			sc := -1
348			if result.Response().Response.Response != nil {
349				sc = result.page.Response().Response.Response.StatusCode
350			}
351			tracing.EndSpan(ctx, sc, err)
352		}()
353	}
354	result.page, err = client.ListByService(ctx, resourceGroupName, serviceName, top, skip)
355	return
356}
357