jsonapi-scala-http4s-client

jsonapi.org scala implementation

License

License

Categories

Categories

Scala Languages Ant Build Tools CLI User Interface JSON Data
GroupId

GroupId

com.qvantel
ArtifactId

ArtifactId

jsonapi-scala-http4s-client_2.12
Last Version

Last Version

10.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

jsonapi-scala-http4s-client
jsonapi.org scala implementation
Project URL

Project URL

https://github.com/qvantel/jsonapi-scala
Project Organization

Project Organization

com.qvantel
Source Code Management

Source Code Management

https://github.com/qvantel/jsonapi-scala

Download jsonapi-scala-http4s-client_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.qvantel/jsonapi-scala-http4s-client_2.12/ -->
<dependency>
    <groupId>com.qvantel</groupId>
    <artifactId>jsonapi-scala-http4s-client_2.12</artifactId>
    <version>10.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.qvantel/jsonapi-scala-http4s-client_2.12/
implementation 'com.qvantel:jsonapi-scala-http4s-client_2.12:10.2.0'
// https://jarcasting.com/artifacts/com.qvantel/jsonapi-scala-http4s-client_2.12/
implementation ("com.qvantel:jsonapi-scala-http4s-client_2.12:10.2.0")
'com.qvantel:jsonapi-scala-http4s-client_2.12:jar:10.2.0'
<dependency org="com.qvantel" name="jsonapi-scala-http4s-client_2.12" rev="10.2.0">
  <artifact name="jsonapi-scala-http4s-client_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.qvantel', module='jsonapi-scala-http4s-client_2.12', version='10.2.0')
)
libraryDependencies += "com.qvantel" % "jsonapi-scala-http4s-client_2.12" % "10.2.0"
[com.qvantel/jsonapi-scala-http4s-client_2.12 "10.2.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.10
com.qvantel : jsonapi-scala-core_2.12 jar 10.2.0
org.http4s : http4s-dsl_2.12 jar 0.21.8
org.http4s : http4s-blaze-client_2.12 jar 0.21.8

test (5)

Group / Artifact Type Version
org.http4s : http4s-blaze-server_2.12 jar 0.21.8
org.specs2 : specs2-core_2.12 jar 4.10.5
org.specs2 : specs2-junit_2.12 jar 4.10.5
org.specs2 : specs2-scalacheck_2.12 jar 4.10.5
org.specs2 : specs2-matcher-extra_2.12 jar 4.10.5

Project Modules

There are no modules declared in this project.

http://jsonapi.org/ implementation in scala

Build Status codecov Maven Central

Features

  • Automatic generation of jsonapi json writers with relationship handling for case classes

Requirements

  • Tested to work on Scala 2.12.10 or 2.13.3
  • If you are using Scala 2.12 use the macro paradise plugin. Add addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full) into your build.sbt somewhere.
    If you are using Scala 2.13 you should instead use the compiler flag -Ymacro-annotations.
    An example for handling cross-compiling with both can be found in project/MacrosCompiler.

Example

import _root_.spray.json.DefaultJsonProtocol._
import com.qvantel.jsonapi._

implicit val apiRoot: ApiRoot = ApiRoot(None)

@jsonApiResource final case class Employee(id: String, name: String)
@jsonApiResource final case class Company(id: String, name: String, employees: ToMany[Employee])

val acme = Company("1", "acme", ToMany.loaded(Seq(Employee("1", "number one 1"))))

val json = rawOne(acme)
val parsed = readOne[Company](json, Set("employees"))

acme == parsed // true

or see https://github.com/Doikor/jsonapi-scala-example

Known issues

  • loop handing on reader side (relationship path has to be given manually)

JsonApiClient

There is a very generic JsonApiClient interface for implementing a simple client interface for handling the http query writing side of this

The subproject "akka-client" has an implementation of this using akka-http

The subproject "http4s-client" has an implementation of this using http4s

Usage

import cats.data.OptionT
import com.qvantel.jsonapi.JsonApiClient

val jac = JsonApiClient.instance // won't work if you don't have an actual implementations stuff in scope. See setup.

val one: IO[Option[BillingAccount]] = jac.one[BillingAccount]("ba1") 
val many: IO[List[BillingAccount]] = jac.many[BillingAccount](Set("ba1", "ba2"))

// can also load includes at the same time
val withIncludes = jac.one[BillingAccount]("ba1", Set("customer-account"))

// includes can also be loaded on their own with a method
val ba: OptionT[IO, BillingAccount]  = OptionT(jac.one[BillingAccount]("ba"))
val ca: OptionT[IO, CustomerAccount] = ba.semiflatMap(_.customerAccount.load)

// filtering support
val filtered = jac.filter[BillingAccount]("some nice filter string here")

Setup

akka-http client

// needs ActorSystem and Materializer for akka-http
// the ApiEndPoint is used to as the "root" where to launch queries
import io.lemonlabs.uri.typesafe.dsl._
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import com.qvantel.jsonapi.ApiEndpoint
import com.qvantel.jsonapi.JsonApiClient
import com.qvantel.jsonapi.client.akka.AkkaClient._

implicit val system: ActorSystem  = ActorSystem()
implicit val materializer: ActorMaterializer = ActorMaterializer()
implicit val endpoint: ApiEndpoint = ApiEndpoint.Static("http://localhost:8080/api")

val jac = JsonApiClient.instance

http4s client

Setup for http4s client

import io.lemonlabs.uri.typesafe.dsl._
import org.http4s.client.Client
import org.http4s.client.blaze.Http1Client
import cats.effect.IO
import com.qvantel.jsonapi.ApiEndpoint
import com.qvantel.jsonapi.JsonApiClient

import com.qvantel.jsonapi.client.http4s.Http4sClient._
import com.qvantel.jsonapi.client.http4s.JsonApiInstances._


implicit val endpoint: ApiEndpoint = ApiEndpoint.Static("http://localhost:8080/api")

implicit val client: Client[IO] = Http1Client[IO]().unsafeRunSync()

val jac = JsonApiClient.instance
com.qvantel

Qvantel

Versions

Version
10.2.0
10.1.0
10.0.1
10.0.0
9.4.0
9.3.1
9.3.0
9.2.0
9.1.0
9.0.1
9.0.0
8.3.0
8.2.0
8.1.1
8.1.0
8.0.0
7.0.0
6.1.1
6.1.0
6.0.2
6.0.1
6.0.0