Skip to content

Commit

Permalink
supports listing only successful jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
louismrose committed Mar 22, 2024
1 parent 8993483 commit bc68c3d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/zamzar/api/JobsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ protected JobsService(ZamzarClient zamzar) {
this.api = new JobsApi(zamzar.client);
}


/**
* Retrieves a service for managing successful jobs.
*/
public SuccessfulJobsService successful() {
return new SuccessfulJobsService(zamzar);
}

/**
* Retrieves a job by its ID.
*/
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/zamzar/api/SuccessfulJobsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.zamzar.api;

import com.zamzar.api.core.JobsApi;
import com.zamzar.api.internal.Listable;
import com.zamzar.api.invoker.ApiException;
import com.zamzar.api.model.Job;
import com.zamzar.api.model.Jobs;
import com.zamzar.api.pagination.Anchor;
import com.zamzar.api.pagination.Paged;
import com.zamzar.api.pagination.Paging;

import java.util.List;
import java.util.stream.Collectors;

/**
* Retrieves information about existing successful jobs on the Zamzar API servers.
*/
public class SuccessfulJobsService implements Listable<JobManager, Integer> {

protected final ZamzarClient zamzar;
protected final JobsApi api;

protected SuccessfulJobsService(ZamzarClient zamzar) {
this.zamzar = zamzar;
this.api = new JobsApi(zamzar.client);
}

/**
* Retrieves a list of successful jobs.
*
* @param anchor indicates the position in the list from which to start retrieving jobs
* @param limit indicates the maximum number of jobs to retrieve
*/
@Override
public Paged<JobManager, Integer> list(Anchor<Integer> anchor, Integer limit) throws ApiException {
final Integer after = anchor == null ? null : anchor.getAfterParameterValue();
final Integer before = anchor == null ? null : anchor.getBeforeParameterValue();

final Jobs response = api.listSuccessfulJobs(limit, after, before);
return new Paged<>(this, toJobs(response.getData()), Paging.fromNumeric(response.getPaging()));
}

protected List<JobManager> toJobs(List<Job> models) {
return models.stream().map(this::toJob).collect(Collectors.toList());
}

protected JobManager toJob(Job model) {
return new JobManager(zamzar, model);
}
}
10 changes: 10 additions & 0 deletions src/test/java/com/zamzar/api/JobsServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public void list() throws Exception {
}
}

@Test
public void listSuccessful() throws Exception {
final Paged<JobManager, Integer> jobs = zamzar().jobs().successful().list();

for (JobManager job : jobs.getItems()) {
assertTrue(job.getId() > 0);
assertTrue(job.hasSucceeded());
}
}

@Test
public void listAndPageForwards() throws Exception {
// There are at least 3 jobs in the mock server
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/zamzar/api/examples/jobs/RetrieveJobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ public static void main(String[] args) throws ApiException {
// For fine-grained control over pagination, use an anchor and a limit
// For example, retrieve the 20 jobs immediately after job ID 123456
Paged<JobManager, Integer> targetedPage = zamzar.jobs().list(Anchor.after(123456), 20);

// To list or page through **only** successful jobs, use the successful() method:
for (JobManager jobManager : zamzar.jobs().successful().list().getItems()) {
System.out.println("Successful Job ID: " + jobManager.getModel().getId() + " was created at " + jobManager.getModel().getCreatedAt());
}
}
}

0 comments on commit bc68c3d

Please sign in to comment.