Code Monkey home page Code Monkey logo

Comments (1)

tobowers avatar tobowers commented on August 15, 2024

curies for links

Let's take the people collection and the person object as references for how to use curies.

curies define the link type of a link (or list of links) and can be either global as defined here or application specific by using curies.

Let's assume for a second that we are using osdi as our curie prefix for links and let's take the person object's link section from here.

where it currently says:

"_links": {
          "addresses": {
            "href": "http://osdi-prototype.herokuapp.com/api/v1/people/23/addresses"
          },
          "question_answers": {
            "href": "http://osdi-prototype.herokuapp.com/api/v1/people/23/question_answers"
          },
          "self": {
            "href": "http://osdi-prototype.herokuapp.com/api/v1/people/23"
          }
        }

it would become:

"_links": {
          "self": {
            "href": "http://osdi-prototype.herokuapp.com/api/v1/people/23"
          },
          "curies": [{ "name": "osdi", "href": "http://osdi-prototype.herokuapp.com/docs/rels/{rel}", "templated": true }],
          "osdi:addresses": {
            "href": "http://osdi-prototype.herokuapp.com/api/v1/people/23/addresses"
          },
          "osdi:question_answers": {
            "href": "http://osdi-prototype.herokuapp.com/api/v1/people/23/question_answers"
          }
        }

the curie is supposed to be a link type so let's take the addresses example and let's say we wanted to individually link from person... we could do that as such:

"_links": {
          "self": {
            "href": "http://osdi-prototype.herokuapp.com/api/v1/people/23"
          },
          "curies": [{ "name": "osdi", "href": "http://osdi-prototype.herokuapp.com/docs/rels/{rel}", "templated": true }],
          "osdi:address": [{
            "href": "http://osdi-prototype.herokuapp.com/api/v1/people/23/addresses",
            "title": "home"
          },{
            "href": "http://osdi-prototype.herokuapp.com/api/v1/people/23/addresses",
            "title": "work"  
          }],
          "osdi:question_answers": {
            "href": "http://osdi-prototype.herokuapp.com/api/v1/people/23/question_answers"
          }
        }

The curie part of the links section is a way to construct links to documentation. It's actually perfectly valid to just drop a "coming soon" link in there
if you don't have docs.

"curies": [{ "name": "osdi", "href": "http://osdi-prototype.herokuapp.com/docs/coming_soon.html"}]

Now you might say: "ok that's not much work, but what's the benefit?"

Well, the benefits are three fold.

  1. The curie can be used by the client to pick out which code should handle that link (think models)
  2. We have a baked in way of defining provider-specific links or links out to other APIs
  3. The client can now use templates to construct urls it'd otherwise have to know.

For number 3, let's take the People collection as an example.

{
  "total_pages": 1,
  "page": 1,
  "total_records": 0,
  "_embedded": {
    "people": []
  },
  "_links": {
    "self": {
      "href": "http://osdi-prototype.herokuapp.com/api/v1/people"
    },
    "curies": [{ "name": "osdi", "href": "http://osdi-prototype.herokuapp.com/docs/rels/{rel}", "templated": true }],
    "osdi:addresses": {
      "href": "http://osdi-prototype.herokuapp.com/api/v1/addresses"
    },
    "osdi:find": { "href": "/people{?id}", "templated": true },
    "osdi:filter": { "href": "/people{?odata_query}", "templated": true },
  }
}

Template syntax is defined here

Now, as a client, i can easily determine how to find a person by ID without having to just know the uri (a basic tenet of JSON+HAL).

Curies are also not very verbose and are human readable.

from osdi-docs.

Related Issues (20)

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.