Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

AbstractPageRequestHandler.java

abstact class AbstractPageRequestHandler() {
    public List<Pair<String, String>>  getPagingHeaders(ReadFilter filter){
       List<Pair<String, String>> toReturn = new ArrayList<>();
       return toReturn;
    }
    
    public byte[] appendToBody(ReadFilter filter, byte[] body){
      return body;
    }
    
    public URL appendQuery(ReadFilter filter, URL baseUrl){
      return baseUrl;
    }
    
    public ReadFilter getNextPage(HeaderAndBody response, ReadFilter originalFilter) {
      return new ReadFilter();
    }
    
}

AG-Controller Pipe Setup

Android

  Pipeline pipeline = new Pipeline(TODO_URL);
  Pipe taskPipe = pipeline.pipe(Tasks.class);//Defaults to using a PageRequestHandler for AG-Controller

  ReadFilter filter = new ReadFilter();
  filter.setLimitValue(10);
  
  taskPipe.readWithFilter(filter, new CallBack<Task>() {
    onSuccess(List<Task> taskList) {
      ((PagedList)taskList).next(callback);
    }
  });

iOS

JS

// Any items marked as "default" could be left out but included for illustration
var pipe = AeroGear.Pipeline({
    name: "aerogear",
    settings: {
        paging: {
            locatorValue: 0,
            countValue: 10,
        }
    }
}).pipes.aerogear;

Server which uses headers with custom names

