@Override public Tree getRec( final String sha ) { return new RtTree( this.entry.uri().queryParam("recursive", "1").back(), this.owner, sha ); }
@Override public RequestURI set(final URI uri) { return new SearchURI(this.address.set(uri)); } @Override
/** * Ctor. * @param req RESTful API entry point * @param path Search path * @param keywords Search keywords * @param sort Sort field * @param order Sort order * @param mppng Pagination mapping * @checkstyle ParameterNumber (4 lines) */ RtSearchPagination(final Request req, final String path, final String keywords, final String sort, final String order, final RtValuePagination.Mapping<T, JsonObject> mppng) { this.request = req.uri().path(path) .queryParam("q", keywords) .queryParam("sort", sort) .queryParam("order", order) .back(); this.mapping = mppng; }
@Override public ReleaseAsset upload( final byte[] content, final String type, final String name ) throws IOException { return this.get( this.request.uri() .set(URI.create("https://uploads.github.com")) .path("/repos") .path(this.owner.repo().coordinates().user()) .path(this.owner.repo().coordinates().repo()) .path("/releases") .path(String.valueOf(this.owner.number())) .path("/assets") .queryParam("name", name) .back() .method(Request.POST) .reset(HttpHeaders.CONTENT_TYPE) .header(HttpHeaders.CONTENT_TYPE, type) .body().set(content).back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_CREATED) .as(JsonResponse.class) .json().readObject().getInt("id") ); }
/** * Jump to a new location. * @param uri Destination to jump to * @return New request */ @SuppressWarnings("PMD.UseConcurrentHashMap") public Request jump(final URI uri) { Request req = this.back().uri() .set(this.back().uri().get().resolve(uri)) .back(); final Map<String, List<String>> headers = this.headers(); if (headers.containsKey(HttpHeaders.SET_COOKIE)) { for (final String header : headers.get(HttpHeaders.SET_COOKIE)) { for (final HttpCookie cookie : HttpCookie.parse(header)) { req = req.header( HttpHeaders.COOKIE, String.format( "%s=%s", cookie.getName(), cookie.getValue() ) ); } } } return req; }
@Override public URI get() { return this.address.get(); } @Override
@Override public Iterable<Pull> iterate(final Map<String, String> params) { return new RtPagination<Pull>( this.request.uri().queryParams(params).back(), new RtValuePagination.Mapping<Pull, JsonObject>() { @Override public Pull map(final JsonObject object) { return RtPulls.this.get(object.getInt("number")); } } ); }
@Override public Request back() { return new SearchRequest(this.address.back()); } @Override
@Override public RequestURI queryParam(final String name, final Object value) { return new SearchURI(this.address.queryParam(name, value)); } @Override
@Override public RequestURI port(final int num) { return new SearchURI(this.address.port(num)); } }
@Override public Iterable<Content> iterate( final String path, final String ref ) { return new RtPagination<Content>( this.request.method(Request.GET) .uri().path(path).queryParam("ref", ref).back(), new RtValuePagination.Mapping<Content, JsonObject>() { @Override public Content map(final JsonObject object) { return new RtContent( RtContents.this.entry, RtContents.this.owner, object.getString("path") ); }; } ); }
@Override public String toString() { return this.request.uri().get().toString(); } }
@Override public void delete(final String name) throws IOException { final long start = System.currentTimeMillis(); this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel( String.format( // @checkstyle LineLength (1 line) "/page/counters/counter[name='%s']/links/link[@rel='delete']/@href", name ) ) .uri().queryParam("name", name).back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_SEE_OTHER); Logger.info( this, "counter \"%s\" deleted in %[ms]s", name, System.currentTimeMillis() - start ); }
@Override public Iterable<Fork> iterate( final String sort) { return new RtPagination<Fork>( this.request.uri().queryParam("sort", sort).back(), new RtValuePagination.Mapping<Fork, JsonObject>() { @Override public Fork map(final JsonObject object) { return RtForks.this.get(object.getInt(ID)); } } ); }
@Override public boolean exists(final String path, final String ref) throws IOException { final RestResponse response = this.request.method(Request.GET) .uri().path(path).queryParam("ref", ref).back() .fetch().as(RestResponse.class); return response.status() == HttpURLConnection.HTTP_OK; }
/** * Get user name from Twitter, with the token provided. * @param tkn Twitter access token * @return The user found in Twitter * @throws IOException If fails */ private Identity identity(final String tkn) throws IOException { return parse( this.user .uri() .set( URI.create( new Href(PsTwitter.VERIFY_URL) .with(PsTwitter.ACCESS_TOKEN, tkn) .toString() ) ) .back() .header("accept", "application/json") .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class) .json() .readObject() ); }