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 | |
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.RequestURI; |
36 | |
import com.jcabi.http.response.RestResponse; |
37 | |
import java.io.IOException; |
38 | |
import java.net.HttpURLConnection; |
39 | |
import java.util.Map; |
40 | |
import javax.json.JsonObject; |
41 | |
import javax.ws.rs.core.HttpHeaders; |
42 | |
import lombok.EqualsAndHashCode; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
@Immutable |
51 | |
@Loggable(Loggable.DEBUG) |
52 | 0 | @EqualsAndHashCode(of = { "request", "owner", "entry" }) |
53 | |
final class RtRepoCommits implements RepoCommits { |
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 Request comp; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
private final transient Repo owner; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | 8 | RtRepoCommits(final Request req, final Repo repo) { |
81 | 8 | this.entry = req; |
82 | 8 | this.owner = repo; |
83 | 8 | final RequestURI rep = req.uri() |
84 | 7 | .path("/repos") |
85 | 7 | .path(repo.coordinates().user()) |
86 | 8 | .path(repo.coordinates().repo()); |
87 | 8 | this.request = rep |
88 | 7 | .path("/commits") |
89 | 7 | .back(); |
90 | 7 | this.comp = rep |
91 | 7 | .path("/compare") |
92 | 7 | .back(); |
93 | 7 | } |
94 | |
|
95 | |
@Override |
96 | |
public Iterable<RepoCommit> iterate( |
97 | |
final Map<String, String> params |
98 | |
) { |
99 | 3 | return new RtPagination<RepoCommit>( |
100 | 1 | this.request.uri().queryParams(params).back(), |
101 | 2 | new RtValuePagination.Mapping<RepoCommit, JsonObject>() { |
102 | |
@Override |
103 | |
public RepoCommit map(final JsonObject value) { |
104 | 1 | return RtRepoCommits.this.get(value.getString("sha")); |
105 | |
} |
106 | |
} |
107 | |
); |
108 | |
} |
109 | |
|
110 | |
@Override |
111 | |
public RepoCommit get( |
112 | |
final String sha |
113 | |
) { |
114 | 6 | return new RtRepoCommit(this.entry, this.owner, sha); |
115 | |
} |
116 | |
|
117 | |
@Override |
118 | |
public CommitsComparison compare( |
119 | |
final String base, |
120 | |
final String head) { |
121 | 4 | return new RtCommitsComparison(this.entry, this.owner, base, head); |
122 | |
} |
123 | |
|
124 | |
@Override |
125 | |
public String diff( |
126 | |
final String base, |
127 | |
final String head) |
128 | |
throws IOException { |
129 | 3 | return this.comp.reset(HttpHeaders.ACCEPT) |
130 | 1 | .header(HttpHeaders.ACCEPT, "application/vnd.github.v3.diff") |
131 | 1 | .uri() |
132 | 1 | .path(String.format("%s...%s", base, head)) |
133 | 1 | .back() |
134 | 1 | .fetch().as(RestResponse.class) |
135 | 1 | .assertStatus(HttpURLConnection.HTTP_OK) |
136 | 1 | .body(); |
137 | |
} |
138 | |
|
139 | |
@Override |
140 | |
public String patch( |
141 | |
final String base, |
142 | |
final String head) |
143 | |
throws IOException { |
144 | 3 | return this.comp.reset(HttpHeaders.ACCEPT) |
145 | 1 | .header(HttpHeaders.ACCEPT, "application/vnd.github.v3.patch") |
146 | 1 | .uri() |
147 | 1 | .path(String.format("%s...%s", base, head)) |
148 | 1 | .back() |
149 | 1 | .fetch().as(RestResponse.class) |
150 | 1 | .assertStatus(HttpURLConnection.HTTP_OK) |
151 | 1 | .body(); |
152 | |
} |
153 | |
|
154 | |
@Override |
155 | |
public String toString() { |
156 | 0 | return this.request.uri().get().toString(); |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
public JsonObject json() throws IOException { |
161 | 0 | return new RtJson(this.request).fetch(); |
162 | |
} |
163 | |
} |