@Override public void react(final Reaction reaction) throws IOException { final JsonStructure json = Json.createObjectBuilder() .add(RtIssue.CONTENT, reaction.type()) .build(); this.request.method(Request.POST) .body().set(json).back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK); }
/** * Retrieve Google access token. * @param code Google "authorization code" * @return The token * @throws IOException If fails */ private String token(final String code) throws IOException { return new JdkRequest("https://accounts.google.com/o/oauth2/token") .body() .formParam("client_id", this.app) .formParam("redirect_uri", this.resource.uriInfo().getBaseUri()) .formParam("client_secret", this.secret) .formParam("grant_type", "authorization_code") .formParam("code", code) .back() .header( HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED ) .method(Request.POST) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class).json() .readObject() // @checkstyle MultipleStringLiterals (1 line) .getString("access_token"); }
@Override public RequestBody formParams(final Map<String, String> params) { RequestBody body = this; for (final Map.Entry<String, String> param : params.entrySet()) { body = body.formParam(param.getKey(), param.getValue()); } return body; } }
@Override public boolean isLocked() { boolean locked = false; try { locked ^= this.request.method(Request.PUT).uri().path("/lock").back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_NO_CONTENT).back().body() .get().isEmpty(); } catch (final IOException error) { locked = false; } return locked; }
@Override public RequestBody formParams(final Map<String, String> params) { RequestBody body = this; for (final Map.Entry<String, String> param : params.entrySet()) { body = body.formParam(param.getKey(), param.getValue()); } return body; }
@Override public void react(final Reaction reaction) throws IOException { final JsonStructure json = Json.createObjectBuilder() .add(RtComment.CONTENT, reaction.type()) .build(); this.request.method(Request.POST) .body().set(json).back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK); }
@Override public boolean lock(final String label) throws IOException { final long start = System.currentTimeMillis(); final Response rsp = this.front("lock") .body().formParam("label", label) .formParam("name", this.lck).back() .method(Request.POST) .fetch(); final boolean locked = rsp.status() == HttpURLConnection.HTTP_SEE_OTHER; Logger.info( this, "lock of \"%s\" is %B in %[ms]s: %s", this.lck, locked, System.currentTimeMillis() - start, rsp.body() ); return locked; }
/** * Patch it. * @param json JSON to use for patching * @throws IOException If fails */ public void patch( final JsonObject json ) throws IOException { final StringWriter post = new StringWriter(); Json.createWriter(post).writeObject(json); this.request.body().set(post.toString()).back() .method(Request.PATCH) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK); }
/** * Retrieve PsLinkedin access token. * @param home Home of this page * @param code PsLinkedin "authorization code" * @return The token * @throws IOException If failed */ private String token(final String home, final String code) throws IOException { final String uri = this.tkhref.toString(); return new JdkRequest(uri) .method("POST") .header("Accept", "application/xml") .body() .formParam("grant_type", "authorization_code") .formParam("client_id", this.app) .formParam("redirect_uri", home) .formParam("client_secret", this.key) .formParam(PsLinkedin.CODE, code) .back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class) .json().readObject().getString("access_token"); }
@Override public void lock(final String reason) { final JsonStructure json = Json.createObjectBuilder() .add("lock_reason", reason) .build(); try { this.request.method(Request.PUT).uri().path("/lock").back() .body().set(json).back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); } catch (final IOException error) { throw new IllegalStateException(error); } }
/** * Retrieve PsLinkedin access token. * @param home Home of this page * @param code PsLinkedin "authorization code" * @return The token * @throws IOException If failed */ private String token(final String home, final String code) throws IOException { final String uri = this.tkhref.toString(); return new JdkRequest(uri) .method("POST") .header("Accept", "application/xml") .body() .formParam("grant_type", "authorization_code") .formParam("client_id", this.app) .formParam("redirect_uri", home) .formParam("client_secret", this.key) .formParam(PsLinkedin.CODE, code) .back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class) .json().readObject().getString("access_token"); }
@Override public Label create( final String name, final String color) throws IOException { final JsonStructure json = Json.createObjectBuilder() // @checkstyle MultipleStringLiterals (1 line) .add("name", name) .add("color", color) .build(); this.request.method(Request.POST) .body().set(json).back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_CREATED) .as(JsonResponse.class) .json(); return new RtLabel(this.entry, this.owner, name); }
/** * Retrieve Google access token. * @param code Google "authorization code" * @return The token * @throws IOException If failed */ private String token(final String code) throws IOException { return new JdkRequest( new Href(this.gauth).path("o").path("oauth2").path("token") .toString() ).body() .formParam("client_id", this.app) .formParam("redirect_uri", this.redir) .formParam("client_secret", this.key) .formParam("grant_type", "authorization_code") .formParam(PsGoogle.CODE, code) .back() .header("Content-Type", "application/x-www-form-urlencoded") .method(com.jcabi.http.Request.POST) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class).json() .readObject() .getString(PsGoogle.ACCESS_TOKEN); }
/** * Helper method for merge operations. * @param payload The JSON payload for the merge * @return Response received from GitHub * @throws IOException If there is any I/O problem */ private RestResponse merge(final JsonStructure payload) throws IOException { return this.request.uri() .path("/merge").back() .body().set(payload).back() .method(Request.PUT) .fetch() .as(RestResponse.class); }
/** * Retrieve Google access token. * @param code Google "authorization code" * @return The token * @throws IOException If failed */ private String token(final String code) throws IOException { return new JdkRequest( new Href(this.gauth).path("o").path("oauth2").path("token") .toString() ).body() .formParam("client_id", this.app) .formParam("redirect_uri", this.redir) .formParam("client_secret", this.key) .formParam("grant_type", "authorization_code") .formParam(PsGoogle.CODE, code) .back() .header("Content-Type", "application/x-www-form-urlencoded") .method(com.jcabi.http.Request.POST) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class).json() .readObject() .getString(PsGoogle.ACCESS_TOKEN); }
@Override public void remove(final Iterable<String> emails) throws IOException { final JsonArrayBuilder json = Json.createArrayBuilder(); for (final String email : emails) { json.add(email); } this.request.method(Request.DELETE) .body().set(json.build()).back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); }
/** * Retrieve Github access token. * @param home Home of this page * @param code Github "authorization code" * @return The token * @throws IOException If failed */ private String token(final String home, final String code) throws IOException { final String uri = new Href(this.github) .path(PsGithub.LOGIN).path("oauth").path(PsGithub.ACCESS_TOKEN) .toString(); return new JdkRequest(uri) .method("POST") .header("Accept", "application/xml") .body() .formParam("client_id", this.app) .formParam("redirect_uri", home) .formParam("client_secret", this.key) .formParam(PsGithub.CODE, code) .back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .assertXPath("/OAuth/access_token") .xml() .xpath("/OAuth/access_token/text()") .get(0); }
@Override public Gist create(final Map<String, String> files, final boolean visible ) throws IOException { JsonObjectBuilder builder = Json.createObjectBuilder(); for (final Map.Entry<String, String> file : files.entrySet()) { builder = builder.add( file.getKey(), Json.createObjectBuilder().add("content", file.getValue()) ); } final JsonStructure json = Json.createObjectBuilder() .add("files", builder) .add("public", visible) .build(); return this.get( this.request.method(Request.POST) .body().set(json).back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_CREATED) .as(JsonResponse.class) .json().readObject().getString("id") ); }
/** * Retrieve Github access token. * @param home Home of this page * @param code Github "authorization code" * @return The token * @throws IOException If failed */ private String token(final String home, final String code) throws IOException { final String uri = new Href(this.github) .path(PsGithub.LOGIN).path("oauth").path(PsGithub.ACCESS_TOKEN) .toString(); return new JdkRequest(uri) .method("POST") .header("Accept", "application/xml") .body() .formParam("client_id", this.app) .formParam("redirect_uri", home) .formParam("client_secret", this.key) .formParam(PsGithub.CODE, code) .back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .assertXPath("/OAuth/access_token") .xml() .xpath("/OAuth/access_token/text()") .get(0); }
@Override public Reference create( final String ref, final String sha ) throws IOException { final JsonObject json = Json.createObjectBuilder() .add("sha", sha).add(RtReferences.REF, ref).build(); return this.get( this.request.method(Request.POST) .body().set(json).back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_CREATED) .as(JsonResponse.class) .json().readObject().getString(RtReferences.REF) ); }