Skip to content

Commit b0840d4

Browse files
eiselemskdavisk6
authored andcommitted
Add POST example to README.md and example-github (OpenFeign#986)
Fixes: OpenFeign#978
1 parent 0dc8c5e commit b0840d4

2 files changed

Lines changed: 41 additions & 5 deletions

File tree

‎README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,25 @@ Usage typically looks like this, an adaptation of the [canonical Retrofit sample
2626
interface GitHub {
2727
@RequestLine("GET /repos/{owner}/{repo}/contributors")
2828
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
29+
30+
@RequestLine("POST /repos/{owner}/{repo}/issues")
31+
void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);
32+
2933
}
3034

3135
public static class Contributor {
3236
String login;
3337
int contributions;
3438
}
3539

40+
public static class Issue {
41+
String title;
42+
String body;
43+
List<String> assignees;
44+
int milestone;
45+
List<String> labels;
46+
}
47+
3648
public class MyApp {
3749
public static void main(String... args) {
3850
GitHub github = Feign.builder()

‎example-github/src/main/java/example/github/GitHubExample.java‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
*/
1414
package example.github;
1515

16-
import feign.Feign;
17-
import feign.Logger;
18-
import feign.Param;
19-
import feign.RequestLine;
20-
import feign.Response;
16+
import feign.*;
2117
import feign.codec.Decoder;
18+
import feign.codec.Encoder;
2219
import feign.codec.ErrorDecoder;
2320
import feign.gson.GsonDecoder;
21+
import feign.gson.GsonEncoder;
2422
import java.io.IOException;
2523
import java.util.List;
2624
import java.util.stream.Collectors;
@@ -40,12 +38,26 @@ class Contributor {
4038
String login;
4139
}
4240

41+
class Issue {
42+
43+
Issue() {}
44+
45+
String title;
46+
String body;
47+
List<String> assignees;
48+
int milestone;
49+
List<String> labels;
50+
}
51+
4352
@RequestLine("GET /users/{username}/repos?sort=full_name")
4453
List<Repository> repos(@Param("username") String owner);
4554

4655
@RequestLine("GET /repos/{owner}/{repo}/contributors")
4756
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
4857

58+
@RequestLine("POST /repos/{owner}/{repo}/issues")
59+
void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);
60+
4961
/** Lists all contributors for all repos owned by a user. */
5062
default List<String> contributors(String owner) {
5163
return repos(owner).stream()
@@ -57,7 +69,9 @@ default List<String> contributors(String owner) {
5769

5870
static GitHub connect() {
5971
Decoder decoder = new GsonDecoder();
72+
Encoder encoder = new GsonEncoder();
6073
return Feign.builder()
74+
.encoder(encoder)
6175
.decoder(decoder)
6276
.errorDecoder(new GitHubErrorDecoder(decoder))
6377
.logger(new Logger.ErrorLogger())
@@ -97,6 +111,16 @@ public static void main(String... args) {
97111
} catch (GitHubClientError e) {
98112
System.out.println(e.getMessage());
99113
}
114+
115+
System.out.println("Now, try to create an issue - which will also cause an error.");
116+
try {
117+
GitHub.Issue issue = new GitHub.Issue();
118+
issue.title = "The title";
119+
issue.body = "Some Text";
120+
github.createIssue(issue, "OpenFeign", "SomeRepo");
121+
} catch (GitHubClientError e) {
122+
System.out.println(e.getMessage());
123+
}
100124
}
101125

102126
static class GitHubErrorDecoder implements ErrorDecoder {

0 commit comments

Comments
 (0)