Class: Ubiquity::Wiredrive::API::V3::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ubiquity/wiredrive/api/v3/client.rb,
lib/ubiquity/wiredrive/api/v3/client/requests/base_request.rb

Direct Known Subclasses

Utilities

Defined Under Namespace

Classes: Paginator, Requests

Instance Attribute Summary collapse

API METHODS collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = { }) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 12

def initialize(args = { })
  _token_cache = args.fetch(:token_cache_allow, false)

  _token = args[:auth_token] || _token_cache ? token_cache_read(args) : nil
  args[:auth_token] = _token if _token.is_a?(String) # _token.is_a?(String) ? Token.new(_token, self) : _token if _token

  initialize_logger(args)
  initialize_http_client(args)

  unless _token
     = args.fetch(:do_login, true)
    if 
      _token = (args)
      args[:auth_token] = _token
      token_cache_write(args) if _token_cache && _token
    end
  end
end

Instance Attribute Details

#client_codeObject

Returns the value of attribute client_code.



10
11
12
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 10

def client_code
  @client_code
end

#http_clientObject

Returns the value of attribute http_client.



10
11
12
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 10

def http_client
  @http_client
end

#http_responseObject

Returns the value of attribute http_response.



10
11
12
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 10

def http_response
  @http_response
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 8

def logger
  @logger
end

#requestObject

Returns the value of attribute request.



10
11
12
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 10

def request
  @request
end

#responseObject

Returns the value of attribute response.



10
11
12
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 10

def response
  @response
end

Instance Method Details

#auth_token(args = { }, options = { }) ⇒ Object

################################################################################################################## #



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 161

def auth_token(args = { }, options = { })
  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'auth/api-token-auth',
      :http_method => :post,
      :default_parameter_send_in_value => :body,
      :parameters => [
        { :name => :client_code, :required => true, :default_value => client_code },
        { :name => :username, :required => true },
        { :name => :password, :required => true }
      ]
    }.merge(options)
  )
  _initial_http_host_address = http_client.http_host_address
  client_code = _request.arguments[:client_code]
  _http_host_address = "#{client_code}.wiredrive.com"

  http_client.http_host_address = _http_host_address
  _response = process_request(_request, options)
  http_client.http_host_address = _initial_http_host_address
  _token = http_client.response['Authorization']
  _token || _response
end

#auth_token=(value) ⇒ Object



154
155
156
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 154

def auth_token=(value)
  http_client.auth_token = value
end

#errorObject



130
131
132
133
134
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 130

def error
  _error = errors.first
  return _error.inspect if _error.is_a?(Hash)
  _error
end

#error_messageObject



136
137
138
139
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 136

def error_message
  _error = error
  _error.is_a?(Hash) ? (_error['message'] || _error) : _error
end

#errorsObject



141
142
143
144
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 141

def errors
  return (response['errors'] || [ ]) if response.is_a?(Hash)
  [ ]
end

#http(method, *args) ⇒ Object

Exposes HTTP Methods

Examples:

http(:get, ‘/’)




113
114
115
116
117
118
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 113

def http(method, *args)
  @request = nil
  @response = http_client.send(method, *args)
  @request = http_client.request
  response
end

#initialize_http_client(args = { }) ⇒ Object



69
70
71
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 69

def initialize_http_client(args = { })
  @http_client = Ubiquity::Wiredrive::API::V3::HTTPClient.new(args)
end

#initialize_logger(args = { }) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 59

def initialize_logger(args = { })
  @logger = args[:logger] ||= Logger.new(args[:log_to] || STDOUT)
  log_level = args[:log_level]
  if log_level
    @logger.level = log_level
    args[:logger] = @logger
  end
  @logger
end

#invitations_get(args = { }, options = { }) ⇒ Object



186
187
188
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 186

def invitations_get(args = { }, options = { })

end

#login_and_set_token(args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 47

