/**
* Copyright (c) 2013-2014, jcabi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcabi.github;
import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Loggable;
import com.jcabi.http.Request;
import com.jcabi.http.response.JsonResponse;
import com.jcabi.http.response.RestResponse;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Map;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonStructure;
import javax.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
/**
* Github hooks.
* @author Paul Polishchuk (ppol@ua.fm)
* @version $Id$
* @since 0.8
*/
@Immutable
@Loggable(Loggable.DEBUG)
@EqualsAndHashCode(of = { "entry", "owner", "request" })
final class RtHooks implements Hooks {
/**
* API entry point.
*/
private final transient Request entry;
/**
* RESTful request.
*/
private final transient Request request;
/**
* Repository.
*/
private final transient Repo owner;
/**
* Public ctor.
* @param req Request
* @param repo Repository
*/
public RtHooks(final Request req, final Repo repo) {
this.entry = req;
final Coordinates coords = repo.coordinates();
this.request = this.entry.uri()
.path("/repos")
.path(coords.user())
.path(coords.repo())
.path("/hooks")
.back();
this.owner = repo;
}
@Override
@NotNull(message = "repository is never NULL")
public Repo repo() {
return this.owner;
}
@Override
@NotNull(message = "iterable of hooks is never NULL")
public Iterable<Hook> iterate() {
return new RtPagination<Hook>(
this.request,
new RtPagination.Mapping<Hook, JsonObject>() {
@Override
public Hook map(final JsonObject object) {
// @checkstyle MultipleStringLiterals (1 line)
return RtHooks.this.get(object.getInt("id"));
}
}
);
}
@Override
public void remove(final int number) throws IOException {
this.request.method(Request.DELETE)
.uri().path(Integer.toString(number)).back()
.fetch()
.as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
}
@Override
@NotNull(message = "Hook is never NULL")
public Hook get(final int number) {
return new RtHook(this.entry, this.owner, number);
}
@Override
@NotNull(message = "Hook is never NULL")
public Hook create(
@NotNull(message = "name can't be NULL") final String name,
@NotNull(message = "config can't be NULL")
final Map<String, String> config) throws IOException {
final JsonObjectBuilder builder = Json.createObjectBuilder();
for (final Map.Entry<String, String> entr : config.entrySet()) {
builder.add(entr.getKey(), entr.getValue());
}
final JsonStructure json = Json.createObjectBuilder()
.add("name", name)
.add("config", builder)
.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().getInt("id")
);
}
}