[utopian.java v0.1.0] Utopian API for desktop Java and Android

in #utopian-io7 years ago (edited)

Overview

Java API for utopian.io REST service, covers all aspects of official javascript ibrary utopian-api. Uses latest java version and http library and will work on both desktop and android platform.

Requirements

  • Java 8

utopian.java API uses features of java 8 such as lambda expressions, API is designed in such a way that it will also work on newer and older android devices. I will cover tutorial on how to setup and use this API on android.

Technology Stack

pic1.png

Its best HTTP and HTTP/2 library available for both desktop and android platform. Used in millions of android and java desktop apps. Github repo stats tells how much popular it is right now.

Another reason to choose this api is i want utopian.java api to run on android platform as well.

pic2.png

For parsing JSON, utopian.java uses another popular JSON library from google. This library works on both desktop and android platform.

Synchronous API

Its simple to create utopian service object

UtopianService service = new DefaultUtopianService(); 
API
service.moderators().get(); // Returns all moderators as JsonObject

service.sponsors().get(); // Returns all sponsors as JsonObject

service.stats().get(); // Returns all stats as JsonObject

service.moderator(userName).get(); // Returns moderator as JsonObject with given username otherwise empty JsonObject

service.sponsor(userName).get();// Returns sponsor as JsonObject with given username otherwise empty JsonObject

service.posts(options).get(); // Returns posts as JsonObject with given filter options

service.topProjects(options).get(); // Returns top projects as JsonArray with given filter options

service.totalPostsCount().get(); // Returns total posts count as Integer

service.post(userName, permLink).get(); // Returns post as JsonObject with given params

service.postURL(postId).get(); // Returns post url as String

service.postByAuthor(userName, options).get(); //  Returns list of posts as JsonObject with given author name and filter options

service.postsByGithubProject(repoName, options).get(); // Returns list of posts as JsonObject links with specified github repository

Synchronous API examples

Creating utopian service java object
UtopianService service = new DefaultUtopianService();
1. Get all moderators and print their names
JsonObject moderatorsJson = service.moderators().get();
System.out.println("Total moderators: " +moderatorsJson.get("total"));

JsonArray results = moderatorsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
    System.out.println(result.getAsJsonObject().get("account").getAsString());
}
2. Get all sponsors and print their names
JsonObject sponsorsJson = service.sponsors().get();
System.out.println("Total sponsors: " +sponsorsJson.get("total"));
JsonArray results = sponsorsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
    System.out.println(result.getAsJsonObject().get("account").getAsString());
}
3. Get stats and print them
JsonObject statsJson = service.stats().get();
JsonObject stats = statsJson.get("stats").getAsJsonObject();
System.out.println(stats);
4. Get specific moderator and access its properties
JsonObject moderatorJson = service.moderator("ruah").get();
if(moderatorJson.size() != 0)
    System.out.println(moderatorJson.get("account").getAsString());
5. Get specific sponsor and access its properties
JsonObject sponsorJson = service.sponsor("freedom").get();
if(sponsorJson.size() != 0)
    System.out.println(sponsorJson.get("account").getAsString());
6. Get first 50 posts titles in All category
JsonObject postsJson = service.posts(new HashMap<>()).get();
JsonArray results = postsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
    System.out.println(result.getAsJsonObject().get("title").getAsString());
}
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);

JsonObject postsJson = service.posts(filterOptions).get();
JsonArray results = postsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
    System.out.println(result.getAsJsonObject().get("title").getAsString());
}
8. Get specific post and print moderator of this post
JsonObject postJson = 
                     service.post("kabooom", "building-and-using-multiple-android-shared-libraries").get();
System.out.println(postJson.get("moderator").getAsString());
9. Get total posts count
int count = service.totalPostsCount().get();
System.out.println("Total posts count: "+ count);
10. Get post url
String url = service.postURL("38068955").get();
System.out.println("URL of post: " + url);
11. Get all posts by specific author
JsonObject postsJson = service.postByAuthor("kabooom", new HashMap<>()).get();
JsonArray results = postsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
    System.out.println(result.getAsJsonObject().get("title").getAsString());
}
12. Get posts done on github repo "java-native-access/jna"
JsonObject postsJson = 
                           service.postsByGithubProject("java-native-access/jna", new HashMap<>()).get();
JsonArray results = postsJson.get("results").getAsJsonArray();
for (JsonElement result : results) {
    System.out.println(result.getAsJsonObject().get("title").getAsString());
}
13. Get top projects
JsonArray results = service.topProjects(new HashMap<>()).get();
for (JsonElement result : results) {
    System.out.println(result.getAsJsonObject().get("_id").getAsString());
}

Roadmap

  • Asynchronous API
  • String response to Java type mapping

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.1.0</version>
    </dependency>
</dependencies>

Gradle:

repositories {
    jcenter()
}

dependencies {
    compile 'io.faob:utopian-java:0.1.0'
}

Github

Code



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @kabooom I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Coin Marketplace

STEEM 0.26
TRX 0.20
JST 0.037
BTC 94837.57
ETH 3452.01
USDT 1.00
SBD 3.93