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// UserConfirmationPasswordClient is the apiManagement Client
30type UserConfirmationPasswordClient struct {
31	BaseClient
32}
33
34// NewUserConfirmationPasswordClient creates an instance of the UserConfirmationPasswordClient client.
35func NewUserConfirmationPasswordClient(subscriptionID string) UserConfirmationPasswordClient {
36	return NewUserConfirmationPasswordClientWithBaseURI(DefaultBaseURI, subscriptionID)
37}
38
39// NewUserConfirmationPasswordClientWithBaseURI creates an instance of the UserConfirmationPasswordClient client using
40// a custom endpoint.  Use this when interacting with an Azure cloud that uses a non-standard base URI (sovereign
41// clouds, Azure stack).
42func NewUserConfirmationPasswordClientWithBaseURI(baseURI string, subscriptionID string) UserConfirmationPasswordClient {
43	return UserConfirmationPasswordClient{NewWithBaseURI(baseURI, subscriptionID)}
44}
45
46// SendMethod sends confirmation
47// Parameters:
48// resourceGroupName - the name of the resource group.
49// serviceName - the name of the API Management service.
50// userID - user identifier. Must be unique in the current API Management service instance.
51// appType - determines the type of application which send the create user request. Default is legacy publisher
52// portal.
53func (client UserConfirmationPasswordClient) SendMethod(ctx context.Context, resourceGroupName string, serviceName string, userID string, appType AppType) (result autorest.Response, err error) {
54	if tracing.IsEnabled() {
55		ctx = tracing.StartSpan(ctx, fqdn+"/UserConfirmationPasswordClient.SendMethod")
56		defer func() {
57			sc := -1
58			if result.Response != nil {
59				sc = result.Response.StatusCode
60			}
61			tracing.EndSpan(ctx, sc, err)
62		}()
63	}
64	if err := validation.Validate([]validation.Validation{
65		{TargetValue: serviceName,
66			Constraints: []validation.Constraint{{Target: "serviceName", Name: validation.MaxLength, Rule: 50, Chain: nil},
67				{Target: "serviceName", Name: validation.MinLength, Rule: 1, Chain: nil},
68				{Target: "serviceName", Name: validation.Pattern, Rule: `^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$`, Chain: nil}}},
69		{TargetValue: userID,
70			Constraints: []validation.Constraint{{Target: "userID", Name: validation.MaxLength, Rule: 80, Chain: nil},
71				{Target: "userID", Name: validation.MinLength, Rule: 1, Chain: nil}}}}); err != nil {
72		return result, validation.NewError("apimanagement.UserConfirmationPasswordClient", "SendMethod", err.Error())
73	}
74
75	req, err := client.SendMethodPreparer(ctx, resourceGroupName, serviceName, userID, appType)
76	if err != nil {
77		err = autorest.NewErrorWithError(err, "apimanagement.UserConfirmationPasswordClient", "SendMethod", nil, "Failure preparing request")
78		return
79	}
80
81	resp, err := client.SendMethodSender(req)
82	if err != nil {
83		result.Response = resp
84		err = autorest.NewErrorWithError(err, "apimanagement.UserConfirmationPasswordClient", "SendMethod", resp, "Failure sending request")
85		return
86	}
87
88	result, err = client.SendMethodResponder(resp)
89	if err != nil {
90		err = autorest.NewErrorWithError(err, "apimanagement.UserConfirmationPasswordClient", "SendMethod", resp, "Failure responding to request")
91		return
92	}
93
94	return
95}
96
97// SendMethodPreparer prepares the SendMethod request.
98func (client UserConfirmationPasswordClient) SendMethodPreparer(ctx context.Context, resourceGroupName string, serviceName string, userID string, appType AppType) (*http.Request, error) {
99	pathParameters := map[string]interface{}{
100		"resourceGroupName": autorest.Encode("path", resourceGroupName),
101		"serviceName":       autorest.Encode("path", serviceName),
102		"subscriptionId":    autorest.Encode("path", client.SubscriptionID),
103		"userId":            autorest.Encode("path", userID),
104	}
105
106	const APIVersion = "2019-12-01"
107	queryParameters := map[string]interface{}{
108		"api-version": APIVersion,
109	}
110	if len(string(appType)) > 0 {
111		queryParameters["appType"] = autorest.Encode("query", appType)
112	} else {
113		queryParameters["appType"] = autorest.Encode("query", "portal")
114	}
115
116	preparer := autorest.CreatePreparer(
117		autorest.AsPost(),
118		autorest.WithBaseURL(client.BaseURI),
119		autorest.WithPathParameters("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/users/{userId}/confirmations/password/send", pathParameters),
120		autorest.WithQueryParameters(queryParameters))
121	return preparer.Prepare((&http.Request{}).WithContext(ctx))
122}
123
124// SendMethodSender sends the SendMethod request. The method will close the
125// http.Response Body if it receives an error.
126func (client UserConfirmationPasswordClient) SendMethodSender(req *http.Request) (*http.Response, error) {
127	return client.Send(req, azure.DoRetryWithRegistration(client.Client))
128}
129
130// SendMethodResponder handles the response to the SendMethod request. The method always
131// closes the http.Response Body.
132func (client UserConfirmationPasswordClient) SendMethodResponder(resp *http.Response) (result autorest.Response, err error) {
133	err = autorest.Respond(
134		resp,
135		azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent),
136		autorest.ByClosing())
137	result.Response = resp
138	return
139}
140