2016-05-10

Heroku ConnectのAPI叩いてみた

Heroku Conncect APIを叩いてみましたー。ということで、いつもどおり備忘録

認証

認証用のAPIキーを取得 ```bash $ heroku auth:token ``` HerokuのAccountページのAPI Keyのセクションでも確認可能です(要パスワード) heroku_api_key1 Regenerate API KeyをクリックするとAPIキーが表示されます。 heroku_api_key2 Direct Authorization Tokenの取得 ```bash $ curl -i -X POST \ -H "Authorization:Basic {base64encode(':{api_key}')}" \ 'https://api.heroku.com/oauth/authorizations' ``` Basic認証でユーザ名を空白、パスワードをAPIキーにしたものをセットすればOK レスポンスはこんな感じ↓ ```js { "created_at":"2016/05/05 00:45:17 -0700", "description":"Long-lived user authorization", "id":"15d2306c-4567-47bf-bd0c-38989a00e7e6", "scopes":[ "global" ], "updated_at":"2016/05/05 00:45:17 -0700", "access_tokens":[ { "token":"{token}", "expires_in":null } ], "client":null, "grants":[], "refresh_tokens":[] } ```

Herokuアプリのコネクション一覧の取得

$ curl -i -X GET \
-H "Authorization:Bearer {token}" \
'https://connect-us.heroku.com/api/v3/connections?app={app}'

レスポンスはこんな感じ↓

{
  "count": 1,
  "next": null,
  "previous": null,
  "results": [
    {
      "id": "{connection_id}",
      "_id": xxxxx,
      "name": "hogehoge",
      "app_name": "hogehoge",
      ...
}

コネクションの詳細表示

$ curl -i -X GET \
-H "Authorization:Bearer {token}" \
'https://connect-us.heroku.com/api/v3/connections/{connection_id}?deep=true'

レスポンスはこんな感じ↓

{
  "id": "{connection_id}",
  "_id": xxxxx,
  ...
  "schema_name": "salesforce",
  "db_key": "DATABASE_URL",
  "database": {
    "host": "ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com",
    "port": xxxx,
    "database": "xxxxxxxxxx"
  },
  ...
  "mappings": [
    {
      "id": "{mapping_id}",
      "_id": xxxxx,
      "object_name": "Account",
      "state": "POLLING_SF_CHANGES",
      "config": {
        "fields": {
          "Name": {},
          "Hoge__c": {},
          "Fuga__c": {},
          ...
}

マッピングの作成

マッピングの作成はこんな感じで出来ます。

$ curl -i -X POST \
   -H "Authorization:Bearer {token}" \
   -H "Content-Type:application/json" \
   -d \
'{
  "object_name": "Contact",
  "config": {
    "access": "read_write",
    "sf_notify_enabled": true,
    "sf_polling_seconds": 600,
    "fields": {
      "CreatedDate": {},
      "Id": {},
      "IsDeleted": {},
      "LastName": {},
      "FirstName": {},
      "Email": {},
      "SystemModstamp": {}
    },
    "indexes": {
      "Id": {
        "unique": false
      },
      "SystemModstamp": {
        "unique": false
      }
    }
  }
}' \
 'https://connect-us.heroku.com/api/v3/connections/{connection_id}/mappings'

access項目は”read_only”でSalesforceからHeroku Postgresの一方向同期、”read_write”で双方向同期になります。sf_notify_enabled項目はStreaming APIの利用有無を表します。

レスポンスはこんな感じ

{
  "id": "{mapping_id}",
  "_id": xxxxx,
  "object_name": "Contact",
  "state": "SCHEMA_CHANGED",
  "config": {
    "indexes": {
      "Id": {
        "unique": true
      },
      "SystemModstamp": {
        "unique": false
      }
    },
    "sf_polling_seconds": 600,
    "applied_at": "2016-05-08T07:19:55.552Z",
    "access": "read_write",
    "sf_notify_enabled": true,
    "revision": 111334,
    "fields": {
      "FirstName": {},
      "SystemModstamp": {},
      "Id": {},
      "LastName": {},
      "CreatedDate": {},
      "IsDeleted": {},
      "Email": {}
    }
  },
  ...
}

更新時も同じフォーマットでHTTPメソッドをPUTにして、revision項目を追加(現設定のrevision値を指定)してリクエストすればOKです。

一時停止/再開

一時停止

$ curl -X POST -H  "Authorization: Bearer {token}" \
https://connect-us.heroku.com/api/v3/connections/{connection_id}/actions/pause

再開

$ curl -X POST -H  "Authorization: Bearer {token}" \
https://connect-us.heroku.com/api/v3/connections/{connection_id}/actions/resume

どちらも成功時はHTTPステータスコードで202 Acceptedが返ってきます。

参考URL

このエントリーをはてなブックマークに追加