/* tslint:disable */
/* eslint-disable */
/**
 * UCX Proxy
 * A single use API for the UCX Proxy.
 *
 * The version of the OpenAPI document: 0.0.12
 * Contact: customersuccess@equisoft.com
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


import * as runtime from '../runtime';
import type {
  CreatePayloadSourceAuthenticationSchema,
  ErrorSchema,
  ListOrganizationSourceAuthenticationResultSchema,
  OrganizationSourceAuthenticationResultSchema,
  ToggleSourceAuthenticationSchema,
  UpdatePayloadSourceAuthenticationSchema,
} from '../models/index';
import {
    CreatePayloadSourceAuthenticationSchemaFromJSON,
    CreatePayloadSourceAuthenticationSchemaToJSON,
    ErrorSchemaFromJSON,
    ErrorSchemaToJSON,
    ListOrganizationSourceAuthenticationResultSchemaFromJSON,
    ListOrganizationSourceAuthenticationResultSchemaToJSON,
    OrganizationSourceAuthenticationResultSchemaFromJSON,
    OrganizationSourceAuthenticationResultSchemaToJSON,
    ToggleSourceAuthenticationSchemaFromJSON,
    ToggleSourceAuthenticationSchemaToJSON,
    UpdatePayloadSourceAuthenticationSchemaFromJSON,
    UpdatePayloadSourceAuthenticationSchemaToJSON,
} from '../models/index';

export interface CreateOrganizationSourceRequest {
    organizationId: string;
    payload: CreatePayloadSourceAuthenticationSchema;
    certificate?: Blob | null;
}

export interface DeleteOrganizationSourceRequest {
    organizationId: string;
    sourceKey: string;
}

export interface ListOrganizationSourcesRequest {
    organizationId: string;
}

export interface ToggleOrganizationSourceRequest {
    organizationId: string;
    sourceKey: string;
    toggleSourceAuthenticationSchema: ToggleSourceAuthenticationSchema;
}

export interface UpdateOrganizationSourceRequest {
    organizationId: string;
    sourceKey: string;
    payload: UpdatePayloadSourceAuthenticationSchema;
    certificate?: Blob | null;
}

/**
 * 
 */
export class OrganizationSourceAuthenticationApi extends runtime.BaseAPI {

    /**
     * Creates request options for createOrganizationSource without sending the request
     */
    async createOrganizationSourceRequestOpts(requestParameters: CreateOrganizationSourceRequest): Promise<runtime.RequestOpts> {
        if (requestParameters['organizationId'] == null) {
            throw new runtime.RequiredError(
                'organizationId',
                'Required parameter "organizationId" was null or undefined when calling createOrganizationSource().'
            );
        }

        if (requestParameters['payload'] == null) {
            throw new runtime.RequiredError(
                'payload',
                'Required parameter "payload" was null or undefined when calling createOrganizationSource().'
            );
        }

        const queryParameters: any = {};

        const headerParameters: runtime.HTTPHeaders = {};

        if (this.configuration && this.configuration.accessToken) {
            // oauth required
            const token = this.configuration.accessToken;
            const tokenString = await token("default", ["ucx:organization"]);
            if (tokenString) {
                headerParameters["Authorization"] = `Bearer ${tokenString}`;
            }
        }

        const consumes: runtime.Consume[] = [
            { contentType: 'multipart/form-data' },
        ];
        // @ts-ignore: canConsumeForm may be unused
        const canConsumeForm = runtime.canConsumeForm(consumes);

        let formParams: { append(param: string, value: any): any };
        let useForm = false;
        // use FormData to transmit files using content-type "multipart/form-data"
        useForm = canConsumeForm;
        if (useForm) {
            formParams = new FormData();
        } else {
            formParams = new URLSearchParams();
        }

        if (requestParameters['payload'] != null) {
            formParams.append('payload', new Blob([JSON.stringify(CreatePayloadSourceAuthenticationSchemaToJSON(requestParameters['payload']))], { type: "application/json", }));
                    }

        if (requestParameters['certificate'] != null) {
            formParams.append('certificate', requestParameters['certificate'] as any);
        }


        let urlPath = `/authentications/organizations/{organizationId}/sources`;
        urlPath = urlPath.replace(`{${"organizationId"}}`, encodeURIComponent(String(requestParameters['organizationId'])));

        return {
            path: urlPath,
            method: 'POST',
            headers: headerParameters,
            query: queryParameters,
            body: formParams,
        };
    }

