Skip to content

Commit fe96380

Browse files
author
David Weiser
committed
Updates Retrofit Gerrit code example
1 parent 27c0a21 commit fe96380

File tree

5 files changed

+84
-12
lines changed

5 files changed

+84
-12
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import io.reactivex.disposables.CompositeDisposable;
99
import io.reactivex.observers.DisposableSingleObserver;
1010
import io.reactivex.schedulers.Schedulers;
11+
import okhttp3.Credentials;
1112
import okhttp3.OkHttpClient;
1213
import okhttp3.ResponseBody;
1314
import okhttp3.logging.HttpLoggingInterceptor;
15+
import okhttp3.logging.HttpLoggingInterceptor.Level;
1416
import retrofit2.Retrofit;
1517
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
1618
import retrofit2.converter.gson.GsonConverterFactory;
@@ -30,11 +32,11 @@ public Controller(UserInterface userInterface) {
3032

3133
private void initGerritApi() {
3234
HttpLoggingInterceptor loggerInterceptor = new HttpLoggingInterceptor();
33-
// loggerInterceptor.setLevel(Level.BODY);
35+
loggerInterceptor.setLevel(Level.BODY);
3436

3537
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(loggerInterceptor).build();
3638

37-
Gson gson = new GsonBuilder().setLenient().create();
39+
Gson gson = new GsonBuilder().registerTypeAdapter(ReviewInput.class, new JsonSerializer()).setLenient().create();
3840

3941
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
4042
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
@@ -61,11 +63,12 @@ public void loadChangesForUser(int userId) {
6163
.observeOn(Schedulers.single()).subscribeWith(getChangesObserver()));
6264
}
6365

64-
public void upvote(int i) {
66+
public void upvote(String username, String password, int i) {
6567
Change currentChange = currentData.get(i);
66-
// compositeDisposable.add(gerritAPI.postUpvote(currentChange.getChangeId(),
67-
// currentChange.getCurrentRevision())
68-
// .subscribeOn(Schedulers.io()).observeOn(Schedulers.single()).subscribeWith(getUpvoteObserver()));
68+
compositeDisposable.add(gerritAPI
69+
.postUpvote(Credentials.basic(username, username), currentChange.getChangeId(),
70+
currentChange.getCurrentRevision(), new ReviewInput())
71+
.subscribeOn(Schedulers.io()).observeOn(Schedulers.single()).subscribeWith(getUpvoteObserver()));
6972
}
7073

7174
private DisposableSingleObserver<List<Change>> getChangesObserver() {
@@ -89,6 +92,7 @@ private DisposableSingleObserver<ResponseBody> getUpvoteObserver() {
8992

9093
@Override
9194
public void onSuccess(ResponseBody value) {
95+
9296
}
9397

9498
@Override

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
import io.reactivex.Single;
66
import okhttp3.ResponseBody;
7+
import retrofit2.http.Body;
78
import retrofit2.http.GET;
9+
import retrofit2.http.Header;
810
import retrofit2.http.POST;
911
import retrofit2.http.Path;
1012
import retrofit2.http.Url;
@@ -21,5 +23,5 @@ public interface GerritAPI {
2123
Single<List<Change>> getChangesForUser(@Url String url);
2224

2325
@POST("changes/{change-id}/revisions/{revision-id}/review")
24-
Single<ResponseBody> postUpvote(@Path("change-id") String changeId, @Path("revision-id") String revisionId);
26+
Single<ResponseBody> postUpvote(@Header("Authentication") String credentials, @Path("change-id") String changeId, @Path("revision-id") String revisionId, @Body ReviewInput reviewInput);
2527
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.vogella.java.retrofitgerrit;
2+
3+
import java.lang.reflect.Type;
4+
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
7+
import com.google.gson.JsonSerializationContext;
8+
9+
public class JsonSerializer implements com.google.gson.JsonSerializer<ReviewInput>{
10+
11+
@Override
12+
public JsonElement serialize(ReviewInput src, Type typeOfSrc, JsonSerializationContext context) {
13+
JsonObject codeReviewJson = new JsonObject();
14+
JsonElement codeReviewElement = context.serialize(src.getCodeReview());
15+
codeReviewJson.add("Code-Review", codeReviewElement);
16+
17+
JsonObject parentJson = new JsonObject();
18+
JsonElement parentElement = context.serialize(codeReviewJson);
19+
parentJson.add("labels", parentElement);
20+
21+
return parentJson;
22+
}
23+
24+
}
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 ReviewInput {
6+
7+
@SerializedName("Code-Review")
8+
private String codeReview = "+1";
9+
10+
public String getCodeReview() {
11+
return codeReview;
12+
}
13+
14+
public void setCodeReview(String codeReview) {
15+
this.codeReview = codeReview;
16+
}
17+
}

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.eclipse.swt.layout.GridLayout;
88
import org.eclipse.swt.widgets.Button;
99
import org.eclipse.swt.widgets.Display;
10+
import org.eclipse.swt.widgets.Label;
1011
import org.eclipse.swt.widgets.List;
1112
import org.eclipse.swt.widgets.Shell;
1213
import org.eclipse.swt.widgets.Text;
@@ -23,6 +24,11 @@ public class UserInterface {
2324
private Text userIdText;
2425
private Text projectNameText;
2526
private Text upvoteText;
27+
private Text usernameText;
28+
private Text passwordText;
29+
30+
private Label passwordLabel;
31+
private Label usernameLabel;
2632

2733
private Display display;
2834
private Controller controller;
@@ -38,16 +44,34 @@ public void initUi() {
3844
GridData gridData;
3945
display = new Display();
4046
Shell shell = new Shell(display);
41-
shell.setSize(400, 300);
42-
47+
shell.setSize(500, 400);
48+
4349
GridLayout gridLayout = new GridLayout();
44-
gridLayout.numColumns = 3;
50+
gridLayout.numColumns = 4;
4551
shell.setLayout(gridLayout);
4652

53+
usernameLabel = new Label(shell, SWT.NONE);
54+
usernameLabel.setText("Username:");
55+
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
56+
usernameLabel.setLayoutData(gridData);
57+
58+
usernameText = new Text(shell, SWT.NONE);
59+
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
60+
usernameText.setLayoutData(gridData);
61+
62+
passwordLabel = new Label(shell, SWT.NONE);
63+
passwordLabel.setText("Password:");
64+
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
65+
passwordLabel.setLayoutData(gridData);
66+
67+
passwordText = new Text(shell, SWT.PASSWORD);
68+
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
69+
passwordText.setLayoutData(gridData);
70+
4771
allChangesButton = new Button(shell, SWT.PUSH);
4872
allChangesButton.setText("Get all changes");
4973
gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
50-
gridData.horizontalSpan = 3;
74+
gridData.horizontalSpan = 4;
5175
allChangesButton.setLayoutData(gridData);
5276
allChangesButton.addSelectionListener(new SelectionAdapter() {
5377
@Override
@@ -64,6 +88,7 @@ public void widgetDefaultSelected(SelectionEvent e) {
6488
changes = new List(shell, SWT.NONE);
6589
gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
6690
gridData.verticalSpan = 4;
91+
gridData.horizontalSpan = 2;
6792
changes.setLayoutData(gridData);
6893
changes.addSelectionListener(new SelectionAdapter() {
6994

@@ -139,7 +164,7 @@ public void widgetDefaultSelected(SelectionEvent e) {
139164

140165
@Override
141166
public void widgetSelected(SelectionEvent e) {
142-
controller.upvote(changes.getSelectionIndex());
167+
controller.upvote(usernameText.getText(), passwordText.getText(), changes.getSelectionIndex());
143168
}
144169

145170
@Override

0 commit comments

Comments
 (0)