Skip to content

Different API versions working at the same time

srfrog edited this page Aug 9, 2014 · 2 revisions

Create two or more services that can share resources (even filters):

tickets := &Tickets{} // resource object

// first service handles clients using version 1
svc1 := relax.NewService("/api/v1")
// use handler tickets.Read for GET requests with ticketid (unsigned int)
res1 := svc1.Resource(tickets).GET("{uint:ticketid}", tickets.Read)

// notice that we use the same resource collection "tickets"

// second service handles clients using version 2 (or current)
svc2 := relax.NewService("/api/v2")
// same as res1 but uses a different version of the handler, updated API perhaps.
res2 := svc2.Resource(tickets).GET("{uint:ticketid}", tickets.NewReadVersion)

Alternatively, you can get the version from the clients' Accept header:

~$ curl -i -X GET -H 'Accept: application/vnd.relax+json;version=1.1' https://api.company.com

Then use the requested version in the code:

// tickets.Read handler from above...
func (tix *Tickets) Read(ctx *relax.Context) {
  ver := ctx.Info.GetFloat("content.version") // should be 1.1
  if ver >= 1.0 {
    // new API stuff here...
  } else {
    // older API stuff
  }
}