class Sypht::Client
Constants
- API_BASE_ENDPOINT
- AUTH_ENDPOINT
- FIELDSET
- FINALISED
Public Class Methods
new(client_id, client_secret, base_endpoint=API_BASE_ENDPOINT, auth_endpoint=AUTH_ENDPOINT)
click to toggle source
@param [String] client_id Your Sypht-provided OAuth client_id. @param [String] client_secret Your Sypht-provided OAuth client_secret
# File lib/sypht.rb, line 23 def initialize(client_id, client_secret, base_endpoint=API_BASE_ENDPOINT, auth_endpoint=AUTH_ENDPOINT) @client_id = client_id @client_secret = client_secret @base_endpoint = base_endpoint @auth_endpoint = auth_endpoint @audience = ENV["SYPHT_AUDIENCE"] || @base_endpoint @access_token = authenticate end
Public Instance Methods
fetch_results(file_id, endpoint=nil)
click to toggle source
# File lib/sypht.rb, line 62 def fetch_results(file_id, endpoint=nil) headers = { "Authorization": "Bearer " + @access_token } endpoint ||= API_BASE_ENDPOINT + "/result/final/" + file_id result = JSON.parse(RestClient::Request.execute(method: :get, url: endpoint, headers: headers, timeout: 300)) if result['status'] != FINALISED return nil end result['results'] end
upload(file, fieldSets, tags=nil, endpoint=nil, workflow=nil, options=nil)
click to toggle source
# File lib/sypht.rb, line 32 def upload(file, fieldSets, tags=nil, endpoint=nil, workflow=nil, options=nil) headers = { "Authorization": "Bearer " + @access_token, multipart: true } endpoint ||= API_BASE_ENDPOINT + "/fileupload" data = { fieldSets: parse_fieldSets(fieldSets), 'fileToUpload': File.new(file, 'rb') } if tags data['tags'] = tags end if workflow data['workflowId'] = workflow end if options data['workflowOptions'] = JSON.dump(options) end result = JSON.parse(RestClient.post(endpoint, data, headers)) print("fileId: " + result['fileId']) unless result.key?('fileId') raise Exception('Upload failed with response: ' + JSON.dump(result)) end result['fileId'] end
Private Instance Methods
authenticate()
click to toggle source
# File lib/sypht.rb, line 76 def authenticate endpoint = ENV["SYPHT_AUTH_ENDPOINT"] || @auth_endpoint data = { client_id: @client_id, client_secret: @client_secret, audience: @audience, grant_type: 'client_credentials' } result = JSON.parse(RestClient.post(endpoint, data)) if result['error_description'] raise Exception, 'Authentication failed: {}'.format(result['error_description']) else result['access_token'] end end
parse_fieldSets(fieldSets)
click to toggle source
# File lib/sypht.rb, line 92 def parse_fieldSets(fieldSets) out = [] fieldSets.each do |fs| if FIELDSET.key? fs out.append('"' + FIELDSET[fs] + '"') elsif FIELDSET.has_value? fs out.append('"' + fs + '"') else raise ArgumentError, "Undefined fileset" end end out return "[" + out.join(",") + "]" end