/*
Management API

API version: 3
*/

// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.

package management

import (
	"context"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"

	"github.com/adyen/adyen-go-api-library/v21/src/common"
)

// APICredentialsCompanyLevelApi service
type APICredentialsCompanyLevelApi common.Service

// All parameters accepted by APICredentialsCompanyLevelApi.CreateApiCredential
type APICredentialsCompanyLevelApiCreateApiCredentialInput struct {
	companyId                         string
	createCompanyApiCredentialRequest *CreateCompanyApiCredentialRequest
}

func (r APICredentialsCompanyLevelApiCreateApiCredentialInput) CreateCompanyApiCredentialRequest(createCompanyApiCredentialRequest CreateCompanyApiCredentialRequest) APICredentialsCompanyLevelApiCreateApiCredentialInput {
	r.createCompanyApiCredentialRequest = &createCompanyApiCredentialRequest
	return r
}

/*
Prepare a request for CreateApiCredential
@param companyId The unique identifier of the company account.
@return APICredentialsCompanyLevelApiCreateApiCredentialInput
*/
func (a *APICredentialsCompanyLevelApi) CreateApiCredentialInput(companyId string) APICredentialsCompanyLevelApiCreateApiCredentialInput {
	return APICredentialsCompanyLevelApiCreateApiCredentialInput{
		companyId: companyId,
	}
}

/*
CreateApiCredential Create an API credential.

Creates an [API credential](https://docs.adyen.com/development-resources/api-credentials) for the company account identified in the path. In the request, you can specify which merchant accounts the new API credential will have access to, as well as its roles and allowed origins.

The response includes several types of authentication details:
* [API key](https://docs.adyen.com/development-resources/api-authentication#api-key-authentication): used for API request authentication.
* [Client key](https://docs.adyen.com/development-resources/client-side-authentication#how-it-works): public key used for client-side authentication.
* [Username and password](https://docs.adyen.com/development-resources/api-authentication#using-basic-authentication): used for basic authentication.

> Make sure you store the API key securely in your system. You won't be able to retrieve it later.

If your API key is lost or compromised, you need to [generate a new API key](https://docs.adyen.com/api-explorer/#/ManagementService/v1/post/companies/{companyId}/apiCredentials/{apiCredentialId}/generateApiKey).

To make this request, your API credential must have the following [roles](https://docs.adyen.com/development-resources/api-credentials#api-permissions):
* Management API—API credentials read and write

@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@param r APICredentialsCompanyLevelApiCreateApiCredentialInput - Request parameters, see CreateApiCredentialInput
@return CreateCompanyApiCredentialResponse, *http.Response, error
*/
func (a *APICredentialsCompanyLevelApi) CreateApiCredential(ctx context.Context, r APICredentialsCompanyLevelApiCreateApiCredentialInput) (CreateCompanyApiCredentialResponse, *http.Response, error) {
	res := &CreateCompanyApiCredentialResponse{}
	path := "/companies/{companyId}/apiCredentials"
	path = strings.Replace(path, "{"+"companyId"+"}", url.PathEscape(common.ParameterValueToString(r.companyId, "companyId")), -1)
	queryParams := url.Values{}
	headerParams := make(map[string]string)
	httpRes, err := common.SendAPIRequest(
		ctx,
		a.Client,
		r.createCompanyApiCredentialRequest,
		res,
		http.MethodPost,
		a.BasePath()+path,
		queryParams,
		headerParams,
	)

	if httpRes == nil {
		return *res, httpRes, err
	}

	var serviceError common.RestServiceError
	if httpRes.StatusCode == 400 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 401 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 403 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 422 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 500 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}

	return *res, httpRes, err
}

// All parameters accepted by APICredentialsCompanyLevelApi.GetApiCredential
type APICredentialsCompanyLevelApiGetApiCredentialInput struct {
	companyId       string
	apiCredentialId string
}

