forked from square/retrofit
/
GitHub.java
380 lines (321 loc) · 15 KB
/
GitHub.java
1
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright (C) 2015 8tory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.github.retrofit2;
import retrofit.http.Retrofit.*;
import retrofit.http.Retrofit;
import rx.Observable;
import java.io.File;
import retrofit.converter.*;
import java.util.List;
import rx.functions.*;
import retrofit.mime.TypedFile;
import retrofit.mime.TypedString;
import com.github.mobile.model.*;
import retrofit.client.Response;
import retrofit.Callback;
import android.app.Activity;
import java.util.Map;
import java.util.HashMap;
@Retrofit("https://api.github.com")
@retrofit.http.Retrofit.Headers({ // optional
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit2"
})
@RetryHeaders( // optional
value = "Cache-Control: max-age=640000",
exceptions = retrofit.http.RequestException.class
)
@Retrofit.Converter(GsonConverter.class) // optional
//@Retrofit.Converter(LoganSquareConverter.class) // default
@LogLevel(retrofit.RestAdapter.LogLevel.FULL) // optional
@RequestInterceptor(GitHubRequestInterceptor.class) // optional
@ErrorHandler(GitHubErrorHandler.class) // optional
@Retrofit.OkHttpClient(GitHubOkHttpClienter.class) // optional
public abstract class GitHub {
@GET("/repos/{owner}/{repo}/contributors")
//@Authenticator(GitHubAuthenticator.class)
@RequestInterceptor(GitHubAuthInterceptor.class)
public abstract Observable<List<Contributor>> contributorList(
@Path("owner") String owner,
@Path("repo") String repo);
public Observable<Contributor> contributors(
String owner,
String repo) {
return contributorList(owner, repo).flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
@Override public Observable<Contributor> call(List<Contributor> list) {
return Observable.from(list);
}
});
}
@GET("/repos/{owner}/{repo}/contributors")
public abstract Observable<List<Contributor>> contributorList(
@Path("owner") String owner,
@Path("repo") String repo,
@QueryMap Map<String, String> options);
public Observable<Contributor> contributors(
String owner,
String repo,
Map<String, String> options) {
return contributorList(owner, repo, options).flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
@Override public Observable<Contributor> call(List<Contributor> list) {
return Observable.from(list);
}
});
}
@GET("/repos/{owner}/{repo}/contributors")
public abstract Observable<List<Contributor>> contributorListWithoutAuth(
@Path("owner") String owner,
@Path("repo") String repo);
@GET("/repos/{owner}/{repo}/contributors")
public abstract Observable<List<Contributor>> noPathMatch(
@Path("o") String owner,
@Path("r") String repo);
@GET("/repos/{repo}/contributors")
public abstract Observable<List<Contributor>> noPathMatch2(
@Path("o") String owner,
@Path("repo") String repo);
@GET("/repos/contributors")
public abstract Observable<List<Contributor>> noPathMatch3(
@Path("owner") String owner,
@Path("repo") String repo);
@GET("/repos/{owner}/{repo}/contributors")
public abstract Observable<List<Contributor>> noPathMatch4(String owner, String repo);
@GET("/repos/{owner}/{repo}/contributors")
public abstract Observable<List<Contributor>> noPathMatch5();
public Observable<Contributor> contributorsWithoutAuth(
String owner,
String repo) {
return contributorListWithoutAuth(owner, repo).flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
@Override public Observable<Contributor> call(List<Contributor> list) {
return Observable.from(list);
}
});
}
@GET("https://api.github.com/repos/{owner}/{repo}/contributors")
public abstract Observable<List<Contributor>> contributorListWithoutBaseUrl(
@Path("owner") String owner,
@Path("repo") String repo);
public Observable<Contributor> contributorsWithoutBaseUrl(
String owner,
String repo) {
return contributorListWithoutBaseUrl(owner, repo).flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
@Override public Observable<Contributor> call(List<Contributor> list) {
return Observable.from(list);
}
});
}
@GET("{url}")
public abstract Observable<List<Contributor>> contributorListDynamic(@Path("url") String url);
public Observable<Contributor> contributorsDynamic(String url) {
return contributorListDynamic(url).flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
@Override public Observable<Contributor> call(List<Contributor> list) {
return Observable.from(list);
}
});
}
@POST("/user/edit")
public abstract Observable<Contributor> updateUser(@Body Contributor user);
@PUT("/user/edit")
public abstract Observable<Contributor> putUser(@Body Contributor user);
@PATCH("/user/edit")
public abstract Observable<Contributor> patchUserByBody(@Body Contributor user);
@FormUrlEncoded
@POST("/user/edit")
public abstract Observable<Contributor> updateUser(@Field("first_name") String first, @Field("last_name") String last);
@FormUrlEncoded
@PATCH("/user/edit")
public abstract Observable<Contributor> patchUserByField(@Field("first_name") String first, @Field("last_name") String last);
@Multipart
@PUT("/user/photo")
public abstract Observable<Contributor> updateUser(@retrofit.http.Retrofit.Part(value = "photo", mimeType = "image/png") File photo, @Part("description") String description);
@Multipart
@PUT("/user/photo")
public abstract Observable<Contributor> updateUserWithTypedFile(@Part("photo") TypedFile photo, @Part("description") String description);
@Multipart
@PUT("/user/photo")
public abstract Observable<Contributor> updateUserWithTypedStringAnootation(@Part("photo") TypedFile photo, @retrofit.http.Retrofit.Part(value = "description", mimeType = "application/json") String description);
@Multipart
@PUT("/user/photo")
public abstract Observable<Contributor> updateUserWithTypedString(@Part("photo") TypedFile photo, @Part("description") TypedString description);
@Multipart
@PATCH("/user/photo")
public abstract Observable<Contributor> patchUserWithTypedString(@Part("photo") TypedFile photo, @Part("description") TypedString description);
@Headers("Cache-Control: max-age=640000")
@GET("/widget/list")
public abstract Observable<Contributor> widgetList();
@Headers({
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit2"
})
@GET("/users/{username}")
public abstract Observable<Contributor> getUser(@Path("username") String username);
@Headers({
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit2"
})
@GET("/user")
public abstract Observable<Contributor> getUserWithAuthorization(@Header("Authorization") String authorization);
@GET("/user")
public abstract Observable<Contributor> getUserWithAuthorization2(@Header("Authorization") String authorization);
@Headers({
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit2"
})
@DELETE("/user")
public abstract Observable<Contributor> deleteUserWithAuthorization(@Header("Authorization") String authorization);
@DELETE("/user")
public abstract Observable<Contributor> deleteUserWithAuthorization2(@Header("Authorization") String authorization);
@Headers({
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit2"
})
@PUT("/user/starred/{owner}/{repo}")
public abstract Observable<Contributor> star(@Query("access_token") String accessToken,
@Path("owner") String owner,
@Path("repo") String repo);
@Headers({
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit2"
})
@DELETE("/user/starred/{owner}/{repo}")
public abstract Observable<Contributor> unstar(@Query("access_token") String accessToken,
@Path("owner") String owner,
@Path("repo") String repo);
@DELETE("/repos/{owner}/{repo}")
public abstract Observable<Contributor> deleteRepository(
@Header("Authorization") String basicCredentials,
@Path("owner") String owner,
@Path("repo") String repo);
@GET("/authorizations")
public abstract Observable<Authorization> getAuthorizations(@Header("Authorization") String token);
@POST("/authorizations")
public abstract Observable<Authorization> createDeleteAuthorization(@Header("Authorization") String basicCredentials,
@Body Authorization authorization);
@GET("/users/{username}/repos")
@QueryBinding(value = "access_token", binder = AuthorizationBinder.class)
public abstract Observable<Repository> repositoriesBinder(@Header("username") String username);
//@GET("/users/repos") // TODO intercepter // TODO header-binding
//public abstract Observable<Repository> repositories();
public static class AuthorizationBinder implements Bindable<Authorization> {
@Override public String call(Authorization authorization) {
return authorization.getToken();
}
}
@GET("/repos/{owner}/{repo}/contributors")
public abstract Observable<Response> contributorResponse(
@Path("owner") String owner,
@Path("repo") String repo);
@GET("/repos/{owner}/{repo}/contributors")
public abstract void contributorResponse(
@Path("owner") String owner,
@Path("repo") String repo, Callback<Response> callback);
@GET("/repos/{owner}/{repo}/contributors")
public abstract void contributorList(
@Path("owner") String owner,
@Path("repo") String repo, Callback<List<Contributor>> callback);
@GET("/repos/{owner}/{repo}/contributors")
public abstract List<Contributor> contributorListBlocking(
@Path("owner") String owner,
@Path("repo") String repo);
@GET("/repos/{owner}/{repo}/contributors")
public abstract Response contributorResponseBlocking(
@Path("owner") String owner,
@Path("repo") String repo);
@POST("/user/repos")
public abstract Observable<Repository> createRepository(@Body Repository repository);
@DELETE("/repos/{owner}/{repo}")
public abstract Response deleteRepository(
@Path("owner") String owner,
@Path("repo") String repo);
@POST("/orgs/{org}/repos")
public abstract Observable<Repository> createRepository(@Query("org") String org, @Body Repository repository);
@Retrofit.Converter(GsonConverter.class) // optional
@GET("/repos/{owner}/{repo}/contributors")
public abstract Observable<List<Contributor>> contributorListWithGson(
@Path("owner") String owner,
@Path("repo") String repo);
public Observable<Contributor> contributorsWithGson(
String owner,
String repo) {
return contributorListWithGson(owner, repo).flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
@Override public Observable<Contributor> call(List<Contributor> list) {
return Observable.from(list);
}
});
}
@Retrofit.Converter(DateGsonConverter.class) // optional
@GET("/repos/{owner}/{repo}/contributors")
public abstract Observable<List<Contributor>> contributorListWithDateGson(
@Path("owner") String owner,
@Path("repo") String repo);
public Observable<Contributor> contributorsWithDateGson(
String owner,
String repo) {
return contributorListWithoutAuth(owner, repo).flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
@Override public Observable<Contributor> call(List<Contributor> list) {
return Observable.from(list);
}
});
}
public static class DateGsonConverter extends GsonConverter {
public DateGsonConverter() {
super(new com.google.gson.GsonBuilder()
.setFieldNamingPolicy(com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(java.util.Date.class, new com.google.gson.internal.bind.DateTypeAdapter())
.create());
}
}
@GET("/users/{username}/repos")
public abstract Observable<List<Repository>> repositoryList(@Path("username") String username);
public Observable<Repository> repositories(String username) {
return repositoryList(username).flatMap(new Func1<List<Repository>, Observable<Repository>>() {
@Override public Observable<Repository> call(List<Repository> list) {
return Observable.from(list);
}
});
}
@GET("/orgs/{org}/repos")
public abstract Observable<List<Repository>> orgRepositoryList(@Path("org") String org);
public Observable<Repository> orgRepositories(String org) {
return orgRepositoryList(org).flatMap(new Func1<List<Repository>, Observable<Repository>>() {
@Override public Observable<Repository> call(List<Repository> list) {
return Observable.from(list);
}
});
}
public static GitHub create() {
return new Retrofit_GitHub();
}
// promise parameter
public static GitHub create(Activity activity) {
return builder().context(activity).build();
}
@Retrofit.Builder
public abstract static class Builder {
public abstract Builder baseUrl(String baseUrl);
public abstract Builder converter(retrofit.converter.Converter converter);
public abstract Builder requestInterceptor(retrofit.RequestInterceptor requestInterceptor);
public abstract Builder errorHandler(retrofit.ErrorHandler errorHandler);
public abstract Builder headers(String... headers);
public abstract Builder retryHeaders(String... headers);
public abstract Builder logLevel(retrofit.RestAdapter.LogLevel logLevel);
public abstract Builder context(Object context);
//public abstract Builder cache(Cache cache);
//public abstract Builder okHttpClient(OkHttpClient client);
public abstract GitHub build();
}
public static Builder builder() {
return new Retrofit_GitHub.Builder();
}
}