class BigQueryHelper {

  public Job insertJobOrFetchDuplicate(String projectId, Job job) throws IOException {
    checkArgument(
        job.getJobReference() != null && job.getJobReference().getJobId() != null,
        "Require non-null JobReference and JobId inside; getJobReference() == '%s'",
        job.getJobReference());
    Job response;
    try {
      response = service.jobs().insert(projectId, job).execute();
      logger.atFine().log("Successfully inserted job '%s'. Response: '%s'", job, response);
    } catch (IOException insertJobException) {
      if (errorExtractor.itemAlreadyExists(insertJobException)) {
        logger.atInfo().withCause(insertJobException).log(
            "Fetching existing job after catching exception for duplicate jobId '%s'",
            job.getJobReference().getJobId());
        try {
          response =
              service
                  .jobs()
                  .get(projectId, job.getJobReference().getJobId())
                  .setLocation(job.getJobReference().getLocation())
                  .execute();
        } catch (IOException getJobException) {
          getJobException.addSuppressed(insertJobException);
          throw new IOException(
              String.format("Failed to get duplicate job '%s'", job), getJobException);
        }
      } else {
        throw new IOException(
            String.format("Unhandled exception trying to insert job '%s'", job),
            insertJobException);
      }
    }
    checkJobIdEquality(job, response);
    return response;
  }

  public  BigQueryHelper(Bigquery service);

}