    /**
     * Create a new source for an organization and authenticate
     */
    async createOrganizationSourceRaw(requestParameters: CreateOrganizationSourceRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<OrganizationSourceAuthenticationResultSchema>> {
        const requestOptions = await this.createOrganizationSourceRequestOpts(requestParameters);
        const response = await this.request(requestOptions, initOverrides);

        return new runtime.JSONApiResponse(response, (jsonValue) => OrganizationSourceAuthenticationResultSchemaFromJSON(jsonValue));
    }

    /**
     * Create a new source for an organization and authenticate
     */
    async createOrganizationSource(requestParameters: CreateOrganizationSourceRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<OrganizationSourceAuthenticationResultSchema> {
        const response = await this.createOrganizationSourceRaw(requestParameters, initOverrides);
        return await response.value();
    }

    /**
     * Creates request options for deleteOrganizationSource without sending the request
     */
    async deleteOrganizationSourceRequestOpts(requestParameters: DeleteOrganizationSourceRequest): Promise<runtime.RequestOpts> {
        if (requestParameters['organizationId'] == null) {
            throw new runtime.RequiredError(
                'organizationId',
                'Required parameter "organizationId" was null or undefined when calling deleteOrganizationSource().'
            );
        }

        if (requestParameters['sourceKey'] == null) {
            throw new runtime.RequiredError(
                'sourceKey',
                'Required parameter "sourceKey" was null or undefined when calling deleteOrganizationSource().'
            );
        }

        const queryParameters: any = {};

        const headerParameters: runtime.HTTPHeaders = {};

        if (this.configuration && this.configuration.accessToken) {
            // oauth required
            const token = this.configuration.accessToken;
            const tokenString = await token("default", ["ucx:organization"]);
            if (tokenString) {
                headerParameters["Authorization"] = `Bearer ${tokenString}`;
            }
        }


        let urlPath = `/authentications/organizations/{organizationId}/sources/{sourceKey}`;
        urlPath = urlPath.replace(`{${"organizationId"}}`, encodeURIComponent(String(requestParameters['organizationId'])));
        urlPath = urlPath.replace(`{${"sourceKey"}}`, encodeURIComponent(String(requestParameters['sourceKey'])));

        return {
            path: urlPath,
            method: 'DELETE',
            headers: headerParameters,
            query: queryParameters,
        };
    }

    /**
     * Delete an existing source
     */
    async deleteOrganizationSourceRaw(requestParameters: DeleteOrganizationSourceRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<void>> {
        const requestOptions = await this.deleteOrganizationSourceRequestOpts(requestParameters);
        const response = await this.request(requestOptions, initOverrides);

        return new runtime.VoidApiResponse(response);
    }

    /**
     * Delete an existing source
     */
    async deleteOrganizationSource(requestParameters: DeleteOrganizationSourceRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<void> {
        await this.deleteOrganizationSourceRaw(requestParameters, initOverrides);
    }

    /**
     * Creates request options for listOrganizationSources without sending the request
     */
    async listOrganizationSourcesRequestOpts(requestParameters: ListOrganizationSourcesRequest): Promise<runtime.RequestOpts> {
        if (requestParameters['organizationId'] == null) {
            throw new runtime.RequiredError(
                'organizationId',
                'Required parameter "organizationId" was null or undefined when calling listOrganizationSources().'
            );
        }

        const queryParameters: any = {};

        const headerParameters: runtime.HTTPHeaders = {};

        if (this.configuration && this.configuration.accessToken) {
            // oauth required
            const token = this.configuration.accessToken;
            const tokenString = await token("default", ["ucx:organization"]);
            if (tokenString) {
                headerParameters["Authorization"] = `Bearer ${tokenString}`;
            }
        }


        let urlPath = `/authentications/organizations/{organizationId}/sources`;
        urlPath = urlPath.replace(`{${"organizationId"}}`, encodeURIComponent(String(requestParameters['organizationId'])));

        return {
            path: urlPath,
            method: 'GET',
            headers: headerParameters,
            query: queryParameters,
        };
    }

    /**
     * Get all the authentication sources for an organization
     */
    async listOrganizationSourcesRaw(requestParameters: ListOrganizationSourcesRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<ListOrganizationSourceAuthenticationResultSchema>> {
        const requestOptions = await this.listOrganizationSourcesRequestOpts(requestParameters);
        const response = await this.request(requestOptions, initOverrides);

        return new runtime.JSONApiResponse(response, (jsonValue) => ListOrganizationSourceAuthenticationResultSchemaFromJSON(jsonValue));
    }

    /**
     * Get all the authentication sources for an organization
     */
    async listOrganizationSources(requestParameters: ListOrganizationSourcesRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<ListOrganizationSourceAuthenticationResultSchema> {
        const response = await this.listOrganizationSourcesRaw(requestParameters, initOverrides);
        return await response.value();
    }

    /**
     * Creates request options for toggleOrganizationSource without sending the request
     */
    async toggleOrganizationSourceRequestOpts(requestParameters: ToggleOrganizationSourceRequest): Promise<runtime.RequestOpts> {
        if (requestParameters['organizationId'] == null) {
            throw new runtime.RequiredError(
                'organizationId',
                'Required parameter "organizationId" was null or undefined when calling toggleOrganizationSource().'
            );
        }

        if (requestParameters['sourceKey'] == null) {
            throw new runtime.RequiredError(
                'sourceKey',
                'Required parameter "sourceKey" was null or undefined when calling toggleOrganizationSource().'
            );
        }

        if (requestParameters['toggleSourceAuthenticationSchema'] == null) {
            throw new runtime.RequiredError(
                'toggleSourceAuthenticationSchema',
                'Required parameter "toggleSourceAuthenticationSchema" was null or undefined when calling toggleOrganizationSource().'
            );
        }

        const queryParameters: any = {};

        const headerParameters: runtime.HTTPHeaders = {};

        headerParameters['Content-Type'] = 'application/json';

        if (this.configuration && this.configuration.accessToken) {
            // oauth required
            const token = this.configuration.accessToken;
            const tokenString = await token("default", ["ucx:organization"]);
            if (tokenString) {
                headerParameters["Authorization"] = `Bearer ${tokenString}`;
            }
        }


        let urlPath = `/authentications/organizations/{organizationId}/sources/{sourceKey}/state`;
        urlPath = urlPath.replace(`{${"organizationId"}}`, encodeURIComponent(String(requestParameters['organizationId'])));
        urlPath = urlPath.replace(`{${"sourceKey"}}`, encodeURIComponent(String(requestParameters['sourceKey'])));

        return {
            path: urlPath,
            method: 'PUT',
            headers: headerParameters,
            query: queryParameters,
            body: ToggleSourceAuthenticationSchemaToJSON(requestParameters['toggleSourceAuthenticationSchema']),
        };
    }

    /**
     * Enable or disable a given source for an organization
     */
    async toggleOrganizationSourceRaw(requestParameters: ToggleOrganizationSourceRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<OrganizationSourceAuthenticationResultSchema>> {
        const requestOptions = await this.toggleOrganizationSourceRequestOpts(requestParameters);
        const response = await this.request(requestOptions, initOverrides);

        return new runtime.JSONApiResponse(response, (jsonValue) => OrganizationSourceAuthenticationResultSchemaFromJSON(jsonValue));
    }

    /**
     * Enable or disable a given source for an organization
     */
    async toggleOrganizationSource(requestParameters: ToggleOrganizationSourceRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<OrganizationSourceAuthenticationResultSchema> {
        const response = await this.toggleOrganizationSourceRaw(requestParameters, initOverrides);
        return await response.value();
    }

    /**
     * Creates request options for updateOrganizationSource without sending the request
     */
    async updateOrganizationSourceRequestOpts(requestParameters: UpdateOrganizationSourceRequest): Promise<runtime.RequestOpts> {
        if (requestParameters['organizationId'] == null) {
            throw new runtime.RequiredError(
                'organizationId',
                'Required parameter "organizationId" was null or undefined when calling updateOrganizationSource().'
            );
        }

        if (requestParameters['sourceKey'] == null) {
            throw new runtime.RequiredError(
                'sourceKey',
                'Required parameter "sourceKey" was null or undefined when calling updateOrganizationSource().'
            );
        }

        if (requestParameters['payload'] == null) {
            throw new runtime.RequiredError(
                'payload',
                'Required parameter "payload" was null or undefined when calling updateOrganizationSource().'
            );
        }

        const queryParameters: any = {};

        const headerParameters: runtime.HTTPHeaders = {};

        if (this.configuration && this.configuration.accessToken) {
            // oauth required
            const token = this.configuration.accessToken;
            const tokenString = await token("default", ["ucx:organization"]);
            if (tokenString) {
                headerParameters["Authorization"] = `Bearer ${tokenString}`;
            }
        }

        const consumes: runtime.Consume[] = [
            { contentType: 'multipart/form-data' },
        ];
        // @ts-ignore: canConsumeForm may be unused
        const canConsumeForm = runtime.canConsumeForm(consumes);

        let formParams: { append(param: string, value: any): any };
        let useForm = false;
        // use FormData to transmit files using content-type "multipart/form-data"
        useForm = canConsumeForm;
        if (useForm) {
            formParams = new FormData();
        } else {
            formParams = new URLSearchParams();
        }

        if (requestParameters['payload'] != null) {
            formParams.append('payload', new Blob([JSON.stringify(UpdatePayloadSourceAuthenticationSchemaToJSON(requestParameters['payload']))], { type: "application/json", }));
                    }

        if (requestParameters['certificate'] != null) {
            formParams.append('certificate', requestParameters['certificate'] as any);
        }


        let urlPath = `/authentications/organizations/{organizationId}/sources/{sourceKey}`;
        urlPath = urlPath.replace(`{${"organizationId"}}`, encodeURIComponent(String(requestParameters['organizationId'])));
        urlPath = urlPath.replace(`{${"sourceKey"}}`, encodeURIComponent(String(requestParameters['sourceKey'])));

        return {
            path: urlPath,
            method: 'PUT',
            headers: headerParameters,
            query: queryParameters,
            body: formParams,
        };
    }

    /**
     * Update the credentials of a given source for an organization
     */
    async updateOrganizationSourceRaw(requestParameters: UpdateOrganizationSourceRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<OrganizationSourceAuthenticationResultSchema>> {
        const requestOptions = await this.updateOrganizationSourceRequestOpts(requestParameters);
        const response = await this.request(requestOptions, initOverrides);

        return new runtime.JSONApiResponse(response, (jsonValue) => OrganizationSourceAuthenticationResultSchemaFromJSON(jsonValue));
    }

    /**
     * Update the credentials of a given source for an organization
     */
    async updateOrganizationSource(requestParameters: UpdateOrganizationSourceRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<OrganizationSourceAuthenticationResultSchema> {
        const response = await this.updateOrganizationSourceRaw(requestParameters, initOverrides);
        return await response.value();
    }

}
