aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/ievkit/client.rb45
-rw-r--r--lib/ievkit/client/jobs.rb18
-rw-r--r--lib/ievkit/default.rb7
-rw-r--r--lib/ievkit/error.rb42
4 files changed, 85 insertions, 27 deletions
diff --git a/lib/ievkit/client.rb b/lib/ievkit/client.rb
index 53eb61074..c304a029d 100644
--- a/lib/ievkit/client.rb
+++ b/lib/ievkit/client.rb
@@ -66,6 +66,15 @@ module Ievkit
def post(url, options = {})
request :post, url, options
end
+
+ # Make a HTTP POST request
+ #
+ # @param url [String] The path, relative to {#api_endpoint}
+ # @param options [Hash] Body and header params for request
+ # @return [Sawyer::Resource]
+ def multipart_post(url, options = {})
+ multipart_request :post, url, options
+ end
# Make a HTTP PUT request
#
@@ -140,6 +149,27 @@ module Ievkit
# Hypermedia agent for the Iev API
#
# @return [Sawyer::Agent]
+ def multipart_agent
+ @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
+ http.headers[:accept] = default_media_type
+ http.headers[:content_type] = "multipart/form-data"
+ http.headers[:user_agent] = user_agent
+
+ # Activate if authentication is needed
+ #
+ # if basic_authenticated?
+ # http.basic_auth(@login, @password)
+ # elsif token_authenticated?
+ # http.authorization 'token', @access_token
+ # elsif application_authenticated?
+ # http.params = http.params.merge application_authentication
+ # end
+ end
+ end
+
+ # Hypermedia agent for the Iev API
+ #
+ # @return [Sawyer::Agent]
def agent
@agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
http.headers[:accept] = default_media_type
@@ -253,6 +283,19 @@ module Ievkit
@agent = nil
end
+ def multipart_request(method, path, data, options = {})
+ if data.is_a?(Hash)
+ options[:query] = data.delete(:query) || {}
+ options[:headers] = data.delete(:headers) || {}
+ if accept = data.delete(:accept)
+ options[:headers][:accept] = accept
+ end
+ end
+
+ @last_response = response = multipart_agent.call(method, URI::Parser.new.escape(path.to_s), data, options)
+ response.data
+ end
+
def request(method, path, data, options = {})
if data.is_a?(Hash)
options[:query] = data.delete(:query) || {}
@@ -261,7 +304,7 @@ module Ievkit
options[:headers][:accept] = accept
end
end
-
+
@last_response = response = agent.call(method, URI::Parser.new.escape(path.to_s), data, options)
response.data
end
diff --git a/lib/ievkit/client/jobs.rb b/lib/ievkit/client/jobs.rb
index a530b2527..09bb8ff4b 100644
--- a/lib/ievkit/client/jobs.rb
+++ b/lib/ievkit/client/jobs.rb
@@ -9,7 +9,7 @@ module Ievkit
# @example Fetch all jobs for referential test
# client.jobs("test")
def jobs(referential, options = {})
- paginate "referentials/#{referential}/jobs", options
+ get "referentials/#{referential}/jobs", options
end
# Get scheduled job
@@ -40,8 +40,20 @@ module Ievkit
# @return [Sawyer::Resource] Hash representing the new job.
# @example
# client.create_job("test",....)
- def create_job(referential, options = {})
- post "jobs", options
+ def create_job(referential, action, format = "", options = {})
+ url = "referentials/#{referential}/#{action}"
+ url += "/#{format}" if format.present?
+ multipart_post url, options
+ end
+
+ # Delete jobs
+ #
+ # @param referential [String] Data referential name.
+ # @return [Boolean] Success
+ # @example
+ # client.delete_jobs("test")
+ def delete_jobs(referential)
+ boolean_from_response :delete, "referentials/#{referential}/jobs", options
end
end
diff --git a/lib/ievkit/default.rb b/lib/ievkit/default.rb
index af0df422f..fff1c1d50 100644
--- a/lib/ievkit/default.rb
+++ b/lib/ievkit/default.rb
@@ -25,10 +25,13 @@ module Ievkit
RACK_BUILDER_CLASS = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder
# Default Faraday middleware stack
- MIDDLEWARE = RACK_BUILDER_CLASS.new do |builder|
- builder.use Ievkit::Response::RaiseError
+ MIDDLEWARE = RACK_BUILDER_CLASS.new do |builder|
builder.use Faraday::Request::Multipart
+ builder.use Faraday::Request::UrlEncoded
+ builder.use Ievkit::Response::RaiseError
builder.use FaradayMiddleware::FollowRedirects
+ builder.use Faraday::Response::Logger
+
builder.adapter Faraday.default_adapter
end
diff --git a/lib/ievkit/error.rb b/lib/ievkit/error.rb
index 593ed25a3..c758ec71d 100644
--- a/lib/ievkit/error.rb
+++ b/lib/ievkit/error.rb
@@ -1,5 +1,5 @@
module Ievkit
- # Custom error class for rescuing from all GitHub errors
+ # Custom error class for rescuing from all Iev errors
class Error < StandardError
# Returns the appropriate Ievkit::Error subclass based
@@ -147,21 +147,21 @@ module Ievkit
# Raised on errors in the 400-499 range
class ClientError < Error; end
- # Raised when GitHub returns a 400 HTTP status code
+ # Raised when Iev returns a 400 HTTP status code
class BadRequest < ClientError; end
- # Raised when GitHub returns a 401 HTTP status code
+ # Raised when Iev returns a 401 HTTP status code
class Unauthorized < ClientError; end
- # Raised when GitHub returns a 401 HTTP status code
- # and headers include "X-GitHub-OTP"
+ # Raised when Iev returns a 401 HTTP status code
+ # and headers include "X-Iev-OTP"
class OneTimePasswordRequired < ClientError
#@private
OTP_DELIVERY_PATTERN = /required; (\w+)/i
#@private
def self.required_header(headers)
- OTP_DELIVERY_PATTERN.match headers['X-GitHub-OTP'].to_s
+ OTP_DELIVERY_PATTERN.match headers['X-Iev-OTP'].to_s
end
# Delivery method for the user's OTP
@@ -180,56 +180,56 @@ module Ievkit
end
end
- # Raised when GitHub returns a 403 HTTP status code
+ # Raised when Iev returns a 403 HTTP status code
class Forbidden < ClientError; end
- # Raised when GitHub returns a 403 HTTP status code
+ # Raised when Iev returns a 403 HTTP status code
# and body matches 'rate limit exceeded'
class TooManyRequests < Forbidden; end
- # Raised when GitHub returns a 403 HTTP status code
+ # Raised when Iev returns a 403 HTTP status code
# and body matches 'login attempts exceeded'
class TooManyLoginAttempts < Forbidden; end
- # Raised when GitHub returns a 403 HTTP status code
+ # Raised when Iev returns a 403 HTTP status code
# and body matches 'abuse'
class AbuseDetected < Forbidden; end
- # Raised when GitHub returns a 403 HTTP status code
+ # Raised when Iev returns a 403 HTTP status code
# and body matches 'repository access blocked'
class RepositoryUnavailable < Forbidden; end
- # Raised when GitHub returns a 404 HTTP status code
+ # Raised when Iev returns a 404 HTTP status code
class NotFound < ClientError; end
- # Raised when GitHub returns a 405 HTTP status code
+ # Raised when Iev returns a 405 HTTP status code
class MethodNotAllowed < ClientError; end
- # Raised when GitHub returns a 406 HTTP status code
+ # Raised when Iev returns a 406 HTTP status code
class NotAcceptable < ClientError; end
- # Raised when GitHub returns a 409 HTTP status code
+ # Raised when Iev returns a 409 HTTP status code
class Conflict < ClientError; end
- # Raised when GitHub returns a 414 HTTP status code
+ # Raised when Iev returns a 414 HTTP status code
class UnsupportedMediaType < ClientError; end
- # Raised when GitHub returns a 422 HTTP status code
+ # Raised when Iev returns a 422 HTTP status code
class UnprocessableEntity < ClientError; end
# Raised on errors in the 500-599 range
class ServerError < Error; end
- # Raised when GitHub returns a 500 HTTP status code
+ # Raised when Iev returns a 500 HTTP status code
class InternalServerError < ServerError; end
- # Raised when GitHub returns a 501 HTTP status code
+ # Raised when Iev returns a 501 HTTP status code
class NotImplemented < ServerError; end
- # Raised when GitHub returns a 502 HTTP status code
+ # Raised when Iev returns a 502 HTTP status code
class BadGateway < ServerError; end
- # Raised when GitHub returns a 503 HTTP status code
+ # Raised when Iev returns a 503 HTTP status code
class ServiceUnavailable < ServerError; end
# Raised when client fails to provide valid Content-Type