// Copyright 2022 Luca Casonato. All rights reserved. MIT license. /** * Data Portability API Client for Deno * ==================================== * * The Data Portability API lets you build applications that request authorization from a user to move a copy of data from Google services into your application. This enables data portability and facilitates switching services. * * Docs: https://developers.google.com/data-portability * Source: https://googleapis.deno.dev/v1/dataportability:v1.ts */ import { auth, CredentialsClient, GoogleAuth, request } from "/_/base@v1/mod.ts"; export { auth, GoogleAuth }; export type { CredentialsClient }; /** * The Data Portability API lets you build applications that request * authorization from a user to move a copy of data from Google services into * your application. This enables data portability and facilitates switching * services. */ export class DataPortability { #client: CredentialsClient | undefined; #baseUrl: string; constructor(client?: CredentialsClient, baseUrl: string = "https://dataportability.googleapis.com/") { this.#client = client; this.#baseUrl = baseUrl; } /** * Gets the access type of the token. * */ async accessTypeCheck(req: CheckAccessTypeRequest): Promise { const url = new URL(`${this.#baseUrl}v1/accessType:check`); const body = JSON.stringify(req); const data = await request(url.href, { client: this.#client, method: "POST", body, }); return data as CheckAccessTypeResponse; } /** * Cancels a Portability Archive job. * * @param name Required. The Archive job ID you're canceling. This is returned by the InitiatePortabilityArchive response. The format is: archiveJobs/{archive_job}. Canceling is only executed if the job is in progress. */ async archiveJobsCancel(name: string, req: CancelPortabilityArchiveRequest): Promise { const url = new URL(`${this.#baseUrl}v1/${ name }:cancel`); const body = JSON.stringify(req); const data = await request(url.href, { client: this.#client, method: "POST", body, }); return data as CancelPortabilityArchiveResponse; } /** * Retrieves the state of an Archive job for the Portability API. * * @param name Required. The archive job ID that is returned when you request the state of the job. The format is: archiveJobs/{archive_job}/portabilityArchiveState. archive_job is the job ID returned by the InitiatePortabilityArchiveResponse. */ async archiveJobsGetPortabilityArchiveState(name: string): Promise { const url = new URL(`${this.#baseUrl}v1/${ name }`); const data = await request(url.href, { client: this.#client, method: "GET", }); return deserializePortabilityArchiveState(data); } /** * Retries a failed Portability Archive job. * * @param name Required. The Archive job ID you're retrying. This is returned by the InitiatePortabilityArchiveResponse. Retrying is only executed if the initial job failed. */ async archiveJobsRetry(name: string, req: RetryPortabilityArchiveRequest): Promise { const url = new URL(`${this.#baseUrl}v1/${ name }:retry`); const body = JSON.stringify(req); const data = await request(url.href, { client: this.#client, method: "POST", body, }); return data as RetryPortabilityArchiveResponse; } /** * Revokes OAuth tokens and resets exhausted scopes for a user/project pair. * This method allows you to initiate a request after a new consent is * granted. This method also indicates that previous archives can be garbage * collected. You should call this method when all jobs are complete and all * archives are downloaded. Do not call it only when you start a new job. * */ async authorizationReset(req: ResetAuthorizationRequest): Promise { const url = new URL(`${this.#baseUrl}v1/authorization:reset`); const body = JSON.stringify(req); const data = await request(url.href, { client: this.#client, method: "POST", body, }); return data as Empty; } /** * Initiates a new Archive job for the Portability API. * */ async portabilityArchiveInitiate(req: InitiatePortabilityArchiveRequest): Promise { req = serializeInitiatePortabilityArchiveRequest(req); const url = new URL(`${this.#baseUrl}v1/portabilityArchive:initiate`); const body = JSON.stringify(req); const data = await request(url.href, { client: this.#client, method: "POST", body, }); return data as InitiatePortabilityArchiveResponse; } } /** * Request to cancel a Portability Archive job. */ export interface CancelPortabilityArchiveRequest { } /** * Response to canceling a Data Portability Archive job. */ export interface CancelPortabilityArchiveResponse { } /** * Request to check the token's access type. All required information is * derived from the attached OAuth token. */ export interface CheckAccessTypeRequest { } /** * Response to checking the token's access type. */ export interface CheckAccessTypeResponse { /** * Jobs initiated with this token will be one-time if any requested resources * have one-time access. */ oneTimeResources?: string[]; /** * Jobs initiated with this token will be time-based if all requested * resources have time-based access. */ timeBasedResources?: string[]; } /** * A generic empty message that you can re-use to avoid defining duplicated * empty messages in your APIs. A typical example is to use it as the request or * the response type of an API method. For instance: service Foo { rpc * Bar(google.protobuf.Empty) returns (google.protobuf.Empty); } */ export interface Empty { } /** * Request to kick off an Archive job. */ export interface InitiatePortabilityArchiveRequest { /** * Optional. The timestamp that represents the end point for the data you are * exporting. If the end_time is not specified in the * InitiatePortabilityArchiveRequest, this field is set to the latest * available data. */ endTime?: Date; /** * The resources from which you're exporting data. These values have a 1:1 * correspondence with the OAuth scopes. */ resources?: string[]; /** * Optional. The timestamp that represents the starting point for the data * you are exporting. If the start_time is not specified in the * InitiatePortabilityArchiveRequest, the field is set to the earliest * available data. */ startTime?: Date; } function serializeInitiatePortabilityArchiveRequest(data: any): InitiatePortabilityArchiveRequest { return { ...data, endTime: data["endTime"] !== undefined ? data["endTime"].toISOString() : undefined, startTime: data["startTime"] !== undefined ? data["startTime"].toISOString() : undefined, }; } function deserializeInitiatePortabilityArchiveRequest(data: any): InitiatePortabilityArchiveRequest { return { ...data, endTime: data["endTime"] !== undefined ? new Date(data["endTime"]) : undefined, startTime: data["startTime"] !== undefined ? new Date(data["startTime"]) : undefined, }; } /** * Response from initiating an Archive job. */ export interface InitiatePortabilityArchiveResponse { /** * The access type of the Archive job initiated by the API. */ accessType?: | "ACCESS_TYPE_UNSPECIFIED" | "ACCESS_TYPE_ONE_TIME" | "ACCESS_TYPE_TIME_BASED"; /** * The archive job ID that is initiated in the API. This can be used to get * the state of the job. */ archiveJobId?: string; } /** * Resource that contains the state of an Archive job. */ export interface PortabilityArchiveState { /** * The timestamp that represents the end point for the data you are * exporting. If the end_time value is set in the * InitiatePortabilityArchiveRequest, this field is set to that value. If * end_time is not set, this value is set to the time the export was * requested. */ exportTime?: Date; /** * The resource name of ArchiveJob's PortabilityArchiveState singleton. The * format is: archiveJobs/{archive_job}/portabilityArchiveState. archive_job * is the job ID provided in the request. */ name?: string; /** * The timestamp that represents the starting point for the data you are * exporting. This field is set only if the start_time field is specified in * the InitiatePortabilityArchiveRequest. */ startTime?: Date; /** * Resource that represents the state of the Archive job. */ state?: | "STATE_UNSPECIFIED" | "IN_PROGRESS" | "COMPLETE" | "FAILED" | "CANCELLED"; /** * If the state is complete, this method returns the signed URLs of the * objects in the Cloud Storage bucket. */ urls?: string[]; } function serializePortabilityArchiveState(data: any): PortabilityArchiveState { return { ...data, exportTime: data["exportTime"] !== undefined ? data["exportTime"].toISOString() : undefined, startTime: data["startTime"] !== undefined ? data["startTime"].toISOString() : undefined, }; } function deserializePortabilityArchiveState(data: any): PortabilityArchiveState { return { ...data, exportTime: data["exportTime"] !== undefined ? new Date(data["exportTime"]) : undefined, startTime: data["startTime"] !== undefined ? new Date(data["startTime"]) : undefined, }; } /** * Request to reset exhausted OAuth scopes. */ export interface ResetAuthorizationRequest { } /** * Request to retry a failed Portability Archive job. */ export interface RetryPortabilityArchiveRequest { } /** * Response from retrying a Portability Archive. */ export interface RetryPortabilityArchiveResponse { /** * The archive job ID that is initiated by the retry endpoint. This can be * used to get the state of the new job. */ archiveJobId?: string; }