private static List < JsonObject > getJsonObject ( String path , Gson deserializer ) { String url = String . format ( "%s/%s" , ENDPOINT , path ) ; Request request = new Request . Builder ( ) . url ( url ) . build ( ) ; Response response ; try { response = CLIENT . newCall ( request ) . execute ( ) ; ArrayList < JsonObject > objectList = new ArrayList <> ( ) ; String linkHeader = response . headers ( ) . get ( "Link" ) ; if ( linkHeader == null || linkHeader . isEmpty ( ) || path . contains ( "page=" ) ) { objectList . add ( deserializer . fromJson ( response . body ( ) . string ( ) , JsonObject . class ) ) ; return objectList ; } else { int numberOfPages = 0 ; String [ ] linkStrings = linkHeader . split ( DELIM_LINK ) ; List < String [ ] > paramList = new ArrayList <> ( ) ; for ( String link : linkStrings ) { paramList . add ( link . split ( DELIM_LINK_PARAM ) ) ; } for ( String [ ] params : paramList ) { if ( params [ 1 ] . contains ( "last" ) ) { Matcher matcher = Pattern . compile ( "page=[0-9]+" ) . matcher ( params [ 0 ] ) ; numberOfPages = ( matcher . find ( ) ) ? Integer . parseInt ( matcher . group ( ) . substring ( 5 ) ) : 0 ; } } objectList . add ( deserializer . fromJson ( response . body ( ) . string ( ) , JsonObject . class ) ) ; if ( ! url . contains ( "?" ) ) { url += "?" ; } for ( int i = 1 ; i <= numberOfPages ; i ++ ) { request = new Request . Builder ( ) . url ( url + "&page=" + i ) . build ( ) ; response = CLIENT . newCall ( request ) . execute ( ) ; objectList . add ( deserializer . fromJson ( response . body ( ) . string ( ) , JsonObject . class ) ) ; } return objectList ; } } catch ( IOException e ) { throw new HttpRequestFailedException ( e ) ; } }
Deserialize the object to the type expected .