DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Accessing the Heroku REST API via Dispatch

Accessing the Heroku REST API via Dispatch

Joachim Hofer user avatar by
Joachim Hofer
·
Oct. 14, 11 · Interview
Like (0)
Save
Tweet
Share
4.67K Views

Join the DZone community and get the full member experience.

Join For Free

Now that I have an extremely simple Heroku app up and running (see my previous post) and don’t know what to do with it, the logical next step for me was to check out Heroku’s REST API. :)

Ok, so first, let’s look up the REST API documentation…

… but wait, such an abstruse beast does not exist! – Instead, we get the Ruby sources of the Heroku REST client. That’s fine with me, except that I don’t speak Ruby so well. But never mind, this looks easy enough.

Let’s check out what to use as REST client API in Scala: Restlet, Jersey or something? – Nooo! Let’s use Dispatch. Dispatch wins the coolness competition hands down, if you ask me. You’ll see below, I hope.

Of course, I’ll use sbt for building my little project. We add the following two dependencies and are done with it:

libraryDependencies ++= Seq(
  "net.databinder" %% "dispatch-http" % "0.8.5",
  "net.databinder" %% "dispatch-lift-json" % "0.8.5")
}

Now let’s import all the good stuff:

implicit val formats = DefaultFormats

val http = new Http
private val user = "*my-heroku-user*"
private val password = "*my-heroku-password*"

val herokuApi = :/("api.heroku.com", 443).as(user, password)
val herokuHeaders = Map(
    "Accept" -> "application/json",
    "X-Heroku-Api-Version" -> "2")

I’m going to prepare a few helpers next, and then we’re ready to rumble:

implicit val formats = DefaultFormats

val http = new Http
private val user = "*my-heroku-user*"
private val password = "*my-heroku-password*"

val herokuApi = :/("api.heroku.com", 443).as(user, password)
val herokuHeaders = Map(
    "Accept" -> "application/json",
    "X-Heroku-Api-Version" -> "2")

Next, we can login, for example. This will return our api key, mostly (I don’t know yet what it’s good for though, seeing all the basic authentication that’s going on in the Ruby client).

def login = {
  http(resource(herokuApi / "login")
    < < Map("username" -> user, "password" -> password)
    ># (_.extract[User]) )
}

def resource(request: Request) =
  request.secure <: < herokuHeaders

(The "< <" and "<: <" above should really be "<<" and "<:<", don't ask me why WordPress puts in a space each time I save this post)

Wait, what's User? I have to define that before the above will work. It's just a simple case class which will be filled by Lift-JSON. And when I'm already at it, I'll define the case class for the next request here, too:

case class User(
    email: String,
    api_key: String,
    verified_at: Option[String])

case class HerokuApp(
    id: Int,
    name: String,
    stack: Option[String],
    requested_stack: Option[String],
    slug_size: Int,
    repo_size: Int,
    dynos: Int,
    workers: Int,
    created_at: String,
    create_status: String,
    repo_migrate_status: String)

Now I can call login and will receive something like this:
User(*my-heroku-user*,*my-api-key*,None)

This is fun!

Let's use the above HerokuApp case class to request our current Heroku apps:

def apps = http(herokuApi / "apps"
    ># (_.children map (_.extract[HerokuApp])))

Uhm... - yup, that's all there is to it. This returns a convenient list of apps I have running on Heroku:
List(HerokuApp(*my-id*,*my-app-name*,Some(cedar),None,*large-number-1*,*large-number-2*,0,0,2011/10/10 11:52:03 -0700,complete,complete))

This is great, isn't it? - I at least had no idea that it would turn out to be so very easy to connect to Heroku from Scala.

I still don't know what to put up on Heroku, though...

 

From http://jmhofer.johoop.de/?p=238

REST Web Protocols API

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fargate vs. Lambda: The Battle of the Future
  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • REST vs. Messaging for Microservices
  • Cloud Performance Engineering

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: