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
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.
Added asynchronous API.
Javadocs added for new functional interfaces.
Added asynchronous API examples on github.
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.
UtopianService service = new DefaultUtopianService();
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
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();
});
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();
});
service
.stats()
.getAsync(statsJson -> {
JsonObject stats = statsJson.get("stats").getAsJsonObject();
System.out.println(stats);
}, e -> {
e.printStackTrace();
});
service
.moderator("espoem")
.getAsync(moderatorJson -> {
if (moderatorJson.size() != 0)
System.out.println(moderatorJson.get("account").getAsString());
}, e -> {
e.printStackTrace();
});
service
.sponsor("freedom")
.getAsync(sponsorJson -> {
if (sponsorJson.size() != 0)
System.out.println(sponsorJson.get("account").getAsString());
}, e -> {
e.printStackTrace();
});
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();
});
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();
});
service
.post("kabooom", "building-and-using-multiple-android-shared-libraries")
.getAsync(postJson -> {
System.out.println(postJson.get("moderator").getAsString());
}, e -> {
e.printStackTrace();
});
service
.totalPostsCount()
.getAsync(count -> {
System.out.println("Total posts count: " + count);
}, e -> {
e.printStackTrace();
});
service
.postURL("38068955")
.getAsync(url -> {
System.out.println("URL of post: " + url);
}, e -> {
e.printStackTrace();
});
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();
});
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();
});
service
.topProjects(new HashMap<>())
.getAsync(results -> {
for (JsonElement result : results) {
System.out.println(result.getAsJsonObject().get("_id").getAsString());
}
}, e -> {
e.printStackTrace();
});
Contributions are always welcome, contribution to this project is simple create fork add new features or bug fixes and send a pull request.
Maven:
<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/
</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'
}