lib/buildbox/api.rb in buildbox-0.0.4 vs lib/buildbox/api.rb in buildbox-0.1
- old
+ new
@@ -1,112 +1,57 @@
+require 'rubygems'
+require 'faraday'
+require 'faraday_middleware'
+require 'hashie/mash'
+
module Buildbox
class API
- require 'net/http'
- require 'openssl'
- require 'json'
-
- def initialize(options)
- @options = options
- @queue = ::Queue.new
+ def initialize(config = Buildbox.config)
+ @config = config
end
- def crash(exception, information = {})
- payload = {
- :exception => exception.class.name,
- :message => exception.message,
- :backtrace => exception.backtrace,
- :meta => {}
- }
-
- payload[:meta][:worker_uuid] = worker_uuid if worker_uuid
- payload[:meta][:build_uuid] = information[:build] if information[:build]
- payload[:meta][:client_version] = Buildbox::VERSION
-
- request(:post, "crashes", payload)
+ def worker(access_token: access_token, hostname: hostname)
+ put("workers/#{access_token}", :hostname => hostname)
end
- def register(payload)
- request(:post, "workers", payload)
+ def scheduled_builds(project)
+ get(project.scheduled_builds_url).map { |build| Buildbox::Build.new(build) }
end
- def login
- request(:get, "user")
+ def update_build(build)
+ put(build.url, :output => build.output, :exit_status => build.exit_status)
end
- def builds(options = {})
- request(:get, "workers/#{worker_uuid}/builds")
- end
-
- def update_build_state_async(*args)
- async :update_build_state, args
- end
-
- def update_build_state(build_uuid, state)
- request(:put, "workers/#{worker_uuid}/builds/#{build_uuid}", :state => state)
- end
-
- def update_build_result_async(*args)
- async :update_build_result, args
- end
-
- def update_build_result(build_uuid, result_uuid, attributes)
- request(:put, "workers/#{worker_uuid}/builds/#{build_uuid}/results/#{result_uuid}", attributes)
- end
-
private
- def async(method, args)
- @queue << [ method, args ]
+ def connection
+ @connection ||= Faraday.new(:url => @config.api_endpoint) do |faraday|
+ faraday.request :json
- if !@thread || !@thread.alive?
- @thread = Thread.new do
- loop do
- async_call = @queue.pop
- Thread.exit if async_call.nil?
+ faraday.response :logger
+ faraday.response :mashify
- self.send(async_call[0], *async_call[1])
- end
- end
+ # json needs to come after mashify as it needs to run before the mashify
+ # middleware.
+ faraday.response :json
- @thread.abort_on_exception = true
+ faraday.adapter Faraday.default_adapter
end
end
- def http(uri)
- Net::HTTP.new(uri.host, uri.port).tap do |http|
- http.use_ssl = uri.scheme == "https"
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- end
+ def post(path, body = {})
+ connection.post(path) do |request|
+ request.body = body
+ end.body
end
- def request(method, path, payload = nil)
- klass = case method
- when :get then Net::HTTP::Get
- when :put then Net::HTTP::Put
- when :post then Net::HTTP::Post
- else raise "No request class defined for `#{method}`"
- end
-
- uri = URI.parse(endpoint(path))
- request = klass.new(uri.request_uri, 'Content-Type' => 'application/json',
- 'Accept' => 'application/json')
-
- if payload.nil?
- Buildbox.logger.debug "#{method.to_s.upcase} #{uri}"
- else
- request.body = JSON.dump(payload)
- Buildbox.logger.debug "#{method.to_s.upcase} #{uri} #{payload.inspect}"
- end
-
- Response.new http(uri).request(request)
+ def put(path, body = {})
+ connection.put(path) do |request|
+ request.body = body
+ end.body
end
- def worker_uuid
- Buildbox.configuration.worker_uuid
- end
-
- def endpoint(path)
- (Buildbox.configuration.use_ssl ? "https://" : "http://") +
- "#{Buildbox.configuration.endpoint}/v#{Buildbox.configuration.api_version}/#{path}"
+ def get(path)
+ connection.get(path).body
end
end
end