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// LoggerClient is the client for the Logger methods of the Apimanagement service.
30type LoggerClient struct {
31	BaseClient
32}
33
34// NewLoggerClient creates an instance of the LoggerClient client.
35func NewLoggerClient() LoggerClient {
36	return LoggerClient{New()}
37}
38
39// CreateOrUpdate creates or Updates a logger.
40// Parameters:
41// apimBaseURL - the management endpoint of the API Management service, for example
42// https://myapimservice.management.azure-api.net.
43// loggerid - logger identifier. Must be unique in the API Management service instance.
44// parameters - create parameters.
45func (client LoggerClient) CreateOrUpdate(ctx context.Context, apimBaseURL string, loggerid string, parameters LoggerContract) (result LoggerContract, err error) {
46	if tracing.IsEnabled() {
47		ctx = tracing.StartSpan(ctx, fqdn+"/LoggerClient.CreateOrUpdate")
48		defer func() {
49			sc := -1
50			if result.Response.Response != nil {
51				sc = result.Response.Response.StatusCode
52			}
53			tracing.EndSpan(ctx, sc, err)
54		}()
55	}
56	if err := validation.Validate([]validation.Validation{
57		{TargetValue: loggerid,
58			Constraints: []validation.Constraint{{Target: "loggerid", Name: validation.MaxLength, Rule: 256, Chain: nil},
59				{Target: "loggerid", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
60		return result, validation.NewError("apimanagement.LoggerClient", "CreateOrUpdate", err.Error())
61	}
62
63	req, err := client.CreateOrUpdatePreparer(ctx, apimBaseURL, loggerid, parameters)
64	if err != nil {
65		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "CreateOrUpdate", nil, "Failure preparing request")
66		return
67	}
68
69	resp, err := client.CreateOrUpdateSender(req)
70	if err != nil {
71		result.Response = autorest.Response{Response: resp}
72		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "CreateOrUpdate", resp, "Failure sending request")
73		return
74	}
75
76	result, err = client.CreateOrUpdateResponder(resp)
77	if err != nil {
78		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "CreateOrUpdate", resp, "Failure responding to request")
79		return
80	}
81
82	return
83}
84
85// CreateOrUpdatePreparer prepares the CreateOrUpdate request.
86func (client LoggerClient) CreateOrUpdatePreparer(ctx context.Context, apimBaseURL string, loggerid string, parameters LoggerContract) (*http.Request, error) {
87	urlParameters := map[string]interface{}{
88		"apimBaseUrl": apimBaseURL,
89	}
90
91	pathParameters := map[string]interface{}{
92		"loggerid": autorest.Encode("path", loggerid),
93	}
94
95	const APIVersion = "2017-03-01"
96	queryParameters := map[string]interface{}{
97		"api-version": APIVersion,
98	}
99
100	preparer := autorest.CreatePreparer(
101		autorest.AsContentType("application/json; charset=utf-8"),
102		autorest.AsPut(),
103		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
104		autorest.WithPathParameters("/loggers/{loggerid}", pathParameters),
105		autorest.WithJSON(parameters),
106		autorest.WithQueryParameters(queryParameters))
107	return preparer.Prepare((&http.Request{}).WithContext(ctx))
108}
109
110// CreateOrUpdateSender sends the CreateOrUpdate request. The method will close the
111// http.Response Body if it receives an error.
112func (client LoggerClient) CreateOrUpdateSender(req *http.Request) (*http.Response, error) {
113	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
114}
115
116// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always
117// closes the http.Response Body.
118func (client LoggerClient) CreateOrUpdateResponder(resp *http.Response) (result LoggerContract, err error) {
119	err = autorest.Respond(
120		resp,
121		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
122		autorest.ByUnmarshallingJSON(&result),
123		autorest.ByClosing())
124	result.Response = autorest.Response{Response: resp}
125	return
126}
127
128// Delete deletes the specified logger.
129// Parameters:
130// apimBaseURL - the management endpoint of the API Management service, for example
131// https://myapimservice.management.azure-api.net.
132// loggerid - logger identifier. Must be unique in the API Management service instance.
133// ifMatch - the entity state (Etag) version of the logger to delete. A value of "*" can be used for If-Match
134// to unconditionally apply the operation.
135func (client LoggerClient) Delete(ctx context.Context, apimBaseURL string, loggerid string, ifMatch string) (result autorest.Response, err error) {
136	if tracing.IsEnabled() {
137		ctx = tracing.StartSpan(ctx, fqdn+"/LoggerClient.Delete")
138		defer func() {
139			sc := -1
140			if result.Response != nil {
141				sc = result.Response.StatusCode
142			}
143			tracing.EndSpan(ctx, sc, err)
144		}()
145	}
146	if err := validation.Validate([]validation.Validation{
147		{TargetValue: loggerid,
148			Constraints: []validation.Constraint{{Target: "loggerid", Name: validation.MaxLength, Rule: 256, Chain: nil},
149				{Target: "loggerid", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
150		return result, validation.NewError("apimanagement.LoggerClient", "Delete", err.Error())
151	}
152
153	req, err := client.DeletePreparer(ctx, apimBaseURL, loggerid, ifMatch)
154	if err != nil {
155		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "Delete", nil, "Failure preparing request")
156		return
157	}
158
159	resp, err := client.DeleteSender(req)
160	if err != nil {
161		result.Response = resp
162		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "Delete", resp, "Failure sending request")
163		return
164	}
165
166	result, err = client.DeleteResponder(resp)
167	if err != nil {
168		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "Delete", resp, "Failure responding to request")
169		return
170	}
171
172	return
173}
174
175// DeletePreparer prepares the Delete request.
176func (client LoggerClient) DeletePreparer(ctx context.Context, apimBaseURL string, loggerid string, ifMatch string) (*http.Request, error) {
177	urlParameters := map[string]interface{}{
178		"apimBaseUrl": apimBaseURL,
179	}
180
181	pathParameters := map[string]interface{}{
182		"loggerid": autorest.Encode("path", loggerid),
183	}
184
185	const APIVersion = "2017-03-01"
186	queryParameters := map[string]interface{}{
187		"api-version": APIVersion,
188	}
189
190	preparer := autorest.CreatePreparer(
191		autorest.AsDelete(),
192		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
193		autorest.WithPathParameters("/loggers/{loggerid}", pathParameters),
194		autorest.WithQueryParameters(queryParameters),
195		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
196	return preparer.Prepare((&http.Request{}).WithContext(ctx))
197}
198
199// DeleteSender sends the Delete request. The method will close the
200// http.Response Body if it receives an error.
201func (client LoggerClient) DeleteSender(req *http.Request) (*http.Response, error) {
202	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
203}
204
205// DeleteResponder handles the response to the Delete request. The method always
206// closes the http.Response Body.
207func (client LoggerClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) {
208	err = autorest.Respond(
209		resp,
210		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
211		autorest.ByClosing())
212	result.Response = resp
213	return
214}
215
216// Get gets the details of the logger specified by its identifier.
217// Parameters:
218// apimBaseURL - the management endpoint of the API Management service, for example
219// https://myapimservice.management.azure-api.net.
220// loggerid - logger identifier. Must be unique in the API Management service instance.
221func (client LoggerClient) Get(ctx context.Context, apimBaseURL string, loggerid string) (result LoggerContract, err error) {
222	if tracing.IsEnabled() {
223		ctx = tracing.StartSpan(ctx, fqdn+"/LoggerClient.Get")
224		defer func() {
225			sc := -1
226			if result.Response.Response != nil {
227				sc = result.Response.Response.StatusCode
228			}
229			tracing.EndSpan(ctx, sc, err)
230		}()
231	}
232	if err := validation.Validate([]validation.Validation{
233		{TargetValue: loggerid,
234			Constraints: []validation.Constraint{{Target: "loggerid", Name: validation.MaxLength, Rule: 256, Chain: nil},
235				{Target: "loggerid", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
236		return result, validation.NewError("apimanagement.LoggerClient", "Get", err.Error())
237	}
238
239	req, err := client.GetPreparer(ctx, apimBaseURL, loggerid)
240	if err != nil {
241		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "Get", nil, "Failure preparing request")
242		return
243	}
244
245	resp, err := client.GetSender(req)
246	if err != nil {
247		result.Response = autorest.Response{Response: resp}
248		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "Get", resp, "Failure sending request")
249		return
250	}
251
252	result, err = client.GetResponder(resp)
253	if err != nil {
254		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "Get", resp, "Failure responding to request")
255		return
256	}
257
258	return
259}
260
261// GetPreparer prepares the Get request.
262func (client LoggerClient) GetPreparer(ctx context.Context, apimBaseURL string, loggerid string) (*http.Request, error) {
263	urlParameters := map[string]interface{}{
264		"apimBaseUrl": apimBaseURL,
265	}
266
267	pathParameters := map[string]interface{}{
268		"loggerid": autorest.Encode("path", loggerid),
269	}
270
271	const APIVersion = "2017-03-01"
272	queryParameters := map[string]interface{}{
273		"api-version": APIVersion,
274	}
275
276	preparer := autorest.CreatePreparer(
277		autorest.AsGet(),
278		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
279		autorest.WithPathParameters("/loggers/{loggerid}", pathParameters),
280		autorest.WithQueryParameters(queryParameters))
281	return preparer.Prepare((&http.Request{}).WithContext(ctx))
282}
283
284// GetSender sends the Get request. The method will close the
285// http.Response Body if it receives an error.
286func (client LoggerClient) GetSender(req *http.Request) (*http.Response, error) {
287	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
288}
289
290// GetResponder handles the response to the Get request. The method always
291// closes the http.Response Body.
292func (client LoggerClient) GetResponder(resp *http.Response) (result LoggerContract, err error) {
293	err = autorest.Respond(
294		resp,
295		azure.WithErrorUnlessStatusCode(http.StatusOK),
296		autorest.ByUnmarshallingJSON(&result),
297		autorest.ByClosing())
298	result.Response = autorest.Response{Response: resp}
299	return
300}
301
302// List lists a collection of loggers in the specified service instance.
303// Parameters:
304// apimBaseURL - the management endpoint of the API Management service, for example
305// https://myapimservice.management.azure-api.net.
306// filter - | Field | Supported operators    | Supported functions                         |
307// |-------|------------------------|---------------------------------------------|
308// | id    | ge, le, eq, ne, gt, lt | substringof, contains, startswith, endswith |
309// | type  | eq                     |                                             |
310// top - number of records to return.
311// skip - number of records to skip.
312func (client LoggerClient) List(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (result LoggerCollectionPage, err error) {
313	if tracing.IsEnabled() {
314		ctx = tracing.StartSpan(ctx, fqdn+"/LoggerClient.List")
315		defer func() {
316			sc := -1
317			if result.lc.Response.Response != nil {
318				sc = result.lc.Response.Response.StatusCode
319			}
320			tracing.EndSpan(ctx, sc, err)
321		}()
322	}
323	if err := validation.Validate([]validation.Validation{
324		{TargetValue: top,
325			Constraints: []validation.Constraint{{Target: "top", Name: validation.Null, Rule: false,
326				Chain: []validation.Constraint{{Target: "top", Name: validation.InclusiveMinimum, Rule: int64(1), Chain: nil}}}}},
327		{TargetValue: skip,
328			Constraints: []validation.Constraint{{Target: "skip", Name: validation.Null, Rule: false,
329				Chain: []validation.Constraint{{Target: "skip", Name: validation.InclusiveMinimum, Rule: int64(0), Chain: nil}}}}}}); err != nil {
330		return result, validation.NewError("apimanagement.LoggerClient", "List", err.Error())
331	}
332
333	result.fn = client.listNextResults
334	req, err := client.ListPreparer(ctx, apimBaseURL, filter, top, skip)
335	if err != nil {
336		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "List", nil, "Failure preparing request")
337		return
338	}
339
340	resp, err := client.ListSender(req)
341	if err != nil {
342		result.lc.Response = autorest.Response{Response: resp}
343		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "List", resp, "Failure sending request")
344		return
345	}
346
347	result.lc, err = client.ListResponder(resp)
348	if err != nil {
349		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "List", resp, "Failure responding to request")
350		return
351	}
352	if result.lc.hasNextLink() && result.lc.IsEmpty() {
353		err = result.NextWithContext(ctx)
354		return
355	}
356
357	return
358}
359
360// ListPreparer prepares the List request.
361func (client LoggerClient) ListPreparer(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (*http.Request, error) {
362	urlParameters := map[string]interface{}{
363		"apimBaseUrl": apimBaseURL,
364	}
365
366	const APIVersion = "2017-03-01"
367	queryParameters := map[string]interface{}{
368		"api-version": APIVersion,
369	}
370	if len(filter) > 0 {
371		queryParameters["$filter"] = autorest.Encode("query", filter)
372	}
373	if top != nil {
374		queryParameters["$top"] = autorest.Encode("query", *top)
375	}
376	if skip != nil {
377		queryParameters["$skip"] = autorest.Encode("query", *skip)
378	}
379
380	preparer := autorest.CreatePreparer(
381		autorest.AsGet(),
382		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
383		autorest.WithPath("/loggers"),
384		autorest.WithQueryParameters(queryParameters))
385	return preparer.Prepare((&http.Request{}).WithContext(ctx))
386}
387
388// ListSender sends the List request. The method will close the
389// http.Response Body if it receives an error.
390func (client LoggerClient) ListSender(req *http.Request) (*http.Response, error) {
391	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
392}
393
394// ListResponder handles the response to the List request. The method always
395// closes the http.Response Body.
396func (client LoggerClient) ListResponder(resp *http.Response) (result LoggerCollection, err error) {
397	err = autorest.Respond(
398		resp,
399		azure.WithErrorUnlessStatusCode(http.StatusOK),
400		autorest.ByUnmarshallingJSON(&result),
401		autorest.ByClosing())
402	result.Response = autorest.Response{Response: resp}
403	return
404}
405
406// listNextResults retrieves the next set of results, if any.
407func (client LoggerClient) listNextResults(ctx context.Context, lastResults LoggerCollection) (result LoggerCollection, err error) {
408	req, err := lastResults.loggerCollectionPreparer(ctx)
409	if err != nil {
410		return result, autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "listNextResults", nil, "Failure preparing next results request")
411	}
412	if req == nil {
413		return
414	}
415	resp, err := client.ListSender(req)
416	if err != nil {
417		result.Response = autorest.Response{Response: resp}
418		return result, autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "listNextResults", resp, "Failure sending next results request")
419	}
420	result, err = client.ListResponder(resp)
421	if err != nil {
422		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "listNextResults", resp, "Failure responding to next results request")
423	}
424	return
425}
426
427// ListComplete enumerates all values, automatically crossing page boundaries as required.
428func (client LoggerClient) ListComplete(ctx context.Context, apimBaseURL string, filter string, top *int32, skip *int32) (result LoggerCollectionIterator, err error) {
429	if tracing.IsEnabled() {
430		ctx = tracing.StartSpan(ctx, fqdn+"/LoggerClient.List")
431		defer func() {
432			sc := -1
433			if result.Response().Response.Response != nil {
434				sc = result.page.Response().Response.Response.StatusCode
435			}
436			tracing.EndSpan(ctx, sc, err)
437		}()
438	}
439	result.page, err = client.List(ctx, apimBaseURL, filter, top, skip)
440	return
441}
442
443// Update updates an existing logger.
444// Parameters:
445// apimBaseURL - the management endpoint of the API Management service, for example
446// https://myapimservice.management.azure-api.net.
447// loggerid - logger identifier. Must be unique in the API Management service instance.
448// parameters - update parameters.
449// ifMatch - the entity state (Etag) version of the logger to update. A value of "*" can be used for If-Match
450// to unconditionally apply the operation.
451func (client LoggerClient) Update(ctx context.Context, apimBaseURL string, loggerid string, parameters LoggerUpdateContract, ifMatch string) (result autorest.Response, err error) {
452	if tracing.IsEnabled() {
453		ctx = tracing.StartSpan(ctx, fqdn+"/LoggerClient.Update")
454		defer func() {
455			sc := -1
456			if result.Response != nil {
457				sc = result.Response.StatusCode
458			}
459			tracing.EndSpan(ctx, sc, err)
460		}()
461	}
462	if err := validation.Validate([]validation.Validation{
463		{TargetValue: loggerid,
464			Constraints: []validation.Constraint{{Target: "loggerid", Name: validation.MaxLength, Rule: 256, Chain: nil},
465				{Target: "loggerid", Name: validation.Pattern, Rule: `^[^*#&+:<>?]+$`, Chain: nil}}}}); err != nil {
466		return result, validation.NewError("apimanagement.LoggerClient", "Update", err.Error())
467	}
468
469	req, err := client.UpdatePreparer(ctx, apimBaseURL, loggerid, parameters, ifMatch)
470	if err != nil {
471		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "Update", nil, "Failure preparing request")
472		return
473	}
474
475	resp, err := client.UpdateSender(req)
476	if err != nil {
477		result.Response = resp
478		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "Update", resp, "Failure sending request")
479		return
480	}
481
482	result, err = client.UpdateResponder(resp)
483	if err != nil {
484		err = autorest.NewErrorWithError(err, "apimanagement.LoggerClient", "Update", resp, "Failure responding to request")
485		return
486	}
487
488	return
489}
490
491// UpdatePreparer prepares the Update request.
492func (client LoggerClient) UpdatePreparer(ctx context.Context, apimBaseURL string, loggerid string, parameters LoggerUpdateContract, ifMatch string) (*http.Request, error) {
493	urlParameters := map[string]interface{}{
494		"apimBaseUrl": apimBaseURL,
495	}
496
497	pathParameters := map[string]interface{}{
498		"loggerid": autorest.Encode("path", loggerid),
499	}
500
501	const APIVersion = "2017-03-01"
502	queryParameters := map[string]interface{}{
503		"api-version": APIVersion,
504	}
505
506	preparer := autorest.CreatePreparer(
507		autorest.AsContentType("application/json; charset=utf-8"),
508		autorest.AsPatch(),
509		autorest.WithCustomBaseURL("{apimBaseUrl}", urlParameters),
510		autorest.WithPathParameters("/loggers/{loggerid}", pathParameters),
511		autorest.WithJSON(parameters),
512		autorest.WithQueryParameters(queryParameters),
513		autorest.WithHeader("If-Match", autorest.String(ifMatch)))
514	return preparer.Prepare((&http.Request{}).WithContext(ctx))
515}
516
517// UpdateSender sends the Update request. The method will close the
518// http.Response Body if it receives an error.
519func (client LoggerClient) UpdateSender(req *http.Request) (*http.Response, error) {
520	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
521}
522
523// UpdateResponder handles the response to the Update request. The method always
524// closes the http.Response Body.
525func (client LoggerClient) UpdateResponder(resp *http.Response) (result autorest.Response, err error) {
526	err = autorest.Respond(
527		resp,
528		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
529		autorest.ByClosing())
530	result.Response = resp
531	return
532}
533