[utopian.java v0.2.0] - Asynchronous Utopian API added for desktop Java and Android

in #utopian-io6 years ago (edited)

Overview

In this new release asynchronous API has been added to make http calls in background thread.

For information of previous version 0.1.0 please read this post

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 asynchronous API.

Javadocs added for new functional interfaces.

Added asynchronous API examples on github.

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 or Integer depends on which API you are calling and the type of "exp" is Exception.

Creating utopian service java object

UtopianService service = new DefaultUtopianService();

Asynchronous API

service.moderators().getAsync(r -> {}, e -> {}); // Returns all moderators as JsonObject

service.sponsors().getAsync(r -> {}, e -> {}); // Returns all sponsors as JsonObject

service.stats().getAsync(r -> {}, e -> {}); // Returns all stats as JsonObject

service.moderator(userName).getAsync(r -> {}, e -> {}); // Returns moderator as JsonObject with given username otherwise empty JsonObject

service.sponsor(userName).getAsync(r -> {}, e -> {}); // Returns sponsor as JsonObject with given username otherwise empty JsonObject

service.posts(options).getAsync(r -> {}, e -> {}); // Returns posts as JsonObject with given filter options

service.topProjects(options).getAsync(r -> {}, e -> {}); // Returns top projects as JsonArray with given filter options

service.totalPostsCount().getAsync(r -> {}, e -> {}); // Returns total posts count as Integer

service.post(userName, permLink).getAsync(r -> {}, e -> {}); // Returns post as JsonObject with given params

service.postURL(postId).getAsync(r -> {}, e -> {}); // Returns post url as String

service.postByAuthor(userName, options).getAsync(r -> {}, e -> {}); //  Returns list of posts as JsonObject with given author name and filter options

service.postsByGithubProject(repoName, options).getAsync(r -> {}, e -> {}); // Returns list of posts as JsonObject links with specified github repository

Asynchronous API examples

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

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

Examples added on github

async examples

Roadmap

  • Synchronous API completed
  • Asynchronous API completed
  • 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.2.0</version>
    </dependency>
</dependencies>

Gradle:

repositories {
    jcenter()
}

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

Github

Code



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

Please include the most relevant pull request, or in your case the commits, in your posts since the article should be about its improvements.

It seems you like @espoem as well, he's in the code now.

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

thanks for review and i updated the post

Good, but think of the humans reading your post! ;-)

This would be better:

Hey @helo, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

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.28
TRX 0.13
JST 0.032
BTC 61113.33
ETH 2927.99
USDT 1.00
SBD 3.69