// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) Go Code Generator. DO NOT EDIT.

package armrelationships

import (
	"context"
	"errors"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"net/http"
	"net/url"
	"strings"
)

// DependencyOfRelationshipsClient contains the methods for the DependencyOfRelationships group.
// Don't use this type directly, use NewDependencyOfRelationshipsClient() instead.
//
// Generated from API version 2023-09-01-preview
type DependencyOfRelationshipsClient struct {
	internal *arm.Client
}

// NewDependencyOfRelationshipsClient creates a new instance of DependencyOfRelationshipsClient with the specified values.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - Contains optional client configuration. Pass nil to accept the default values.
func NewDependencyOfRelationshipsClient(credential azcore.TokenCredential, options *arm.ClientOptions) (*DependencyOfRelationshipsClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &DependencyOfRelationshipsClient{
		internal: cl,
	}
	return client, nil
}

// BeginCreateOrUpdate - Create a DependencyOfRelationship
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - name - Name of dependencyOf relationship.
//   - resource - Resource create parameters.
//   - options - DependencyOfRelationshipsClientBeginCreateOrUpdateOptions contains the optional parameters for the DependencyOfRelationshipsClient.BeginCreateOrUpdate
//     method.
func (client *DependencyOfRelationshipsClient) BeginCreateOrUpdate(ctx context.Context, resourceURI string, name string, resource DependencyOfRelationship, options *DependencyOfRelationshipsClientBeginCreateOrUpdateOptions) (*runtime.Poller[DependencyOfRelationshipsClientCreateOrUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.createOrUpdate(ctx, resourceURI, name, resource, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[DependencyOfRelationshipsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[DependencyOfRelationshipsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// CreateOrUpdate - Create a DependencyOfRelationship
// If the operation fails it returns an *azcore.ResponseError type.
func (client *DependencyOfRelationshipsClient) createOrUpdate(ctx context.Context, resourceURI string, name string, resource DependencyOfRelationship, options *DependencyOfRelationshipsClientBeginCreateOrUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "DependencyOfRelationshipsClient.BeginCreateOrUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createOrUpdateCreateRequest(ctx, resourceURI, name, resource, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *DependencyOfRelationshipsClient) createOrUpdateCreateRequest(ctx context.Context, resourceURI string, name string, resource DependencyOfRelationship, _ *DependencyOfRelationshipsClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.Relationships/dependencyOf/{name}"
	if resourceURI == "" {
		return nil, errors.New("parameter resourceURI cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	if name == "" {
		return nil, errors.New("parameter name cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{name}", url.PathEscape(name))
	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20230901Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, resource); err != nil {
		return nil, err
	}
	return req, nil
}

// BeginDelete - Delete a DependencyOfRelationship
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - name - Name of dependencyOf relationship.
//   - options - DependencyOfRelationshipsClientBeginDeleteOptions contains the optional parameters for the DependencyOfRelationshipsClient.BeginDelete
//     method.
func (client *DependencyOfRelationshipsClient) BeginDelete(ctx context.Context, resourceURI string, name string, options *DependencyOfRelationshipsClientBeginDeleteOptions) (*runtime.Poller[DependencyOfRelationshipsClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceURI, name, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[DependencyOfRelationshipsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[DependencyOfRelationshipsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - Delete a DependencyOfRelationship
// If the operation fails it returns an *azcore.ResponseError type.
func (client *DependencyOfRelationshipsClient) deleteOperation(ctx context.Context, resourceURI string, name string, options *DependencyOfRelationshipsClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "DependencyOfRelationshipsClient.BeginDelete"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.deleteCreateRequest(ctx, resourceURI, name, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusAccepted, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// deleteCreateRequest creates the Delete request.
func (client *DependencyOfRelationshipsClient) deleteCreateRequest(ctx context.Context, resourceURI string, name string, _ *DependencyOfRelationshipsClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.Relationships/dependencyOf/{name}"
	if resourceURI == "" {
		return nil, errors.New("parameter resourceURI cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	if name == "" {
		return nil, errors.New("parameter name cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{name}", url.PathEscape(name))
	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20230901Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	return req, nil
}

// Get - Get a DependencyOfRelationship
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - name - Name of dependencyOf relationship.
//   - options - DependencyOfRelationshipsClientGetOptions contains the optional parameters for the DependencyOfRelationshipsClient.Get
//     method.
func (client *DependencyOfRelationshipsClient) Get(ctx context.Context, resourceURI string, name string, options *DependencyOfRelationshipsClientGetOptions) (DependencyOfRelationshipsClientGetResponse, error) {
	var err error
	const operationName = "DependencyOfRelationshipsClient.Get"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getCreateRequest(ctx, resourceURI, name, options)
	if err != nil {
		return DependencyOfRelationshipsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DependencyOfRelationshipsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return DependencyOfRelationshipsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *DependencyOfRelationshipsClient) getCreateRequest(ctx context.Context, resourceURI string, name string, _ *DependencyOfRelationshipsClientGetOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.Relationships/dependencyOf/{name}"
	if resourceURI == "" {
		return nil, errors.New("parameter resourceURI cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	if name == "" {
		return nil, errors.New("parameter name cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{name}", url.PathEscape(name))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20230901Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getHandleResponse handles the Get response.
func (client *DependencyOfRelationshipsClient) getHandleResponse(resp *http.Response) (DependencyOfRelationshipsClientGetResponse, error) {
	result := DependencyOfRelationshipsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DependencyOfRelationship); err != nil {
		return DependencyOfRelationshipsClientGetResponse{}, err
	}
	return result, nil
}