Android

  Pipeline pipeline = new Pipeline(SOME_URL);
  PipeConfig dataConfig = new PipeConfig(SOME_URL, Data.class);
  
  dataConfig.setPageRequestHandler(new AbstractPageRequestHandler() {
    
    public List<Pair<String, String>>  getPagingHeaders(ReadFilter filter){
       List<Pair<String, String>> toReturn = new ArrayList<>(4);
       toReturn.add(new Pair("Offset", filter.getOffset());
       toReturn.add(new Pair("Limit", filter.getLimit());
       return toReturn;
    }
    
    public ReadFilter getNextPage(HeaderAndBody response, ReadFilter originalFilter) {
      ReadFilter toReturn = new ReadFilter();
      toReturn.setOffset(response.getHeader("Offset"));
      toReturn.setLimit(originalFilter.getLimit());
      return toReturn;
    }
    
  });
  
  Pipe dataPipe = pipeline.pipe(dataConfig);

  ReadFilter filter = new ReadFilter();
  filter.setLimit(25);
  filter.setOffset("t3_16r4z0");
  dataPipe.readWithFilter(filter, new CallBack<Data>() {
    onSuccess(List<Data> dataList) {
      ((PagedList)dataList).next(callback);
    }
  });

iOS

JS

// Any items marked as "default" could be left out but included for illustration
var pipe = AeroGear.Pipeline({
    name: "customHeaders",
    settings: {
        paging: {
            locatorValue: 0,
            countValue: 10,
            getHeaders: function(pageConfig) {
                return [{"AG-LOCATOR":pageConfig.locatorValue},{"AG-COUNT":pageConfig.countValue}];
            }
        }
    }
}).pipes.customHeaders;

pipe.read({
    success:function(data) {
                data.next(/*more callbacks*/);
    },
    failure:function(error) {
        window.alert("Smithers, release the hounds");
    }
    });

Server which uses query params with custom names

###Android

  Pipeline pipeline = new Pipeline(REDDIT_URL);
  PipeConfig storyConfig = new PipeConfig(REDDIT_URL, Story.class);
  
  storyConfig.setPageRequestHandler(new AbstractPageRequestHandler() {
    @Override 
    public URL appendQuery(ReadFilter filter, URL baseUrl){
      return URLUtil.appendQuery(baseUrl, new Pair("after", filter.getOffset()), new Pair("limit", filter.getLimit()));
    }
    
    public ReadFilter getNextPage(HeaderAndBody response, ReadFilter originalFilter) {
      ReadFilter toReturn = new ReadFilter();
      JSONObject object = JSONUtils.parse(response.getBody());
      toReturn.setOffset(object.get("after"));
      toReturn.setLimit(originalFilter.getLimit());
      return toReturn;
    }
    
  });
  
  Pipe storyPipe = pipeline.pipe(storyConfig);

  ReadFilter filter = new ReadFilter();
  filter.setLimit(25);
  filter.setOffset("t3_16r4z0");
  storyPipe.readWithFilter(filter, new CallBack<Story>() {
    onSuccess(List<Story> taskList) {
      ((PagedList)storyList).next(callback);
    }
  });

###iOS

###JS

Server which uses request body data with custom names

Android

  Pipeline pipeline = new Pipeline(REDDIT_URL);
  PipeConfig storyConfig = new PipeConfig(REDDIT_URL, Story.class);
  
  storyConfig.setPageRequestHandler(new AbstractPageRequestHandler() {
    @Override 
    public byte[] appendToBody(ReadFilter filter, byte[] body){
      JSONObject data = JSONUtils.parse(new String(body));
      data.add("page", filter.getOffset());
      data.add("perPage", filter.getLimit());
      return data.toString().getBytes();
    }
    
    public ReadFilter getNextPage(HeaderAndBody response, ReadFilter originalFilter) {
      ReadFilter toReturn = new ReadFilter();
      JSONObject object = JSONUtils.parse(response.getBody());
      toReturn.setOffset(object.get("after"));
      toReturn.setLimit(originalFilter.getLimit());
      return toReturn;
    }
    
  });
  
  Pipe storyPipe = pipeline.pipe(storyConfig);

  ReadFilter filter = new ReadFilter();
  filter.setLimitValue(25);
  filter.setOffsetValue("t3_16r4z0");
  storyPipe.readWithFilter(filter, new CallBack<Story>() {
    onSuccess(List<Story> taskList) {
      ((PagedList)storyList).next(callback);
    }
  });

iOS

JS

Example which uses all four

Android

  Pipeline pipeline = new Pipeline(REDDIT_URL);
  
  PipeConfig taskConfig = new PipeConfig(REDDIT_URL, Task.class);
  
  taskConfig.setPageRequestHandler(new AbstractPageRequestHandler() {
    
    public List<Pair<String, String>>  getPagingHeaders(ReadFilter filter){
       List<Pair<String, String>> toReturn = new ArrayList<>(4);
       toReturn.add(new Pair("Offset", filter.getOffset());
       toReturn.add(new Pair("Limit", filter.getLimit());
       return toReturn;
    }
    
    public ReadFilter getNextPage(HeaderAndBody response, ReadFilter originalFilter) {
      ReadFilter toReturn = new ReadFilter();
      toReturn.setOffset(response.getHeader("Offset"));
      toReturn.setLimit(originalFilter.getLimit());
      return toReturn;
    }

  
  PipeConfig dataConfig = new PipeConfig(REDDIT_URL, Data.class);
  
  dataConfig.setPageRequestHandler(new AbstractPageRequestHandler() {
    @Override 
    public byte[] appendToBody(ReadFilter filter, byte[] body){
      JSONObject data = JSONUtils.parse(new String(body));
      data.add("page", filter.getOffset());
      data.add("perPage", filter.getLimit());
      return data.toString().getBytes();
    }
    
    public ReadFilter getNextPage(HeaderAndBody response, ReadFilter originalFilter) {
      ReadFilter toReturn = new ReadFilter();
      JSONObject object = JSONUtils.parse(response.getBody());
      toReturn.setOffset(object.get("after"));
      toReturn.setLimit(originalFilter.getLimit());
      return toReturn;
    }
    
    
  });
  
  PipeConfig storyConfig = new PipeConfig(REDDIT_URL, Story.class);
  
  storyConfig.setPageRequestHandler(new AbstractPageRequestHandler() {
    @Override 
    public URL appendQuery(ReadFilter filter, URL baseUrl){
      return URLUtil.appendQuery(baseUrl, new Pair("after", filter.getOffset()), new Pair("limit", filter.getLimit()));
    }
    
    public ReadFilter getNextPage(HeaderAndBody response, ReadFilter originalFilter) {
      ReadFilter toReturn = new ReadFilter();
      JSONObject object = JSONUtils.parse(response.getBody());
      toReturn.setOffset(object.get("after"));
      toReturn.setLimit(originalFilter.getLimit());
      return toReturn;
    }
    
  });
  
  Pipe dataPipe = pipeline.pipe(dataConfig);
  Pipe storyPipe = pipeline.pipe(storyConfig);
  Pipe taskPipe = pipeline.pipe(taskConfig);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment