Class: Bio::BaseSpace::BaseAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/basespace/api/base_api.rb

Overview

Parent class for BaseSpaceAPI and BillingAPI objects. It provides rudimentary API access functionality.

Direct Known Subclasses

BaseSpaceAPI, BillingAPI

Instance Method Summary collapse

Constructor Details

#initialize(access_token = nil) ⇒ BaseAPI

Create a new BaseAPI instance using a given access token.

access_token

Access token provided by App triggering.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/basespace/api/base_api.rb', line 32

def initialize(access_token = nil)
  if  $DEBUG
    $stderr.puts "    # ----- BaseAPI#initialize ----- "
    $stderr.puts "    # caller: #{caller}"
    $stderr.puts "    # access_token: #{access_token}"
    $stderr.puts "    # "
  end
  @api_client = nil
  set_timeout(10)
  set_access_token(access_token)        # logic for setting the access-token 
end

Instance Method Details

#get_access_tokenObject

Returns the access-token that was used to initialize the BaseSpaceAPI object.



228
229
230
231
232
233
# File 'lib/basespace/api/base_api.rb', line 228

def get_access_token
  if @api_client
    return @api_client.api_key
  end
  return ""  # [TODO] Should return nil in Ruby?
end

#get_server_uriObject

Returns the server URI used by this instance.



236
237
238
# File 'lib/basespace/api/base_api.rb', line 236

def get_server_uri
  return @api_client.api_server
end

#hash2urlencode(hash) ⇒ Object

URL encode a Hash of data values.

hash

data encoded in a Hash.



155
156
157
158
159
# File 'lib/basespace/api/base_api.rb', line 155

def hash2urlencode(hash)
  # URI.escape (alias URI.encode) is obsolete since Ruby 1.9.2.
  #return hash.map{|k,v| URI.encode(k.to_s) + "=" + URI.encode(v.to_s)}.join("&")
  return hash.map{|k,v| URI.encode_www_form_component(k.to_s) + "=" + URI.encode_www_form_component(v.to_s)}.join("&")
end

#list_request(my_model, resource_path, method, query_params, header_params, verbose = false, no_api = true) ⇒ Object

Send a list request to the BaseSpace REST API.

my_model

Class or classname of the model that is applied in deserialization.

resource_path

URI that should be used for the API call.

method

HTTP method for the rest call (GET, POST, DELETE, etc.)

query_params

query parameters that should be sent along to the API.

header_params

Additional settings that should be transferred in the HTTP header.

verbose

Truth value indicating whether verbose output should be provided.

no_api

(unclear; TODO)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/basespace/api/base_api.rb', line 109

def list_request(my_model, resource_path, method, query_params, header_params, verbose = false, no_api = true)
  # test if access-token has been set
  if not @api_client and no_api
    raise 'Access-token not set, use the "set_access_token"-method to supply a token value'
  end
  
  # Make the API Call
  if verbose or $DEBUG
    $stderr.puts "    # ----- BaseAPI#list_request ----- "
    $stderr.puts "    # caller: #{caller}"
    $stderr.puts "    # Path: #{resource_path}"
    $stderr.puts "    # Pars: #{query_params}"
    $stderr.puts "    # "
  end
  response = @api_client.call_api(resource_path, method, query_params, nil, header_params)  # post_data = nil
  if verbose or $DEBUG
    $stderr.puts "    # ----- BaseAPI#list_request ----- "
    $stderr.puts "    # response: #{response.inspect}"
    $stderr.puts "    # "
  end
  unless response
    raise "BaseSpace Exception: No data returned"
  end
  unless response.kind_of?(Array)  # list
    response = [response]
  end
  response_objects = []
  response.each do |response_object|
    response_objects << @api_client.deserialize(response_object, 'ListResponse')
  end
  
  # convert list response dict to object type
  # TODO check that Response is present -- errors sometime don't include
  # [TODO] check why the Python SDK only uses the first element in the response_objects
  converted = []
  if response_object = response_objects.first
    response_object.convert_to_object_list.each do |c|
      converted << @api_client.deserialize(c, my_model)
    end
  end
  return converted
end

#make_curl_request(data, url) ⇒ Object

Post data to the given BaseSpace API URL. Method name is a bit of a misnomer, because ‘curl’ is not used by this method – instead only Ruby core classes are used.

data

Data that should be transferred to the API.

url

URL of the BaseSpace API.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/basespace/api/base_api.rb', line 167