/*
Prepare a request for GetApiCredential
@param companyId The unique identifier of the company account.@param apiCredentialId Unique identifier of the API credential.
@return APICredentialsCompanyLevelApiGetApiCredentialInput
*/
func (a *APICredentialsCompanyLevelApi) GetApiCredentialInput(companyId string, apiCredentialId string) APICredentialsCompanyLevelApiGetApiCredentialInput {
	return APICredentialsCompanyLevelApiGetApiCredentialInput{
		companyId:       companyId,
		apiCredentialId: apiCredentialId,
	}
}

/*
GetApiCredential Get an API credential

Returns the [API credential](https://docs.adyen.com/development-resources/api-credentials) identified in the path.

To make this request, your API credential must have the following [roles](https://docs.adyen.com/development-resources/api-credentials#api-permissions):
* Management API—API credentials read and write

@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@param r APICredentialsCompanyLevelApiGetApiCredentialInput - Request parameters, see GetApiCredentialInput
@return CompanyApiCredential, *http.Response, error
*/
func (a *APICredentialsCompanyLevelApi) GetApiCredential(ctx context.Context, r APICredentialsCompanyLevelApiGetApiCredentialInput) (CompanyApiCredential, *http.Response, error) {
	res := &CompanyApiCredential{}
	path := "/companies/{companyId}/apiCredentials/{apiCredentialId}"
	path = strings.Replace(path, "{"+"companyId"+"}", url.PathEscape(common.ParameterValueToString(r.companyId, "companyId")), -1)
	path = strings.Replace(path, "{"+"apiCredentialId"+"}", url.PathEscape(common.ParameterValueToString(r.apiCredentialId, "apiCredentialId")), -1)
	queryParams := url.Values{}
	headerParams := make(map[string]string)
	httpRes, err := common.SendAPIRequest(
		ctx,
		a.Client,
		nil,
		res,
		http.MethodGet,
		a.BasePath()+path,
		queryParams,
		headerParams,
	)

	if httpRes == nil {
		return *res, httpRes, err
	}

	var serviceError common.RestServiceError
	if httpRes.StatusCode == 400 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 401 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 403 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 422 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 500 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}

	return *res, httpRes, err
}

// All parameters accepted by APICredentialsCompanyLevelApi.ListApiCredentials
type APICredentialsCompanyLevelApiListApiCredentialsInput struct {
	companyId  string
	pageNumber *int32
	pageSize   *int32
}

// The number of the page to fetch.
func (r APICredentialsCompanyLevelApiListApiCredentialsInput) PageNumber(pageNumber int32) APICredentialsCompanyLevelApiListApiCredentialsInput {
	r.pageNumber = &pageNumber
	return r
}

// The number of items to have on a page, maximum 100. The default is 10 items on a page.
func (r APICredentialsCompanyLevelApiListApiCredentialsInput) PageSize(pageSize int32) APICredentialsCompanyLevelApiListApiCredentialsInput {
	r.pageSize = &pageSize
	return r
}

/*
Prepare a request for ListApiCredentials
@param companyId The unique identifier of the company account.
@return APICredentialsCompanyLevelApiListApiCredentialsInput
*/
func (a *APICredentialsCompanyLevelApi) ListApiCredentialsInput(companyId string) APICredentialsCompanyLevelApiListApiCredentialsInput {
	return APICredentialsCompanyLevelApiListApiCredentialsInput{
		companyId: companyId,
	}
}