def (args)
  _credentials = args[:credentials] || args
  _client_code = _credentials[:client_code]
  _username = _credentials[:username]
  _password = _credentials[:password]
  if _client_code && _username && _password
    token_data = auth_token(_credentials)
    self.auth_token = token_data
  end
  token_data
end

#presentation_authorize_get(args = { }, options = { }) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 190

def presentation_authorize_get(args = { }, options = { })
  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'presentation-orch-api/api/authorize/#{path_arguments[:invite_token]}',
      :default_parameter_send_in_value => :path,
      :parameters => [
        { :name => :invite_token, :aliases => [ :token ], :required => true },
        { :name => :password, :required => true, :send_in => :query }
      ]
    }.merge(options)
  )
  _response = process_request(_request, options)
  _token = http_client.response['Authorization']
  _token || _response
end

#presentation_get(args = { }, options = { }) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 207

def presentation_get(args = { }, options = { })
  _request = Requests::BaseRequest.new(
    args,
    {
      :http_path => 'presentation-orch-api/api/presentations/#{path_arguments[:presentation_id]}',
      :default_parameter_send_in_value => :path,
      :parameters => [
        { :name => :presentation_id, :aliases => [ :id ], :required => true }
      ]
    }.merge(options)
  )
  process_request(_request, options)
end

#process_request(request, options = nil) ⇒ Object

Parameters:

Options Hash (options):

  • :execute_request (Boolean) — default: true

    Will execute the request

  • :return_request (Boolean) — default: true

    Will return the request instance instead of nil. Only applies if execute_request is false.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 78

def process_request(request, options = nil)
  # @paginator = nil
  @response = nil
  @request = request
  logger.warn { "Request is Missing Required Arguments: #{request.missing_required_arguments.inspect}" } unless request.missing_required_arguments.empty?

  # if ([:all, 'all'].include?(request.arguments[:_page]))
  #   request.arguments[:_page] = 1
  #   include_remaining_pages = true
  # else
  #   include_remaining_pages = false
  # end

  request.client = self unless request.client
  options ||= request.options

  return (options.fetch(:return_request, true) ? request : nil) unless options.fetch(:execute_request, true)

  #@response = http_client.call_method(request.http_method, { :path => request.path, :query => request.query, :body => request.body }, options)
  @response = request.execute

  # if include_remaining_pages
  #   return paginator.include_remaining_pages
  # end

  if @response.is_a?(Hash)
    _results = @response['results']
    return _results if _results
  end

  @response
end

#process_request_using_class(request_class, args = { }, options = { }) ⇒ Object

def paginator

@paginator ||= Paginator.new(self) if @response

end



124
125
126
127
128
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 124

def process_request_using_class(request_class, args = { }, options = { })
  @response = nil
  @request = request_class.new(args, options)
  process_request(request, options)
end

#success?Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 146

def success?
  return unless request
  return request.success? if request.respond_to?(:success?)

  _code = http_client.response.code
  _code and _code.start_with?('2')
end

#token_cache_read(args) ⇒ Object



31
32
33
34
35
36
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 31

def token_cache_read(args)
  _token_path = args[:token_cache_path] || '_token.yaml'
  _token_path = File.expand_path(_token_path)
  _token = File.exists?(_token_path) ? YAML.load_file(_token_path) : nil
  _token
end

#token_cache_write(args) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 38

def token_cache_write(args)
  _token = args[:auth_token]
  return unless _token
  _token_path = args[:token_cache_path] || '_token.yaml'
  _token_path = File.expand_path(_token_path)
  logger.debug { "Writing Token Cache - '#{_token_path}'" }
  File.open(_token_path, 'w') { |f| f.write(YAML.dump(_token)) }
end

#users_get(args = { }, options = { }) ⇒ Object



221
222
223
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 221

def users_get(args = { }, options = { })
  http(:get, 'user-orch-api/api/users')
end

#users_projects_get(args = { }, options = { }) ⇒ Object



225
226
227
# File 'lib/ubiquity/wiredrive/api/v3/client.rb', line 225

def users_projects_get(args = { }, options = { })
  http(:get, 'user-orch-api/api/users/projects')
end