Ruby
TreasureData

Treasure DataのWorkflowをRubyでキックする

More than 1 year has passed since last update.
require 'net/http'
require 'uri'
require 'json'
require 'time'

module Digdag
  class Client
    BASE_URL = 'https://api-workflow.treasuredata.com/api/'

    def initialize(key)
      @key = key
    end

    def get_sessions
      get('sessions')
    end

    def start_attempt(workflow_id, params = {})
      put('attempts', workflowId: workflow_id, params: params, sessionTime: Time.now.iso8601)
    end

    private

    def get(path)
      request(Net::HTTP::Get, path)
    end

    def put(path, params)
      request(Net::HTTP::Put, path, params)
    end

    def request(method_class, path, params = nil)
      uri = URI.parse(File.join(BASE_URL, path))
      https = Net::HTTP.new(uri.host, uri.port)
      https.use_ssl = true

      req = method_class.new(uri.request_uri)
      req['Content-Type'] = 'application/json'
      req['Authorization'] = "TD1 #{@key}"
      req.body = params.to_json if params

      res = https.request(req)
      JSON.parse(res.body)
    end
  end
end

td_apikey = 'xxx'
workflow_id = 12345
workflow_params = { entity_id: 10000 }

client = Digdag::Client.new(td_apikey)
p client.start_attempt(workflow_id, workflow_params)

キックしたいWorkflowのIDとパラメータは適当な値に変更してお使いください