/*
ListApiCredentials Get a list of API credentials

Returns the list of [API credentials](https://docs.adyen.com/development-resources/api-credentials) for the company account. The list is grouped into pages as defined by the query parameters.

To make this request, your API credential must have the following [roles](https://docs.adyen.com/development-resources/api-credentials#api-permissions):
* Management API—API credentials read and write

@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@param r APICredentialsCompanyLevelApiListApiCredentialsInput - Request parameters, see ListApiCredentialsInput
@return ListCompanyApiCredentialsResponse, *http.Response, error
*/
func (a *APICredentialsCompanyLevelApi) ListApiCredentials(ctx context.Context, r APICredentialsCompanyLevelApiListApiCredentialsInput) (ListCompanyApiCredentialsResponse, *http.Response, error) {
	res := &ListCompanyApiCredentialsResponse{}
	path := "/companies/{companyId}/apiCredentials"
	path = strings.Replace(path, "{"+"companyId"+"}", url.PathEscape(common.ParameterValueToString(r.companyId, "companyId")), -1)
	queryParams := url.Values{}
	headerParams := make(map[string]string)
	if r.pageNumber != nil {
		common.ParameterAddToQuery(queryParams, "pageNumber", r.pageNumber, "")
	}
	if r.pageSize != nil {
		common.ParameterAddToQuery(queryParams, "pageSize", r.pageSize, "")
	}
	httpRes, err := common.SendAPIRequest(
		ctx,
		a.Client,
		nil,
		res,
		http.MethodGet,
		a.BasePath()+path,
		queryParams,
		headerParams,
	)

	if httpRes == nil {
		return *res, httpRes, err
	}

	var serviceError common.RestServiceError
	if httpRes.StatusCode == 400 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 401 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 403 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 422 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 500 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}

	return *res, httpRes, err
}

// All parameters accepted by APICredentialsCompanyLevelApi.UpdateApiCredential
type APICredentialsCompanyLevelApiUpdateApiCredentialInput struct {
	companyId                         string
	apiCredentialId                   string
	updateCompanyApiCredentialRequest *UpdateCompanyApiCredentialRequest
}

func (r APICredentialsCompanyLevelApiUpdateApiCredentialInput) UpdateCompanyApiCredentialRequest(updateCompanyApiCredentialRequest UpdateCompanyApiCredentialRequest) APICredentialsCompanyLevelApiUpdateApiCredentialInput {
	r.updateCompanyApiCredentialRequest = &updateCompanyApiCredentialRequest
	return r
}

/*
Prepare a request for UpdateApiCredential
@param companyId The unique identifier of the company account.@param apiCredentialId Unique identifier of the API credential.
@return APICredentialsCompanyLevelApiUpdateApiCredentialInput
*/
func (a *APICredentialsCompanyLevelApi) UpdateApiCredentialInput(companyId string, apiCredentialId string) APICredentialsCompanyLevelApiUpdateApiCredentialInput {
	return APICredentialsCompanyLevelApiUpdateApiCredentialInput{
		companyId:       companyId,
		apiCredentialId: apiCredentialId,
	}
}

/*
UpdateApiCredential Update an API credential.

Changes the API credential's roles, merchant account access, or allowed origins. The request has the new values for the fields you want to change. The response contains the full updated API credential, including the new values from the request.

To make this request, your API credential must have the following [roles](https://docs.adyen.com/development-resources/api-credentials#api-permissions):
* Management API—API credentials read and write

@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@param r APICredentialsCompanyLevelApiUpdateApiCredentialInput - Request parameters, see UpdateApiCredentialInput
@return CompanyApiCredential, *http.Response, error
*/
func (a *APICredentialsCompanyLevelApi) UpdateApiCredential(ctx context.Context, r APICredentialsCompanyLevelApiUpdateApiCredentialInput) (CompanyApiCredential, *http.Response, error) {
	res := &CompanyApiCredential{}
	path := "/companies/{companyId}/apiCredentials/{apiCredentialId}"
	path = strings.Replace(path, "{"+"companyId"+"}", url.PathEscape(common.ParameterValueToString(r.companyId, "companyId")), -1)
	path = strings.Replace(path, "{"+"apiCredentialId"+"}", url.PathEscape(common.ParameterValueToString(r.apiCredentialId, "apiCredentialId")), -1)
	queryParams := url.Values{}
	headerParams := make(map[string]string)
	httpRes, err := common.SendAPIRequest(
		ctx,
		a.Client,
		r.updateCompanyApiCredentialRequest,
		res,
		http.MethodPatch,
		a.BasePath()+path,
		queryParams,
		headerParams,
	)

	if httpRes == nil {
		return *res, httpRes, err
	}

	var serviceError common.RestServiceError
	if httpRes.StatusCode == 400 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 401 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 403 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 422 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 500 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}

	return *res, httpRes, err
}
