Skip to content

Commit b180bf0

Browse files
eiselemsvelo
authored andcommitted
Add POST example to README.md and example-github (OpenFeign#986)
Fixes: OpenFeign#978
1 parent 2cc87bc commit b180bf0

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

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: 31 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;
@@ -42,12 +40,28 @@ class Contributor {
4240
String login;
4341
}
4442

43+
class Issue {
44+
45+
Issue() {
46+
47+
}
48+
49+
String title;
50+
String body;
51+
List<String> assignees;
52+
int milestone;
53+
List<String> labels;
54+
}
55+
4556
@RequestLine("GET /users/{username}/repos?sort=full_name")
4657
List<Repository> repos(@Param("username") String owner);
4758

4859
@RequestLine("GET /repos/{owner}/{repo}/contributors")
4960
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
5061

62+
@RequestLine("POST /repos/{owner}/{repo}/issues")
63+
void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);
64+
5165
/** Lists all contributors for all repos owned by a user. */
5266
default List<String> contributors(String owner) {
5367
return repos(owner).stream()
@@ -59,7 +73,9 @@ default List<String> contributors(String owner) {
5973

6074
static GitHub connect() {
6175
Decoder decoder = new GsonDecoder();
76+
Encoder encoder = new GsonEncoder();
6277
return Feign.builder()
78+
.encoder(encoder)
6379
.decoder(decoder)
6480
.errorDecoder(new GitHubErrorDecoder(decoder))
6581
.logger(new Logger.ErrorLogger())
@@ -101,6 +117,16 @@ public static void main(String... args) {
101117
} catch (GitHubClientError e) {
102118
System.out.println(e.getMessage());
103119
}
120+
121+
System.out.println("Now, try to create an issue - which will also cause an error.");
122+
try {
123+
GitHub.Issue issue = new GitHub.Issue();
124+
issue.title = "The title";
125+
issue.body = "Some Text";
126+
github.createIssue(issue, "OpenFeign", "SomeRepo");
127+
} catch (GitHubClientError e) {
128+
System.out.println(e.getMessage());
129+
}
104130
}
105131

106132
static class GitHubErrorDecoder implements ErrorDecoder {

0 commit comments

Comments
 (0)