| 1 | 2 | |
| 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 | |
|
| 31 | |
package com.jcabi.github; |
| 32 | |
|
| 33 | |
import com.jcabi.aspects.Immutable; |
| 34 | |
import com.jcabi.aspects.Loggable; |
| 35 | |
import com.jcabi.http.Request; |
| 36 | |
import com.jcabi.http.response.RestResponse; |
| 37 | |
import java.io.IOException; |
| 38 | |
import java.net.HttpURLConnection; |
| 39 | |
import javax.json.JsonObject; |
| 40 | |
import lombok.EqualsAndHashCode; |
| 41 | |
import org.hamcrest.Matchers; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
@Immutable |
| 50 | |
@Loggable(Loggable.DEBUG) |
| 51 | 0 | @EqualsAndHashCode(of = { "entry", "request", "owner" }) |
| 52 | |
@SuppressWarnings("PMD.SingularField") |
| 53 | 2 | final class RtCollaborators implements Collaborators { |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private final transient Request entry; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private final transient Request request; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private final transient Repo owner; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 4 | RtCollaborators(final Request req, final Repo repo) { |
| 76 | 4 | this.entry = req; |
| 77 | 4 | final Coordinates coords = repo.coordinates(); |
| 78 | 4 | this.request = this.entry.uri() |
| 79 | 4 | .path("/repos") |
| 80 | 4 | .path(coords.user()) |
| 81 | 4 | .path(coords.repo()) |
| 82 | 4 | .path("/collaborators") |
| 83 | 4 | .back(); |
| 84 | 4 | this.owner = repo; |
| 85 | 4 | } |
| 86 | |
|
| 87 | |
@Override |
| 88 | |
public Repo repo() { |
| 89 | 0 | return this.owner; |
| 90 | |
} |
| 91 | |
|
| 92 | |
@Override |
| 93 | |
public boolean isCollaborator( |
| 94 | |
final String user) |
| 95 | |
throws IOException { |
| 96 | 3 | return this.request |
| 97 | 1 | .method(Request.GET) |
| 98 | 1 | .uri().path(user).back() |
| 99 | 1 | .fetch() |
| 100 | 1 | .as(RestResponse.class) |
| 101 | 1 | .assertStatus( |
| 102 | 1 | Matchers.isOneOf( |
| 103 | 1 | HttpURLConnection.HTTP_NO_CONTENT, |
| 104 | 1 | HttpURLConnection.HTTP_NOT_FOUND |
| 105 | |
) |
| 106 | 1 | ).status() == HttpURLConnection.HTTP_NO_CONTENT; |
| 107 | |
} |
| 108 | |
|
| 109 | |
@Override |
| 110 | |
public void add( |
| 111 | |
final String user) |
| 112 | |
throws IOException { |
| 113 | 2 | this.request.method(Request.PUT) |
| 114 | 1 | .uri().path(user).back() |
| 115 | 1 | .fetch() |
| 116 | 1 | .as(RestResponse.class) |
| 117 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
| 118 | 1 | } |
| 119 | |
|
| 120 | |
@Override |
| 121 | |
public void remove( |
| 122 | |
final String user |
| 123 | |
) |
| 124 | |
throws IOException { |
| 125 | 2 | this.request.method(Request.DELETE) |
| 126 | 1 | .uri().path(user).back() |
| 127 | 1 | .fetch() |
| 128 | 1 | .as(RestResponse.class) |
| 129 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
| 130 | 1 | } |
| 131 | |
|
| 132 | |
@Override |
| 133 | |
public Iterable<User> iterate() { |
| 134 | 2 | return new RtPagination<User>( |
| 135 | |
this.request, |
| 136 | 3 | new RtValuePagination.Mapping<User, JsonObject>() { |
| 137 | |
@Override |
| 138 | |
public User map(final JsonObject object) { |
| 139 | 4 | return RtCollaborators.this.owner.github().users() |
| 140 | 2 | .get(object.getString("login")); |
| 141 | |
} |
| 142 | |
} |
| 143 | |
); |
| 144 | |
} |
| 145 | |
} |