@Override public RequestURI queryParam(final String name, final Object value) { return new SearchURI(this.address.queryParam(name, value)); } @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 Tree getRec( final String sha ) { return new RtTree( this.entry.uri().queryParam("recursive", "1").back(), this.owner, sha ); }
@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 Iterable<Notification> iterate() { return new RtPagination<Notification>( this.request.uri() .queryParam("all", "true") .queryParam("since", "1970-01-01T00:00:00Z") .back(), new RtValuePagination.Mapping<Notification, JsonObject>() { @Override public Notification map(final JsonObject json) { return new RtNotification( Long.valueOf(json.getString("id")) ); } } ); }
@Override public boolean unlock(final String label) throws IOException { final long start = System.currentTimeMillis(); final Response rsp = this.front("unlock") .uri().queryParam("label", label) .queryParam("name", this.lck).back() .method(Request.GET) .fetch(); final boolean unlocked = rsp.status() == HttpURLConnection.HTTP_SEE_OTHER; Logger.info( this, "unlock of \"%s\" is %B in %[ms]s", this.lck, unlocked, System.currentTimeMillis() - start ); return unlocked; }
@Override public Iterable<User> iterate( final String identifier ) { return new RtPagination<User>( this.request.uri().queryParam("since", identifier).back(), new RtValuePagination.Mapping<User, JsonObject>() { @Override public User map(final JsonObject object) { return RtUsers.this.get( String.valueOf(object.getInt("id")) ); } } ); }
@Override public String label() throws IOException { final long start = System.currentTimeMillis(); final String label = this.front("label") .uri().queryParam("name", this.lck).back() .method(Request.GET) .fetch() .body(); Logger.info( this, "label of \"%s\" retrieved in %[ms]s: \"%s\"", this.lck, System.currentTimeMillis() - start, label ); return label; }
@Override public void set(final long value) throws IOException { final long start = System.currentTimeMillis(); this.front("set").method(Request.PUT) .uri().queryParam("value", value).back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK); Logger.info( this, "counter \"%s\" set to %d in %[ms]s", this.label, value, System.currentTimeMillis() - start ); }
@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 void markAsRead(final Date lastread) throws IOException { this.github().entry().uri() .path(RtUser.NOTIF_PATH) .queryParam( "last_read_at", DateTimeFormatter.ISO_INSTANT.format(lastread.toInstant()) ).back() .method(Request.PUT) .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_RESET); }
@Override public Iterable<Comment> iterate(final Date since) { return new RtPagination<Comment>( this.request.uri() .queryParam("since", new Github.Time(since)) .back(), new RtValuePagination.Mapping<Comment, JsonObject>() { @Override public Comment map(final JsonObject object) { return RtComments.this.get(object.getInt("id")); } } ); }
@Override public Iterable<Repo> iterate( final String identifier) { return new RtPagination<Repo>( this.entry.uri().queryParam("since", identifier).back(), new RtValuePagination.Mapping<Repo, JsonObject>() { @Override public Repo map(final JsonObject object) { return RtRepos.this.get( new Coordinates.Simple(object.getString("full_name")) ); } } ); } }
@Override public long incrementAndGet(final long delta) throws IOException { final long start = System.currentTimeMillis(); final long value = Long.parseLong( this.front("increment").method(Request.GET) .uri().queryParam("value", delta).back() .header(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN) .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .body() ); Logger.info( this, "counter \"%s\" incremented by %d to %d in %[ms]s", this.label, delta, value, System.currentTimeMillis() - start ); return value; }
@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 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; }
@Override public String check(final String name) throws IOException { return this.request.fetch() .as(XmlResponse.class) .rel("/page/links/link[@rel='check']/@href") .uri().queryParam("alias", name).back() .fetch() .body(); }
@Override public void write(final InputStream stream, final String ctype, final String etag) throws IOException { this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel("/page/links/link[@rel='upload']/@href") .uri() .queryParam("name", this.name()) .queryParam("ctype", ctype) .queryParam("etag", etag) .back() .body().set(IOUtils.toByteArray(stream)).back() .method(Request.POST) .header( HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED ) .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_SEE_OTHER); Logger.info(this, "attachment '%s' written", this.attachment); }
/** * Get the contents of a file or symbolic link in a repository. * @param path The content path * @param ref The name of the commit/branch/tag. * @return Content fetched * @throws IOException If there is any I/O problem * @see <a href="http://developer.github.com/v3/repos/contents/#get-contents">Get contents</a> */ private Content content( final String path, final String ref ) throws IOException { final String name = "ref"; RtContent content = null; final JsonStructure structure = this.request.method(Request.GET) .uri().path(path).queryParam(name, ref).back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class) .json().read(); if (JsonValue.ValueType.OBJECT.equals(structure.getValueType())) { content = new RtContent( this.entry.uri().queryParam(name, ref).back(), this.owner, ((JsonObject) structure).getString("path") ); } return content; }
@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") ); }