def make_curl_request(data, url)
  if $DEBUG
    $stderr.puts "    # ----- BaseAPI#make_curl_request ----- "
    $stderr.puts "    # caller: #{caller}"
    $stderr.puts "    # data: #{data}"
    $stderr.puts "    # url: #{url}"
    $stderr.puts "    # "
  end
  post = hash2urlencode(data)
  uri = URI.parse(url)
  #res = Net::HTTP.post_form(uri, post).body
  http_opts = {}
  if uri.scheme == "https"
    http_opts[:use_ssl] = true
  end
  res = Net::HTTP.start(uri.host, uri.port, http_opts) { |http|
    http.post(uri.path, post)
  }
  obj = JSON.parse(res.body)
  if $DEBUG
    $stderr.puts "    # res: #{res}"
    $stderr.puts "    # obj: #{obj}"
    $stderr.puts "    # "
  end
  if obj.has_key?('error')
    raise "BaseSpace exception: " + obj['error'] + " - " + obj['error_description']
  end
  return obj
end

#set_access_token(token) ⇒ Object

Sets a new API token.

token

New API token.



220
221
222
223
224
225
# File 'lib/basespace/api/base_api.rb', line 220

def set_access_token(token)
  @api_client = nil
  if token
    @api_client = APIClient.new(token, @api_server, @timeout)
  end
end

#set_timeout(time) ⇒ Object

Specify the timeout in seconds for each request.

param time

Timeout in second.



210
211
212
213
214
215
# File 'lib/basespace/api/base_api.rb', line 210

def set_timeout(time)
  @timeout = time
  if @api_client
    @api_client.timeout = @timeout
  end
end

#single_request(my_model, resource_path, method, query_params, header_params, post_data = nil, verbose = false, force_post = false, no_api = true) ⇒ Object

Make a single request to the BaseSpace REST API.

my_model

Class or classname of the model that is applied in deserialization.

resource_path

URI that should be used for the API call.

method

HTTP method for the rest call (GET, POST, DELETE, etc.)

query_params

query parameters that should be sent along to the API.

header_params

Additional settings that should be transferred in the HTTP header.

post_data

Hash that contains data to be transferred.

verbose

Truth value indicating whether verbose output should be provided.

force_post

Truth value that indicates whether a POST should be forced.

no_api

(unclear; TODO)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/basespace/api/base_api.rb', line 63

def single_request(my_model, resource_path, method, query_params, header_params, post_data = nil, verbose = false, force_post = false, no_api = true)
  # test if access-token has been set
  if not @api_client and no_api
    raise 'Access-token not set, use the "set_access_token"-method to supply a token value'
  end
  # Make the API Call
  if verbose or $DEBUG
    $stderr.puts "    # ----- BaseAPI#single_request ----- "
    $stderr.puts "    # caller: #{caller}"
    $stderr.puts "    # resource_path: #{resource_path}"
    $stderr.puts "    # method: #{method}"
    $stderr.puts "    # query_params: #{query_params}"
    $stderr.puts "    # post_data: #{post_data}"
    $stderr.puts "    # header_params: #{header_params}"
    $stderr.puts "    # force_post: #{force_post}"
    $stderr.puts "    # "
  end
  response = @api_client.call_api(resource_path, method, query_params, post_data, header_params, force_post)
  if verbose or $DEBUG
    $stderr.puts "    # ----- BaseAPI#single_request ----- "
    $stderr.puts "    # response: #{response.inspect}"
    $stderr.puts "    # "
  end
  unless response
    raise 'BaseSpace error: None response returned'
  end

  # throw exception here for various error messages
  if response['ResponseStatus'].has_key?('ErrorCode')
    raise "BaseSpace error: #{response['ResponseStatus']['ErrorCode']}: #{response['ResponseStatus']['Message']}"
  end
   
  # Create output objects if the response has more than one object
  response_object = @api_client.deserialize(response, my_model)
  return response_object.response
end

#to_sObject

Return a string representation of this object.



198
199
200
# File 'lib/basespace/api/base_api.rb', line 198

def to_s
  return "BaseSpaceAPI instance - using token=#{get_access_token}"
end

#to_strObject

Return a string representation of this object.



203
204
205
# File 'lib/basespace/api/base_api.rb', line 203

def to_str
  return self.to_s
end

#update_access_token(access_token) ⇒ Object

Set a new access token. The value of a previously supplied access token is simply overwritten here.

access_token

Access token provided by App triggering.



48
49
50
# File 'lib/basespace/api/base_api.rb', line 48

def update_access_token(access_token)
  @api_client.api_key = access_token
end