class OrangeData::Transport
handles low-level http requests to orangedata, including auth
Constants
- DEFAULT_PRODUCTION_API_URL
- DEFAULT_TEST_API_URL
Public Class Methods
new(api_url=DEFAULT_TEST_API_URL, credentials=Credentials.default_test)
click to toggle source
# File lib/orange_data/transport.rb, line 15 def initialize(api_url=DEFAULT_TEST_API_URL, credentials=Credentials.default_test) raise ArgumentError, "Need full credentials for connection" unless credentials.valid? @credentials = credentials @api_url = api_url end
Public Instance Methods
get_correction(inn, document_id)
click to toggle source
# File lib/orange_data/transport.rb, line 210 def get_correction(inn, document_id) CorrectionResult.from_hash(get_entity("corrections/#{inn}/status/#{document_id}")) end
get_document(inn, document_id)
click to toggle source
# File lib/orange_data/transport.rb, line 202 def get_document(inn, document_id) ReceiptResult.from_hash(get_entity("documents/#{inn}/status/#{document_id}")) end
get_entity(sub_url)
click to toggle source
# File lib/orange_data/transport.rb, line 160 def get_entity(sub_url) res = transport.get(sub_url) case res.status when 200 return res.body when 202 # not processed yet return nil when 400 raise "Cannot get doc: #{res.body['errors'] || res.body}" when 401 raise 'Unauthorized' end end
ping()
click to toggle source
Below actual methods from api
# File lib/orange_data/transport.rb, line 178 def ping res = transport.get(''){|r| r.headers['Accept'] = 'text/plain' } res.status == 200 && res.body == "Nebula.Api v2" rescue StandardError => _e return false end
post_correction(data, raise_errors:true)
click to toggle source
# File lib/orange_data/transport.rb, line 206 def post_correction(data, raise_errors:true) post_entity 'corrections', data, raise_errors:raise_errors, result_class:CorrectionIntermediateResult end
post_document(data, raise_errors:true)
click to toggle source
# File lib/orange_data/transport.rb, line 198 def post_document(data, raise_errors:true) post_entity 'documents', data, raise_errors:raise_errors, result_class:ReceiptIntermediateResult end
post_document_validate(data)
click to toggle source
# File lib/orange_data/transport.rb, line 185 def post_document_validate(data) res = post_request 'validateDocument', data case res.status when 200 return true when 400 return res.body["errors"] else raise "Unexpected response: #{res.status} #{res.reason_phrase}" end end
post_entity(sub_url, data, raise_errors:true, result_class:IntermediateResult, retry_count:0)
click to toggle source
# File lib/orange_data/transport.rb, line 126 def post_entity(sub_url, data, raise_errors:true, result_class:IntermediateResult, retry_count:0) res = post_request(sub_url, data) case res.status when 201 return result_class.new(success: true, data:data, sub_url:sub_url, retry_count:retry_count, transport:self) when 409 raise "Conflict" if raise_errors return result_class.new(data:data, sub_url:sub_url, errors:["Duplicate id"], retry_count:retry_count) when 400 raise "Invalid doc: #{res.body['errors'] || res.body}" if raise_errors return result_class.new(data:data, sub_url:sub_url, errors:res.body['errors'], retry_count:retry_count) when 503 if res.headers['Retry-After'] raise "Document queue full, retry in #{res.headers['Retry-After']}" if raise_errors return result_class.new( attempt_retry: true, retry_in: res.headers['Retry-After'].to_i, data: data, sub_url: sub_url, retry_count: retry_count, transport: self ) end end raise "Unknown code from OD: #{res.status} #{res.reason_phrase} #{res.body}" if raise_errors result_class.new(attempt_retry:true, data:data, sub_url:sub_url, retry_count:0, transport:self) end
post_request(method, data)
click to toggle source
# File lib/orange_data/transport.rb, line 59 def post_request(method, data) raw_post(method, data).tap{|resp| if resp.status == 401 # TODO: better exceptions raise 'Unauthorized' end } end
raw_post(method, data)
click to toggle source
# File lib/orange_data/transport.rb, line 55 def raw_post(method, data) transport.post(method, data) end
transport()
click to toggle source
# File lib/orange_data/transport.rb, line 38 def transport @transport ||= Faraday.new( url: @api_url, ssl: { client_cert: @credentials.certificate, client_key: @credentials.certificate_key, } ) do |conn| conn.headers['User-Agent'] = "OrangeDataRuby/#{OrangeData::VERSION}" conn.headers['Accept'] = "application/json" conn.request(:json) conn.use(RequestSignatureMiddleware, @credentials.signature_key) conn.response :json, content_type: /\bjson$/ conn.adapter(Faraday.default_adapter) end end