Git Product home page Git Product logo

jsonhomeclient's Introduction

jsonhomeclient

Build Status

No-magic Scala client to consume JSON home documents like this:

{
  "resources": {
    "http://spec.example.org/rels/artists": {
      "href": "/artists"
    },
    "http://spec.example.org/rels/artist": {
	  "href-template": "/artists/{artist-id}",
      "href-vars": {
        "artist-id": "http://spec.example.org/params/artist"
      }
	}
  }
}

The jsonhomeclient regularly loads json home documents from json home document providers, and caches them in memory. When your application retrieves some information from this jsonhomeclient, the cached json home documents are used so that there'll be no network roundtrip.

This library was built for Play! Framework projects and uses Play's WS client and Play's JSON library.

Installation

You must add the jsonhomeclient to the dependencies of the build file, e.g. add to build.sbt:

libraryDependencies += "de.kaufhof" %% "json-home-client" % "1.0.0-RC1"

It is published to maven central for both scala 2.10 and 2.11.

Usage

At first you setup the JsonHomeService for various hosts that provide json home documents. The first host serves a json home like the one shown above.

import de.kaufhof.jsonhomeclient._

// Specify which service/host provides which link relations
// By default, json home docs are assumed to be served from `/.well-known/home`.
val host1 = JsonHomeHost("http://some.host", Seq(
  // A link relation without params
  DirectLinkRelationType("http://spec.example.org/rels/artists"),
  // A link relation with template params
  TemplateLinkRelationType("http://spec.example.org/rels/artist")
))

// Create the client, it loads the json home doc from the host (using Play's WSClient for http)
// In a non-Play app, the WSClient can be created via `new NingWSClient(new AsyncHttpClientConfig.Builder().build())`,
// in this setup you should also `close()` the WSClient when the app is stopped.
val client1 = new JsonHomeClient(host1, WS.client)

// Create the cache, it will regularly (using the Akka scheduler) load json home doc
// using the client. The initialTimeToWait is used for requests when the schedule did not yet
// kick in (might happen e.g. in tests) so that the json home doc was not yet requested/loaded.
// In this case the json home doc is directly loaded from the client and the cache will wait for
// the result using the given `initialTimeToWait`.
val cache1 = new JsonHomeCache(client1, Akka.system, updateInterval = 5 minutes, initialTimeToWait = 10 seconds)

// The json home service, the interface for applications, set up with several json home caches
val jsonHomeService = JsonHomeService(Seq(cache1, cache2, ...))

Once the json home service is set up, you can use it to retrieve links for link-relations.

// Example 1: Get a link relation that does not expect template params
jsonHomeService.getUrl(
  host1,
  DirectLinkRelationType("http://spec.example.org/rels/artists")
) == Some("/artists")

// Example 2: Some link relation that expects template params
jsonHomeService.getUrl(
  host1,
  TemplateLinkRelationType("http://spec.example.org/rels/artist"),
  Map("artist-id" -> "acdc")
) == Some("/artists/acdc")

License

The license is Apache 2.0, see LICENSE.

jsonhomeclient's People

Contributors

magro avatar gkhmsc01 avatar manuelkiessling avatar justjoheinz avatar fkoehler avatar

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.