/* tslint:disable */
/* eslint-disable */
/**
 * AutoGlue API
 * API for managing K3s clusters across cloud providers
 *
 * The version of the OpenAPI document: dev
 *
 *
 * 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 {
  DtoCreateSSHRequest,
  DtoSshResponse,
  GetSSHKey200Response,
} from "../models/index";
import {
  DtoCreateSSHRequestFromJSON,
  DtoCreateSSHRequestToJSON,
  DtoSshResponseFromJSON,
  DtoSshResponseToJSON,
  GetSSHKey200ResponseFromJSON,
  GetSSHKey200ResponseToJSON,
} from "../models/index";

export interface CreateSSHKeyRequest {
  dtoCreateSSHRequest: DtoCreateSSHRequest;
  xOrgID?: string;
}

export interface DeleteSSHKeyRequest {
  id: string;
  xOrgID?: string;
}

export interface DownloadSSHKeyRequest {
  xOrgID: string;
  id: string;
  part: DownloadSSHKeyPartEnum;
}

export interface GetSSHKeyRequest {
  id: string;
  xOrgID?: string;
  reveal?: boolean;
}

export interface ListPublicSshKeysRequest {
  xOrgID?: string;
}

/**
 *
 */
export class SshApi extends runtime.BaseAPI {
  /**
   * Generates an RSA or ED25519 keypair, saves it, and returns metadata. For RSA you may set bits (2048/3072/4096). Default is 4096. ED25519 ignores bits.
   * Create ssh keypair (org scoped)
   */
  async createSSHKeyRaw(
    requestParameters: CreateSSHKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<DtoSshResponse>> {
    if (requestParameters["dtoCreateSSHRequest"] == null) {
      throw new runtime.RequiredError(
        "dtoCreateSSHRequest",
        'Required parameter "dtoCreateSSHRequest" was null or undefined when calling createSSHKey().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

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

    if (requestParameters["xOrgID"] != null) {
      headerParameters["X-Org-ID"] = String(requestParameters["xOrgID"]);
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["X-ORG-KEY"] =
        await this.configuration.apiKey("X-ORG-KEY"); // OrgKeyAuth authentication
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["X-ORG-SECRET"] =
        await this.configuration.apiKey("X-ORG-SECRET"); // OrgSecretAuth authentication
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/ssh`;

    const response = await this.request(
      {
        path: urlPath,
        method: "POST",
        headers: headerParameters,
        query: queryParameters,
        body: DtoCreateSSHRequestToJSON(
          requestParameters["dtoCreateSSHRequest"],
        ),
      },
      initOverrides,
    );

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

  /**
   * Generates an RSA or ED25519 keypair, saves it, and returns metadata. For RSA you may set bits (2048/3072/4096). Default is 4096. ED25519 ignores bits.
   * Create ssh keypair (org scoped)
   */
  async createSSHKey(
    requestParameters: CreateSSHKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<DtoSshResponse> {
    const response = await this.createSSHKeyRaw(
      requestParameters,
      initOverrides,
    );
    return await response.value();
  }

  /**
   * Permanently deletes a keypair.
   * Delete ssh keypair (org scoped)
   */
  async deleteSSHKeyRaw(
    requestParameters: DeleteSSHKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<void>> {
    if (requestParameters["id"] == null) {
      throw new runtime.RequiredError(
        "id",
        'Required parameter "id" was null or undefined when calling deleteSSHKey().',
      );
    }

    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    if (requestParameters["xOrgID"] != null) {
      headerParameters["X-Org-ID"] = String(requestParameters["xOrgID"]);
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["X-ORG-KEY"] =
        await this.configuration.apiKey("X-ORG-KEY"); // OrgKeyAuth authentication
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["X-ORG-SECRET"] =
        await this.configuration.apiKey("X-ORG-SECRET"); // OrgSecretAuth authentication
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/ssh/{id}`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "DELETE",
        headers: headerParameters,
        query: queryParameters,
      },
      initOverrides,
    );

    return new runtime.VoidApiResponse(response);
  }

  /**
   * Permanently deletes a keypair.
   * Delete ssh keypair (org scoped)
   */
  async deleteSSHKey(
    requestParameters: DeleteSSHKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<void> {
    await this.deleteSSHKeyRaw(requestParameters, initOverrides);
  }

  /**
   * Download `part=public|private|both` of the keypair. `both` returns a zip file.
   * Download ssh key files by ID (org scoped)
   */
  async downloadSSHKeyRaw(
    requestParameters: DownloadSSHKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<string>> {
    if (requestParameters["xOrgID"] == null) {
      throw new runtime.RequiredError(
        "xOrgID",
        'Required parameter "xOrgID" was null or undefined when calling downloadSSHKey().',
      );
    }

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

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

    const queryParameters: any = {};

    if (requestParameters["part"] != null) {
      queryParameters["part"] = requestParameters["part"];
    }

    const headerParameters: runtime.HTTPHeaders = {};

    if (requestParameters["xOrgID"] != null) {
      headerParameters["X-Org-ID"] = String(requestParameters["xOrgID"]);
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["X-ORG-KEY"] =
        await this.configuration.apiKey("X-ORG-KEY"); // OrgKeyAuth authentication
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["X-ORG-SECRET"] =
        await this.configuration.apiKey("X-ORG-SECRET"); // OrgSecretAuth authentication
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/ssh/{id}/download`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "GET",
        headers: headerParameters,
        query: queryParameters,
      },
      initOverrides,
    );

    if (this.isJsonMime(response.headers.get("content-type"))) {
      return new runtime.JSONApiResponse<string>(response);
    } else {
      return new runtime.TextApiResponse(response) as any;
    }
  }

  /**
   * Download `part=public|private|both` of the keypair. `both` returns a zip file.
   * Download ssh key files by ID (org scoped)
   */
  async downloadSSHKey(
    requestParameters: DownloadSSHKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<string> {
    const response = await this.downloadSSHKeyRaw(
      requestParameters,
      initOverrides,
    );
    return await response.value();
  }

  /**
   * Returns public key fields. Append `?reveal=true` to include the private key PEM.
   * Get ssh key by ID (org scoped)
   */
  async getSSHKeyRaw(
    requestParameters: GetSSHKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<GetSSHKey200Response>> {
    if (requestParameters["id"] == null) {
      throw new runtime.RequiredError(
        "id",
        'Required parameter "id" was null or undefined when calling getSSHKey().',
      );
    }

    const queryParameters: any = {};

    if (requestParameters["reveal"] != null) {
      queryParameters["reveal"] = requestParameters["reveal"];
    }

    const headerParameters: runtime.HTTPHeaders = {};

    if (requestParameters["xOrgID"] != null) {
      headerParameters["X-Org-ID"] = String(requestParameters["xOrgID"]);
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["X-ORG-KEY"] =
        await this.configuration.apiKey("X-ORG-KEY"); // OrgKeyAuth authentication
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["X-ORG-SECRET"] =
        await this.configuration.apiKey("X-ORG-SECRET"); // OrgSecretAuth authentication
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/ssh/{id}`;
    urlPath = urlPath.replace(
      `{${"id"}}`,
      encodeURIComponent(String(requestParameters["id"])),
    );

    const response = await this.request(
      {
        path: urlPath,
        method: "GET",
        headers: headerParameters,
        query: queryParameters,
      },
      initOverrides,
    );

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

  /**
   * Returns public key fields. Append `?reveal=true` to include the private key PEM.
   * Get ssh key by ID (org scoped)
   */
  async getSSHKey(
    requestParameters: GetSSHKeyRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<GetSSHKey200Response> {
    const response = await this.getSSHKeyRaw(requestParameters, initOverrides);
    return await response.value();
  }

  /**
   * Returns ssh keys for the organization in X-Org-ID.
   * List ssh keys (org scoped)
   */
  async listPublicSshKeysRaw(
    requestParameters: ListPublicSshKeysRequest,
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<runtime.ApiResponse<Array<DtoSshResponse>>> {
    const queryParameters: any = {};

    const headerParameters: runtime.HTTPHeaders = {};

    if (requestParameters["xOrgID"] != null) {
      headerParameters["X-Org-ID"] = String(requestParameters["xOrgID"]);
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["X-ORG-KEY"] =
        await this.configuration.apiKey("X-ORG-KEY"); // OrgKeyAuth authentication
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["X-ORG-SECRET"] =
        await this.configuration.apiKey("X-ORG-SECRET"); // OrgSecretAuth authentication
    }

    if (this.configuration && this.configuration.apiKey) {
      headerParameters["Authorization"] =
        await this.configuration.apiKey("Authorization"); // BearerAuth authentication
    }

    let urlPath = `/ssh`;

    const response = await this.request(
      {
        path: urlPath,
        method: "GET",
        headers: headerParameters,
        query: queryParameters,
      },
      initOverrides,
    );

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

  /**
   * Returns ssh keys for the organization in X-Org-ID.
   * List ssh keys (org scoped)
   */
  async listPublicSshKeys(
    requestParameters: ListPublicSshKeysRequest = {},
    initOverrides?: RequestInit | runtime.InitOverrideFunction,
  ): Promise<Array<DtoSshResponse>> {
    const response = await this.listPublicSshKeysRaw(
      requestParameters,
      initOverrides,
    );
    return await response.value();
  }
}

/**
 * @export
 */
export const DownloadSSHKeyPartEnum = {
  public: "public",
  private: "private",
  both: "both",
} as const;
export type DownloadSSHKeyPartEnum =
  (typeof DownloadSSHKeyPartEnum)[keyof typeof DownloadSSHKeyPartEnum];
