All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scala.googleapis.bigquery.JobsClient.scala Maven / Gradle / Ivy

There is a newer version: 0.6.1-v2-20241111
Show newest version
package googleapis.bigquery

import cats.effect.Concurrent
import org.http4s._
import org.http4s.implicits._
import org.http4s.client.Client

class JobsClient[F[_]: Concurrent](client: Client[F]) extends AbstractClient[F](client) {
  val baseUri = uri"https://bigquery.googleapis.com/bigquery/v2"
  def cancel(
      /** Required. Project ID of the job to cancel
        */
      projectId: String,
      /** Required. Job ID of the job to cancel
        */
      jobId: String,
      query: JobsClient.CancelParams = JobsClient.CancelParams(),
  ): F[JobCancelResponse] = expectJson[JobCancelResponse](
    request(
      method = Method.POST,
      uri = (baseUri / "projects" / s"${projectId}" / "jobs" / s"${jobId}" / "cancel")
        .copy(query = Query.fromVector(Vector(List("location" -> query.location).flatMap {
          case (k, v) => v.map(vv => k -> Option(vv))
        }).flatten)),
    )
  )
  def insert(
      /** Project ID of project that will be billed for the job.
        */
      projectId: String
  )(input: Job): F[Job] = expectJson[Job](
    requestWithBody(method = Method.POST, uri = baseUri / "projects" / s"${projectId}" / "jobs")(
      input
    )
  )
  def query(
      /** Required. Project ID of the query request.
        */
      projectId: String
  )(input: QueryRequest): F[QueryResponse] =
    expectJson[QueryResponse](
      requestWithBody(
        method = Method.POST,
        uri = baseUri / "projects" / s"${projectId}" / "queries",
      )(input)
    )
  def delete(
      /** Required. Project ID of the job for which metadata is to be deleted.
        */
      projectId: String,
      /** Required. Job ID of the job for which metadata is to be deleted. If this is a parent job which has child jobs, the metadata from all child jobs will be deleted as well. Direct deletion of the metadata of child jobs is not allowed.
        */
      jobId: String,
      query: JobsClient.DeleteParams = JobsClient.DeleteParams(),
  ): F[Status] = client.status(
    request(
      method = Method.DELETE,
      uri = (baseUri / "projects" / s"${projectId}" / "jobs" / s"${jobId}" / "delete")
        .copy(query = Query.fromVector(Vector(List("location" -> query.location).flatMap {
          case (k, v) => v.map(vv => k -> Option(vv))
        }).flatten)),
    )
  )
  def get(
      /** Required. Project ID of the requested job.
        */
      projectId: String,
      /** Required. Job ID of the requested job.
        */
      jobId: String,
      query: JobsClient.GetParams = JobsClient.GetParams(),
  ): F[Job] = expectJson[Job](
    request(
      method = Method.GET,
      uri = (baseUri / "projects" / s"${projectId}" / "jobs" / s"${jobId}")
        .copy(query = Query.fromVector(Vector(List("location" -> query.location).flatMap {
          case (k, v) => v.map(vv => k -> Option(vv))
        }).flatten)),
    )
  )
  def list(
      /** Project ID of the jobs to list.
        */
      projectId: String,
      query: JobsClient.ListParams = JobsClient.ListParams(),
  ): F[JobList] = expectJson[JobList](
    request(
      method = Method.GET,
      uri = (baseUri / "projects" / s"${projectId}" / "jobs").copy(query =
        Query.fromVector(
          Vector(
            List("allUsers" -> query.allUsers.map(s => QueryParamEncoder[Boolean].encode(s).value))
              .flatMap { case (k, v) => v.map(vv => k -> Option(vv)) },
            List("maxCreationTime" -> query.maxCreationTime).flatMap { case (k, v) =>
              v.map(vv => k -> Option(vv))
            },
            List("maxResults" -> query.maxResults.map(s => QueryParamEncoder[Int].encode(s).value))
              .flatMap { case (k, v) => v.map(vv => k -> Option(vv)) },
            List("minCreationTime" -> query.minCreationTime).flatMap { case (k, v) =>
              v.map(vv => k -> Option(vv))
            },
            List("pageToken" -> query.pageToken).flatMap { case (k, v) =>
              v.map(vv => k -> Option(vv))
            },
            List("parentJobId" -> query.parentJobId).flatMap { case (k, v) =>
              v.map(vv => k -> Option(vv))
            },
            List("projection" -> query.projection).flatMap { case (k, v) =>
              v.map(vv => k -> Option(vv))
            },
            List("stateFilter" -> query.stateFilter).flatMap { case (k, v) =>
              v.map(vv => k -> Option(vv))
            },
          ).flatten
        )
      ),
    )
  )
  def getQueryResults(
      /** Required. Project ID of the query job.
        */
      projectId: String,
      /** Required. Job ID of the query job.
        */
      jobId: String,
      query: JobsClient.GetQueryResultsParams = JobsClient.GetQueryResultsParams(),
  ): F[GetQueryResultsResponse] = expectJson[GetQueryResultsResponse](
    request(
      method = Method.GET,
      uri = (baseUri / "projects" / s"${projectId}" / "queries" / s"${jobId}").copy(query =
        Query.fromVector(
          Vector(
            List(
              "formatOptions.useInt64Timestamp" -> query.formatOptions_useInt64Timestamp.map(s =>
                QueryParamEncoder[Boolean].encode(s).value
              )
            ).flatMap { case (k, v) => v.map(vv => k -> Option(vv)) },
            List("location" -> query.location).flatMap { case (k, v) =>
              v.map(vv => k -> Option(vv))
            },
            List("maxResults" -> query.maxResults.map(s => QueryParamEncoder[Int].encode(s).value))
              .flatMap { case (k, v) => v.map(vv => k -> Option(vv)) },
            List("pageToken" -> query.pageToken).flatMap { case (k, v) =>
              v.map(vv => k -> Option(vv))
            },
            List("startIndex" -> query.startIndex).flatMap { case (k, v) =>
              v.map(vv => k -> Option(vv))
            },
            List("timeoutMs" -> query.timeoutMs.map(s => QueryParamEncoder[Int].encode(s).value))
              .flatMap { case (k, v) => v.map(vv => k -> Option(vv)) },
          ).flatten
        )
      ),
    )
  )
}
object JobsClient {
  final case class CancelParams(
      /** The geographic location of the job. You must specify the location to run the job for the following scenarios:
        * If the location to run a job is not in the `us` or the `eu` multi-regional location
        * If the job's location is in a single region (for example, `us-central1`) For more information, see https://cloud.google.com/bigquery/docs/locations#specifying_your_location.
        */
      location: Option[String] = None
  )
  final case class DeleteParams(
      /** The geographic location of the job. Required. See details at: https://cloud.google.com/bigquery/docs/locations#specifying_your_location.
        */
      location: Option[String] = None
  )
  final case class GetParams(
      /** The geographic location of the job. You must specify the location to run the job for the following scenarios:
        * If the location to run a job is not in the `us` or the `eu` multi-regional location
        * If the job's location is in a single region (for example, `us-central1`) For more information, see https://cloud.google.com/bigquery/docs/locations#specifying_your_location.
        */
      location: Option[String] = None
  )
  final case class ListParams(
      /** Whether to display jobs owned by all users in the project. Default False.
        */
      allUsers: Option[Boolean] = None,
      /** Max value for job creation time, in milliseconds since the POSIX epoch. If set, only jobs created before or at this timestamp are returned.
        */
      maxCreationTime: Option[String] = None,
      /** The maximum number of results to return in a single response page. Leverage the page tokens to iterate through the entire collection.
        */
      maxResults: Option[Int] = None,
      /** Min value for job creation time, in milliseconds since the POSIX epoch. If set, only jobs created after or at this timestamp are returned.
        */
      minCreationTime: Option[String] = None,
      /** Page token, returned by a previous call, to request the next page of results.
        */
      pageToken: Option[String] = None,
      /** If set, show only child jobs of the specified parent. Otherwise, show all top-level jobs.
        */
      parentJobId: Option[String] = None,
      /** Restrict information returned to a set of selected fields
        */
      projection: Option[String] = None,
      /** Filter for job state
        */
      stateFilter: Option[String] = None,
  )
  final case class GetQueryResultsParams(
      /** Optional. Output timestamp as usec int64. Default is false.
        */
      formatOptions_useInt64Timestamp: Option[Boolean] = None,
      /** The geographic location of the job. You must specify the location to run the job for the following scenarios:
        * If the location to run a job is not in the `us` or the `eu` multi-regional location
        * If the job's location is in a single region (for example, `us-central1`) For more information, see https://cloud.google.com/bigquery/docs/locations#specifying_your_location.
        */
      location: Option[String] = None,
      /** Maximum number of results to read.
        */
      maxResults: Option[Int] = None,
      /** Page token, returned by a previous call, to request the next page of results.
        */
      pageToken: Option[String] = None,
      /** Zero-based index of the starting row.
        */
      startIndex: Option[String] = None,
      /** Optional: Specifies the maximum amount of time, in milliseconds, that the client is willing to wait for the query to complete. By default, this limit is 10 seconds (10,000 milliseconds). If the query is complete, the jobComplete field in the response is true. If the query has not yet completed, jobComplete is false. You can request a longer timeout period in the timeoutMs field. However, the call is not guaranteed to wait for the specified timeout; it typically returns after around 200 seconds (200,000 milliseconds), even if the query is not complete. If jobComplete is false, you can continue to wait for the query to complete by calling the getQueryResults method until the jobComplete field in the getQueryResults response is true.
        */
      timeoutMs: Option[Int] = None,
  )
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy