Skip to content

Commit 27c0a21

Browse files
author
David Weiser
committed
Updates Retrofit Gerrit example
1 parent 52e3a2e commit 27c0a21

File tree

8 files changed

+352
-43
lines changed

8 files changed

+352
-43
lines changed

com.vogella.java.retrofitgerrit/.project

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
<arguments>
1111
</arguments>
1212
</buildCommand>
13-
<buildCommand>
14-
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
15-
<arguments>
16-
</arguments>
17-
</buildCommand>
1813
</buildSpec>
1914
<natures>
2015
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>

com.vogella.java.retrofitgerrit/build.gradle

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,23 @@ repositories {
1717
jcenter()
1818
}
1919

20+
jar {
21+
manifest {
22+
attributes 'Main-Class' : 'com.vogella.java.retrofitgerrit.StartUp'
23+
}
24+
}
25+
2026
// In this section you declare the dependencies for your production and test code
2127
dependencies {
2228
// The production code uses the SLF4J logging API at compile time
2329

2430
compile 'org.slf4j:slf4j-api:1.7.21'
25-
compile 'com.squareup.retrofit2:retrofit:2.1.0'
26-
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
31+
compile 'com.squareup.retrofit2:retrofit:2.2.0'
32+
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
33+
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
2734
compile 'com.google.code.gson:gson:2.6.1'
35+
compile 'org.eclipse.swt:org.eclipse.swt.gtk.linux.x86_64:4.3'
36+
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
2837

2938
// Declare the dependency for your favourite test framework you want to use in your tests.
3039
// TestNG is also supported by the Gradle Test task. Just change the
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.vogella.java.retrofitgerrit;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
public class Account {
6+
7+
@SerializedName("_account_id")
8+
private String accountId;
9+
10+
public String getAccountId() {
11+
return accountId;
12+
}
13+
14+
public void setAccountId(String accountId) {
15+
this.accountId = accountId;
16+
}
17+
}
Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,58 @@
11
package com.vogella.java.retrofitgerrit;
22

3+
import com.google.gson.annotations.SerializedName;
4+
35
public class Change {
4-
String subject;
6+
7+
@SerializedName("change_id")
8+
private String changeId;
9+
10+
private String subject;
11+
12+
@SerializedName("current_revision")
13+
private String currentRevisionId;
14+
15+
private String project;
16+
17+
private Account owner;
518

619
public String getSubject() {
720
return subject;
821
}
922

1023
public void setSubject(String subject) {
1124
this.subject = subject;
25+
}
26+
27+
public String getCurrentRevision() {
28+
return currentRevisionId == null ? "" : currentRevisionId;
29+
}
30+
31+
public void setCurrentRevision(String currentRevision) {
32+
this.currentRevisionId = currentRevision;
33+
}
34+
35+
public String getProject() {
36+
return project;
37+
}
38+
39+
public void setProject(String project) {
40+
this.project = project;
41+
}
42+
43+
public Account getOwner() {
44+
return owner;
45+
}
46+
47+
public void setOwner(Account owner) {
48+
this.owner = owner;
49+
}
50+
51+
public String getChangeId() {
52+
return changeId;
53+
}
54+
55+
public void setChangeId(String changeId) {
56+
this.changeId = changeId;
1257
}
1358
}

com.vogella.java.retrofitgerrit/src/main/java/com/vogella/java/retrofitgerrit/Controller.java

Lines changed: 83 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,100 @@
55
import com.google.gson.Gson;
66
import com.google.gson.GsonBuilder;
77

8-
import retrofit2.Call;
9-
import retrofit2.Callback;
10-
import retrofit2.Response;
8+
import io.reactivex.disposables.CompositeDisposable;
9+
import io.reactivex.observers.DisposableSingleObserver;
10+
import io.reactivex.schedulers.Schedulers;
11+
import okhttp3.OkHttpClient;
12+
import okhttp3.ResponseBody;
13+
import okhttp3.logging.HttpLoggingInterceptor;
1114
import retrofit2.Retrofit;
15+
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
1216
import retrofit2.converter.gson.GsonConverterFactory;
1317

14-
public class Controller implements Callback<List<Change>> {
18+
public class Controller {
1519

16-
static final String BASE_URL = "https://git.eclipse.org/r/";
20+
private static final String BASE_URL = "https://git.eclipse.org/r/";
21+
private GerritAPI gerritAPI;
22+
private CompositeDisposable compositeDisposable = new CompositeDisposable();
23+
private UserInterface userInterface;
24+
protected List<Change> currentData;
1725

18-
public void start() {
19-
Gson gson = new GsonBuilder()
20-
.setLenient()
21-
.create();
22-
23-
Retrofit retrofit = new Retrofit.Builder()
24-
.baseUrl(BASE_URL)
25-
.addConverterFactory(GsonConverterFactory.create(gson))
26-
.build();
26+
public Controller(UserInterface userInterface) {
27+
this.userInterface = userInterface;
28+
initGerritApi();
29+
}
30+
31+
private void initGerritApi() {
32+
HttpLoggingInterceptor loggerInterceptor = new HttpLoggingInterceptor();
33+
// loggerInterceptor.setLevel(Level.BODY);
34+
35+
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(loggerInterceptor).build();
2736

28-
GerritAPI gerritAPI = retrofit.create(GerritAPI.class);
37+
Gson gson = new GsonBuilder().setLenient().create();
2938

30-
Call<List<Change>> call = gerritAPI.loadChanges("status:open");
31-
call.enqueue(this);
39+
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
40+
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
41+
.addConverterFactory(GsonConverterFactory.create(gson)).client(okHttpClient).build();
42+
43+
gerritAPI = retrofit.create(GerritAPI.class);
44+
}
45+
46+
public void loadAllChanges() {
47+
compositeDisposable.add(gerritAPI.getAllChanges().subscribeOn(Schedulers.io()).observeOn(Schedulers.single())
48+
.subscribeWith(getChangesObserver()));
49+
}
50+
51+
public void loadChangesForProject(String project) {
52+
String url = BASE_URL + "changes/?o=CURRENT_REVISION&o=DETAILED_ACCOUNTS&q=status:open+project:"
53+
+ project.replace("/", "%2F");
54+
compositeDisposable.add(gerritAPI.getChangesForProject(url).subscribeOn(Schedulers.io())
55+
.observeOn(Schedulers.single()).subscribeWith(getChangesObserver()));
56+
}
3257

58+
public void loadChangesForUser(int userId) {
59+
String url = BASE_URL + "changes/?o=CURRENT_REVISION&o=DETAILED_ACCOUNTS&q=status:open+owner:" + userId;
60+
compositeDisposable.add(gerritAPI.getChangesForUser(url).subscribeOn(Schedulers.io())
61+
.observeOn(Schedulers.single()).subscribeWith(getChangesObserver()));
3362
}
3463

35-
@Override
36-
public void onResponse(Call<List<Change>> call, Response<List<Change>> response) {
37-
if(response.isSuccessful()) {
38-
List<Change> changesList = response.body();
39-
changesList.forEach(change -> System.out.println(change.subject));
40-
} else {
41-
System.out.println(response.errorBody());
42-
}
64+
public void upvote(int i) {
65+
Change currentChange = currentData.get(i);
66+
// compositeDisposable.add(gerritAPI.postUpvote(currentChange.getChangeId(),
67+
// currentChange.getCurrentRevision())
68+
// .subscribeOn(Schedulers.io()).observeOn(Schedulers.single()).subscribeWith(getUpvoteObserver()));
69+
}
70+
71+
private DisposableSingleObserver<List<Change>> getChangesObserver() {
72+
return new DisposableSingleObserver<List<Change>>() {
73+
74+
@Override
75+
public void onSuccess(List<Change> value) {
76+
Controller.this.currentData = value;
77+
userInterface.updateList(value);
78+
}
79+
80+
@Override
81+
public void onError(Throwable e) {
82+
e.printStackTrace();
83+
}
84+
};
85+
}
86+
87+
private DisposableSingleObserver<ResponseBody> getUpvoteObserver() {
88+
return new DisposableSingleObserver<ResponseBody>() {
89+
90+
@Override
91+
public void onSuccess(ResponseBody value) {
92+
}
93+
94+
@Override
95+
public void onError(Throwable e) {
96+
e.printStackTrace();
97+
}
98+
};
4399
}
44100

45-
@Override
46-
public void onFailure(Call<List<Change>> call, Throwable t) {
47-
t.printStackTrace();
101+
public void getChangeInformation(int selectionIndex) {
102+
userInterface.updateTextFields(currentData.get(selectionIndex));
48103
}
49104
}

com.vogella.java.retrofitgerrit/src/main/java/com/vogella/java/retrofitgerrit/GerritAPI.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
import java.util.List;
44

5-
import retrofit2.Call;
5+
import io.reactivex.Single;
6+
import okhttp3.ResponseBody;
67
import retrofit2.http.GET;
7-
import retrofit2.http.Query;
8+
import retrofit2.http.POST;
9+
import retrofit2.http.Path;
10+
import retrofit2.http.Url;
811

912
public interface GerritAPI {
1013

11-
@GET("changes/")
12-
Call<List<Change>> loadChanges(@Query("q") String status);
14+
@GET("changes/?q=status:open&o=CURRENT_REVISION&o=DETAILED_ACCOUNTS")
15+
Single<List<Change>> getAllChanges();
16+
17+
@GET
18+
Single<List<Change>> getChangesForProject(@Url String url);
19+
20+
@GET
21+
Single<List<Change>> getChangesForUser(@Url String url);
22+
23+
@POST("changes/{change-id}/revisions/{revision-id}/review")
24+
Single<ResponseBody> postUpvote(@Path("change-id") String changeId, @Path("revision-id") String revisionId);
1325
}

com.vogella.java.retrofitgerrit/src/main/java/com/vogella/java/retrofitgerrit/StartUp.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
public class StartUp {
44

55
public static void main(String[] args) {
6-
Controller ctrl = new Controller();
7-
ctrl.start();
6+
UserInterface userInterface = new UserInterface();
7+
Controller controller = new Controller(userInterface);
8+
userInterface.setController(controller);
9+
userInterface.initUi();
810
}
9-
1011
}

0 commit comments

Comments
 (0)