1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at
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とパラメータは適当な値に変更してお使いください

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?