Skip to content

Commit

Permalink
Add POST example to README.md and example-github (OpenFeign#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiselems authored and kdavisk6 committed Jun 18, 2019
1 parent 23ee09e commit 0ed5008
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,25 @@ Usage typically looks like this, an adaptation of the [canonical Retrofit sample
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);

@RequestLine("POST /repos/{owner}/{repo}/issues")
void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);

}

public static class Contributor {
String login;
int contributions;
}

public static class Issue {
String title;
String body;
List<String> assignees;
int milestone;
List<String> labels;
}

public class MyApp {
public static void main(String... args) {
GitHub github = Feign.builder()
Expand Down
36 changes: 31 additions & 5 deletions example-github/src/main/java/example/github/GitHubExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
*/
package example.github;

import feign.Feign;
import feign.Logger;
import feign.Param;
import feign.RequestLine;
import feign.Response;
import feign.*;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -42,12 +40,28 @@ class Contributor {
String login;
}

class Issue {

Issue() {

}

String title;
String body;
List<String> assignees;
int milestone;
List<String> labels;
}

@RequestLine("GET /users/{username}/repos?sort=full_name")
List<Repository> repos(@Param("username") String owner);

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

@RequestLine("POST /repos/{owner}/{repo}/issues")
void createIssue(Issue issue, @Param("owner") String owner, @Param("repo") String repo);

/** Lists all contributors for all repos owned by a user. */
default List<String> contributors(String owner) {
return repos(owner).stream()
Expand All @@ -59,7 +73,9 @@ default List<String> contributors(String owner) {

static GitHub connect() {
Decoder decoder = new GsonDecoder();
Encoder encoder = new GsonEncoder();
return Feign.builder()
.encoder(encoder)
.decoder(decoder)
.errorDecoder(new GitHubErrorDecoder(decoder))
.logger(new Logger.ErrorLogger())
Expand Down Expand Up @@ -101,6 +117,16 @@ public static void main(String... args) {
} catch (GitHubClientError e) {
System.out.println(e.getMessage());
}

System.out.println("Now, try to create an issue - which will also cause an error.");
try {
GitHub.Issue issue = new GitHub.Issue();
issue.title = "The title";
issue.body = "Some Text";
github.createIssue(issue, "OpenFeign", "SomeRepo");
} catch (GitHubClientError e) {
System.out.println(e.getMessage());
}
}

static class GitHubErrorDecoder implements ErrorDecoder {
Expand Down

0 comments on commit 0ed5008

Please sign in to comment.