Code Monkey home page Code Monkey logo

communicator's Introduction

Communicator

CircleCI codecov Maven License

A monix wrapper for OkHttp

Communicator provides a simple way to construct OkHttp requests as monix.Tasks which is equipped with a beautiful functional interface and comes with first class support for cancellation.

Installation

libraryDependencies ++=
    "io.taig" %% "communicator-request" % "3.5.1" ::
    "io.taig" %% "communicator-builder" % "3.5.1" ::
    Nil

Quickstart

import monix._; import eval.Task; import execution.Scheduler.Implicits.global
import io.taig.communicator._
import okhttp3.OkHttpClient
import scala._; import util._; import concurrent._; import duration._

// To build request tasks, an implicit OkHttpClient should be in scope
implicit val client = new OkHttpClient()

// Simple OkHttp request builder
val builder = new OkHttpRequestBuilder().url("https://github.com/")

// Construct a Task[Response] and parse it to a String
val request = Request(builder.build()).parse[String]

// Kick off the actual request
val response = request.runAsync
Await.result(response, 30.seconds)
// res4: io.taig.communicator.Response[String] =
// >>> https://github.com/
// [No headers]
// <<< 200 OK
// Server: GitHub.com
// Date: Fri, 31 Aug 2018 12:49:58 GMT
// Content-Type: text/html; charset=utf-8
// Transfer-Encoding: chunked
// Status: 200 OK
// Cache-Control: no-cache
// Vary: X-PJAX
// Set-Cookie: has_recent_activity=1; path=/; expires=Fri, 31 Aug 2018 13:49:58 -0000
// Set-Cookie: _octo=GH1.1.1880644452.1535719798; domain=.github.com; path=/; expires=Mon, 31 Aug 2020 12:49:58 -0000
// Set-Cookie: logged_in=no; domain=.github.com; path=/; expires=Tue, 31 Aug 2038 12:49:58 -0000; secure; HttpOnly
// Set-Cookie: _gh_sess=Y3RsdXRvTWFNZ0tZKy9hWlZ1Ujc4KzRYTlBuTm51bFJoWHN0TGZzQjJxa0hLQXJ4enhzcTgvOHhuOTFqWW9qcjhDalpML0UvWSt3NzVTWGYyOHIwL283Y0xsQXJkcnVHKy84Uk8zYnVsdmpxNnpoMG5MbXJucTBxNWs3ejI4MVBQO...

Usage

Communicator provides a thin layer around OkHttp using monix.Task to execute HTTP requests and monix.Observable for Phoenix Channels. To construct requests, the OkHttp builder API is used.

Building Requests

Use the OkHttp builder API to construct requests which are then lifted into io.taig.communicator.request.Request.

val headers = new OkHttpRequestBuilder().
    url("https://github.com/").
    header("X-API-Key", "foobar").
    build()

val request: Request = Request(headers)

Handling Responses

There are several ways to transform a Request to an executable Task[Response].

// Ignores response body
val ignoreBody: Task[Response[Unit]] = request.ignoreBody

// Parses response body to a String
val parse: Task[Response[String]] = request.parse[String]

Android

To use Communicator on the Android platform please extend your ProGuard rules by the following instructions:

proguardOptions ++=
    "-dontnote okhttp3.internal.**" ::
    "-dontnote monix.execution.internals.**" ::
    "-dontwarn io.circe.generic.util.macros.**" ::
    "-dontwarn monix.execution.internals.**" ::
    "-dontwarn okio.**" ::
    "-dontwarn org.jctools.**" ::
    "-dontwarn org.slf4j.**" ::
    Nil

You might also use existing platform Executors to provide a monix Scheduler:

import android.os.AsyncTask
import android.util.Log
import monix.execution.Scheduler

implicit val PoolScheduler: Scheduler = Scheduler {
    ExecutionContext.fromExecutor(
        AsyncTask.THREAD_POOL_EXECUTOR,
        t  Log.e("PoolScheduler", "Failure during asynchronous operation", t)
    )
}

Communicator 2.x

The scala.concurrent.Future predecessor of this library has been deprecated. You can still access the source and documentation.

Communicator 1.x

The Java predecessor of this library has been deprecated. You can still access the source and documentation.

communicator's People

Contributors

taig avatar williamho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

communicator's Issues

Update monix and circe dependencies

Monix has released a stable version in the 3.x series. It would be nice to move from the release candidate to the 3.1.0

I have made this PR #61 to help with this

Simplify the Method API

The Method.* API should be simplified to the effect that a Java call like GET( Parser.TEXT, new URL( "http://www.google.com/" ) ) could be executed as GET[String]( "http://www.google.com/" ) within Scala.

Event class should be an interface

Instead of an abstract class the Event class should be an interface, allowing Scala to subclass it from a trait and implement method stubs. Thereby allowing to inherit from an activity or fragment instead of creating a dedicated inner class. For Java there needs to be an abstract class that implements the interface with stubs.

Invalid sha1 checksum in maven central

I'm unable to resolve Communicator as a dependency in sbt due to the following warning. Are the checksums corrupt?

problem while downloading module descriptor: http://repo1.maven.org/maven2/io/taig/communicator_2.11/2.0.2/communicator_2.11-2.0.2.pom: invalid sha1: expected=8bcb649e6bb835510a337dce76ffa340c933d26f computed=f8ed1419dd1bf3964d1143c4a376adcd75d360ee

I get the same error (with different checksums) when trying 2.0.1. However, 2.0.0 does work.

README.md File

Hi there,

This project looks pretty cool and simple and I'm attempting to use it in my Scala / Android project.
However, the documentation seems out of date, especially the .done callback which expects the Response.Payload class to be a class class which can be pattern matched whereas, it is a simple class.

Any chance of an updated README.md?

Thanks!

How to extend types of Parsers?

Say, the response is a JSON which can be parsed to a student object:

case class Student(username: String, id: Int)

I want a Parser[Student] with parse method return a Task[Response[Student]]
How can I?

Creating responses in tests

I struggle a bit on how to test code that uses Communicator, Ideally I’d create a Task[Response.With[String]] by hand in test and exchange it in tests, but the trait is sealed and the constructor is package private.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.