Overview
In this new release support for string response to java type mapping is included plus some other code improvements.
For more information on previous versions
Requirements
- Java 8
utopian.java API uses features of java 8 such as lambda expressions, Subset of Java 8 features has been used in this library so that it can run on older android devices and latest ones as well as desktop Java.
Code Commits
Added some code improvements to easily access json arrays. In previous versions, to access json array we first have to get JsonObject and then search for array type like this
JsonObject moderatorsJson = service.moderators().get();
JsonArray jsonArray = moderatorsJson.get("results").getAsJsonArray();
Now array type is directly available. API has updated please see next commit features
JsonArray jsonArray = service.moderators().get();
Added support to select between many return types. For array type json response, to get response as String or JsonArray
String response = service
.moderators()
.asString() // will return response as String
.get();
JsonArray jsonArray = service
.moderators()
.asJsonArray() // will return response as JsonArray
.get();
For object type json response, to get response as String or JsonObject
String response = service
.moderator("espoem")
.asString() // will return response as String
.get();
JsonObject jsonObject = service
.moderator("espoem")
.asJsonObject() // will return response as JsonObject
.get();
Added support to map string response to java type. To map array type json response to java type
List<Moderator> moderators = service
.moderators()
.asJavaListType() // will return response as List of Moderators Objects
.get();
To map object type json response to java type
Moderator moderator = service
.moderator("espoem")
.asJavaType() //will return response as Moderator object
.get();
Other java types available are Sponsor, Stats, Post and Project.
Wiki pages added
Synchronous vs Asynchronous API
get(): is a blocking call it will block the thread until it finishes the http request.
getAsync(res -> {}, exp -> {}): is non blocking call it will not block the thread, it will make HTTP request in background thread and will pass result to one of its parameters. It has two parameters first lambda expression will gets executed when HTTP request successfully executed and there is response available. Second lambda expression will gets executed when there is any error occurred while performing HTTP request or while parsing string response to JsonObject.
Note : In lambda expressions type of "res" can be JsonObject, JsonArray, String , Integer or mapped java type depends on which API you are calling and the type of "exp" is Exception.
Creating utopian service java object
UtopianService service = new DefaultUtopianService();
Synchronous API examples using Json and Java Type Mapping API
1. Get and print all moderator's names
//Using JsonArray API
JsonArray results = service
.moderators()
.asJsonArray()
.get();
for (JsonElement result : results) {
println(result.getAsJsonObject().get("account").getAsString());
}
//Using java type mapping API
List<Moderator> moderators = service
.moderators()
.asJavaListType()
.get();
for (Moderator moderator : moderators) {
println(moderator.getAccount());
}
2. Get and print all sponsor's names
//Using JsonArray API
JsonArray results = service
.sponsors()
.asJsonArray()
.get();
for (JsonElement result : results) {
println(result.getAsJsonObject().get("account").getAsString());
}
//Using java type mapping API
List<Sponsor> sponsors = service
.sponsors()
.asJavaListType()
.get();
for (Sponsor sponsor : sponsors) {
println(sponsor.getAccount());
}
3. Get stats and print id
//Using JsonObject API
JsonObject statsJson = service
.stats()
.asJsonObject()
.get();
println(statsJson.get("_id").getAsString());
//Using java type mapping API
Stats stats = service
.stats()
.asJavaType()
.get();
println(stats.getId());
4. Get moderator and print how many projects he/she moderated
//Using JsonObject API
JsonObject moderatorJson = service
.moderator("espoem")
.asJsonObject()
.get();
println(moderatorJson.get("total_moderated").getAsString());
//Using java type mapping API
Moderator moderator = service
.moderator("espoem")
.asJavaType()
.get();
println(moderator.getTotalModerated());
5. Get sponsor and print STEEM received
//Using JsonObject API
JsonObject sponsorJson = service
.sponsor("freedom")
.asJsonObject()
.get();
println(sponsorJson.get("total_paid_rewards_steem").getAsDouble());
//Using java type mapping API
Sponsor sponsor = service
.sponsor("freedom")
.asJavaType()
.get();
println(sponsor.getTotalPaidRewardsSteem());
6. Get first 50 posts titles in All category
//Using JsonArray API
JsonArray results = service
.posts(new HashMap<>())
.asJsonArray()
.get();
for (JsonElement result : results) {
println(result.getAsJsonObject().get("title").getAsString());
}
//Using java type mapping API
List<Post> posts = service
.posts(new HashMap<>())
.asJavaListType()
.get();
for (Post post : posts) {
println(post.getTitle());
}
7. Get first 5 posts titles in Tutorials category
Map<String, Object> filterOptions = new HashMap<>();
filterOptions.put("sortBy", "created");
filterOptions.put("type", "tutorials");
filterOptions.put("limit", 5);
//Using JsonArray API
JsonArray results = service
.posts(filterOptions)
.asJsonArray()
.get();
for (JsonElement result : results) {
println(result.getAsJsonObject().get("title").getAsString());
}
//Using java type mapping API
List<Post> posts = service
.posts(filterOptions)
.asJavaListType()
.get();
for (Post post : posts) {
println(post.getTitle());
}
8. Get post and check is reviewed or not
//Using JsonObject API
JsonObject postJson = service
.post("kabooom", "utopian-java-v0-1-0-utopian-api-for-java-and-android")
.asJsonObject()
.get();
println(postJson.get("reviewed").getAsBoolean());
//Using java type mapping API
Post post = service
.post("kabooom", "utopian-java-v0-1-0-utopian-api-for-java-and-android")
.asJavaType()
.get();
println(post.isReviewed());
9. Get total posts count
int count = service
.totalPostsCount()
.get();
println("Total posts count: " + count);
10. Get post url
String url = service
.postURL("40498539")
.get();
println("URL of post: " + url);
11. Get all posts by author
//Using JsonArray API
JsonArray results = service
.postsByAuthor("kabooom", new HashMap<>())
.asJsonArray()
.get();
for (JsonElement result : results) {
println(result.getAsJsonObject().get("title").getAsString());
}
//Using java type mapping API
List<Post> posts = service
.postsByAuthor("kabooom", new HashMap<>())
.asJavaListType()
.get();
for (Post post : posts) {
println(post.getTitle());
}
12. Get posts on github repo "faob-dev/utopian.java"
//Using JsonArray API
JsonArray results = service
.postsByGithubProject("faob-dev/utopian.java", new HashMap<>())
.asJsonArray()
.get();
for (JsonElement result : results) {
println(result.getAsJsonObject().get("title").getAsString());
}
//Using java type mapping API
List<Post> posts = service
.postsByGithubProject("faob-dev/utopian.java", new HashMap<>())
.asJavaListType()
.get();
for (Post post : posts) {
println(post.getTitle());
}
13. Get top projects
//Example using JsonArray
JsonArray results = service
.topProjects(new HashMap<>())
.asJsonArray()
.get();
for (JsonElement result : results) {
println(result.getAsJsonObject().get("_id").getAsString());
}
//Example using java type mapping
List<Project> topProjects = service
.topProjects(new HashMap<>())
.asJavaListType()
.get();
for (Project project : topProjects) {
println(project.getId());
}
Asynchronous API examples using Json and Java Type Mapping API
1. Get and print all moderator's names
//Using JsonArray API
service
.moderators()
.asJsonArray()
.getAsync(results -> {
for (JsonElement result : results) {
println(result.getAsJsonObject().get("account").getAsString());
}
}, e -> {
e.printStackTrace();
});
//Using java type mapping API
service
.moderators()
.asJavaListType()
.getAsync(moderators -> {
for (Moderator moderator : moderators) {
println(moderator.getAccount());
}
}, e -> {
e.printStackTrace();
});
}
2. Get and print all sponsor's names
//Using JsonArray API
service
.sponsors()
.asJsonArray()
.getAsync(results -> {
for (JsonElement result : results) {
println(result.getAsJsonObject().get("account").getAsString());
}
}, e -> {
e.printStackTrace();
});
//Using java type mapping API
service
.sponsors()
.asJavaListType()
.getAsync(sponsors -> {
for (Sponsor sponsor : sponsors) {
println(sponsor.getAccount());
}
}, e -> {
e.printStackTrace();
});
3. Get stats and print id
//Using JsonObject API
service
.stats()
.asJsonObject()
.getAsync(statsJson -> {
println(statsJson.get("_id").getAsString());
}, e -> {
e.printStackTrace();
});
//Using java type mapping API
service
.stats()
.asJavaType()
.getAsync(stats -> {
println(stats.getId());
}, e -> {
e.printStackTrace();
});
4. Get moderator and print how many projects he/she moderated
//Using JsonObject API
service
.moderator("espoem")
.asJsonObject()
.getAsync(moderatorJson -> {
println(moderatorJson.get("total_moderated").getAsString());
}, e -> {
e.printStackTrace();
});
//Using java type mapping API
service
.moderator("espoem")
.asJavaType()
.getAsync(moderator -> {
println(moderator.getTotalModerated());
}, e -> {
e.printStackTrace();
});
5. Get sponsor and print STEEM received
//Using JsonObject API
service
.sponsor("freedom")
.asJsonObject()
.getAsync(sponsorJson -> {
println(sponsorJson.get("total_paid_rewards_steem").getAsDouble());
}, e -> {
e.printStackTrace();
});
//Using java type mapping API
service
.sponsor("freedom")
.asJavaType()
.getAsync(sponsor -> {
println(sponsor.getTotalPaidRewardsSteem());
},e -> {
e.printStackTrace();
});
6. Get first 50 posts titles in All category
//Using JsonArray API
service
.posts(new HashMap<>())
.asJsonArray()
.getAsync(results -> {
for (JsonElement result : results) {
println(result.getAsJsonObject().get("title").getAsString());
}
}, e -> {
e.printStackTrace();
});
//Using java type mapping API
service
.posts(new HashMap<>())
.asJavaListType()
.getAsync(posts -> {
for (Post post : posts) {
println(post.getTitle());
}
}, e -> {
e.printStackTrace();
});
7. Get first 5 posts titles in Tutorials category
Map<String, Object> filterOptions = new HashMap<>();
filterOptions.put("sortBy", "created");
filterOptions.put("type", "tutorials");
filterOptions.put("limit", 5);
//Using JsonArray API
service
.posts(filterOptions)
.asJsonArray()
.getAsync(results -> {
for (JsonElement result : results) {
println(result.getAsJsonObject().get("title").getAsString());
}
}, e -> {
e.printStackTrace();
});
//Using java type mapping API
service
.posts(filterOptions)
.asJavaListType()
.getAsync(posts -> {
for (Post post : posts) {
println(post.getTitle());
}
}, e -> {
e.printStackTrace();
});
8. Get post and check is reviewed or not
//Using JsonObject API
service
.post("kabooom", "utopian-java-v0-1-0-utopian-api-for-java-and-android")
.asJsonObject()
.getAsync(postJson -> {
println(postJson.get("reviewed").getAsBoolean());
}, e -> {
e.printStackTrace();
});
//Using java type mapping API
service
.post("kabooom", "utopian-java-v0-1-0-utopian-api-for-java-and-android")
.asJavaType()
.getAsync(post -> {
println(post.isReviewed());
}, e -> {
e.printStackTrace();
});
9. Get total posts count
service
.totalPostsCount()
.getAsync(count -> {
println("Total posts count: " + count);
}, e -> {
e.printStackTrace();
});
10. Get post url
service
.postURL("40498539")
.getAsync(url -> {
println("URL of post: " + url);
},e -> {
e.printStackTrace();
});
11. Get all posts by author
//Using JsonArray API
service
.postsByAuthor("kabooom", new HashMap<>())
.asJsonArray()
.getAsync(results -> {
for (JsonElement result : results) {
println(result.getAsJsonObject().get("title").getAsString());
}
}, e -> {
e.printStackTrace();
});
//Using java type mapping API
service
.postsByAuthor("kabooom", new HashMap<>())
.asJavaListType()
.getAsync(posts -> {
for (Post post : posts) {
println(post.getTitle());
}
}, e -> {
e.printStackTrace();
});
12. Get posts on github repo "faob-dev/utopian.java"
//Using JsonArray API
service
.postsByGithubProject("faob-dev/utopian.java", new HashMap<>())
.asJsonArray()
.getAsync(results -> {
for (JsonElement result : results) {
println(result.getAsJsonObject().get("title").getAsString());
}
}, e -> {
e.printStackTrace();
});
//Using java type mapping API
service
.postsByGithubProject("faob-dev/utopian.java", new HashMap<>())
.asJavaListType()
.getAsync(posts -> {
for (Post post : posts) {
println(post.getTitle());
}
}, e -> {
e.printStackTrace();
});
13. Get top projects
//Example using JsonArray
service
.topProjects(new HashMap<>())
.asJsonArray()
.getAsync(results -> {
for (JsonElement result : results) {
println(result.getAsJsonObject().get("_id").getAsString());
}
}, e -> {
e.printStackTrace();
});
//Example using java type mapping
service
.topProjects(new HashMap<>())
.asJavaListType()
.getAsync(topProjects -> {
for (Project project : topProjects) {
println(project.getId());
}
}, e -> {
e.printStackTrace();
});
Wiki pages added on github
Roadmap
- Synchronous API completed
- Asynchronous API completed
- String response to Java type mapping completed
Contribution
Contributions are always welcome, contribution to this project is simple create fork add new features or bug fixes and send a pull request.
Download
Maven:
<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.faob</groupId>
<artifactId>utopian-java</artifactId>
<version>0.3.0</version>
</dependency>
</dependencies>
Gradle:
repositories {
jcenter()
}
dependencies {
compile 'io.faob:utopian-java:0.3.0'
}
Github
Posted on Utopian.io - Rewarding Open Source Contributors
Hi! I am a robot. I just upvoted you! Readers might be interested in similar content by the same author:
https://steemit.com/utopian-io/@kabooom/utopian-java-v0-2-0-asynchronous-utopian-api-added-for-desktop-java-and-android
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @kabooom I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit