| 1 | 0 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
package com.jcabi.github; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.aspects.Loggable; |
| 34 | |
import com.jcabi.http.Request; |
| 35 | |
import com.jcabi.http.response.JsonResponse; |
| 36 | |
import com.jcabi.http.response.RestResponse; |
| 37 | |
import java.io.IOException; |
| 38 | |
import java.net.HttpURLConnection; |
| 39 | |
import javax.json.Json; |
| 40 | |
import javax.json.JsonObject; |
| 41 | |
import javax.json.JsonStructure; |
| 42 | |
import lombok.EqualsAndHashCode; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
@Immutable |
| 52 | |
@Loggable(Loggable.DEBUG) |
| 53 | 0 | @EqualsAndHashCode(of = { "entry", "owner" }) |
| 54 | 4 | final class RtLabels implements Labels { |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
private final transient Request entry; |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
private final transient Request request; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
private final transient Repo owner; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | 5 | RtLabels(final Request req, final Repo repo) { |
| 77 | 5 | this.owner = repo; |
| 78 | 5 | final Coordinates coords = repo.coordinates(); |
| 79 | 5 | this.entry = req; |
| 80 | 5 | this.request = req.uri() |
| 81 | 5 | .path("/repos") |
| 82 | 5 | .path(coords.user()) |
| 83 | 5 | .path(coords.repo()) |
| 84 | 5 | .path("/labels") |
| 85 | 5 | .back(); |
| 86 | 5 | } |
| 87 | |
|
| 88 | |
@Override |
| 89 | |
public String toString() { |
| 90 | 0 | return this.request.uri().get().toString(); |
| 91 | |
} |
| 92 | |
|
| 93 | |
@Override |
| 94 | |
public Repo repo() { |
| 95 | 0 | return this.owner; |
| 96 | |
} |
| 97 | |
|
| 98 | |
@Override |
| 99 | |
public Label create( |
| 100 | |
final String name, |
| 101 | |
final String color) |
| 102 | |
throws IOException { |
| 103 | 2 | final JsonStructure json = Json.createObjectBuilder() |
| 104 | |
|
| 105 | 1 | .add("name", name) |
| 106 | 1 | .add("color", color) |
| 107 | 1 | .build(); |
| 108 | 1 | this.request.method(Request.POST) |
| 109 | 1 | .body().set(json).back() |
| 110 | 1 | .fetch() |
| 111 | 1 | .as(RestResponse.class) |
| 112 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
| 113 | 1 | .as(JsonResponse.class) |
| 114 | 1 | .json(); |
| 115 | 1 | return new RtLabel(this.entry, this.owner, name); |
| 116 | |
} |
| 117 | |
|
| 118 | |
@Override |
| 119 | |
public Label get(final String name) { |
| 120 | 2 | return new RtLabel(this.entry, this.owner, name); |
| 121 | |
} |
| 122 | |
|
| 123 | |
@Override |
| 124 | |
public void delete(final String name) throws IOException { |
| 125 | 2 | this.request.method(Request.DELETE) |
| 126 | 1 | .uri().path(name).back() |
| 127 | 1 | .fetch() |
| 128 | 1 | .as(RestResponse.class) |
| 129 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
| 130 | 1 | } |
| 131 | |
|
| 132 | |
@Override |
| 133 | |
public Iterable<Label> iterate() { |
| 134 | 2 | return new RtPagination<Label>( |
| 135 | |
this.request, |
| 136 | 3 | new RtValuePagination.Mapping<Label, JsonObject>() { |
| 137 | |
@Override |
| 138 | |
public Label map(final JsonObject object) { |
| 139 | 4 | return new RtLabel( |
| 140 | 2 | RtLabels.this.entry, |
| 141 | 2 | RtLabels.this.owner, |
| 142 | 2 | object.getString("name") |
| 143 | |
); |
| 144 | |
} |
| 145 | |
} |
| 146 | |
); |
| 147 | |
} |
| 148 | |
|
| 149 | |
} |