Class: Upwork::Api::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/upwork/api/client.rb

Overview

Client for accessing API

Constant Summary collapse

DATA_FORMAT =
'json'
OVERLOAD_VAR =
"http_method"
URI_AUTH =
"/ab/account-security/oauth2/authorize"
URI_RTOKEN =

refresh

"/api/v3/oauth2/token"
URI_ATOKEN =

access

"/api/v3/oauth2/token"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Init client

Arguments:

config: (Config)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/upwork/api/client.rb', line 35

def initialize(config)
  $LOG.i('initializing client')
  @config = config
  @epoint = Upwork::Api::DEFAULT_EPOINT
  @url_auth, @url_rtoken, @url_atoken = URI_AUTH, URI_RTOKEN, URI_ATOKEN

  @oauth2_client = OAuth2::Client.new(
    @config.client_id,
    @config.client_secret,
    :site => Upwork::Api::BASE_HOST,
    :authorize_url => @url_auth,
    :token_url => @url_atoken,
    :connection_opts => { :headers => {'User-Agent' => 'Github Upwork API Ruby Client' }}
  )
end

Instance Attribute Details

#epointObject

Returns the value of attribute epoint.



28
29
30
# File 'lib/upwork/api/client.rb', line 28

def epoint
  @epoint
end

#url_atokenObject (readonly)

Returns the value of attribute url_atoken.



29
30
31
# File 'lib/upwork/api/client.rb', line 29

def url_atoken
  @url_atoken
end

#url_authObject (readonly)

Returns the value of attribute url_auth.



29
30
31
# File 'lib/upwork/api/client.rb', line 29

def url_auth
  @url_auth
end

#url_rtokenObject (readonly)

Returns the value of attribute url_rtoken.



29
30
31
# File 'lib/upwork/api/client.rb', line 29

def url_rtoken
  @url_rtoken
end

Instance Method Details

#delete(uri, params = {}) ⇒ Object

Run DELETE request

Arguments:

uri: (String)
param: (Hash)


109
110
111
# File 'lib/upwork/api/client.rb', line 109

def delete(uri, params = {})
  send_request(uri, :delete, params)
end

#full_url(uri) ⇒ Object

Get full URL



114
115
116
# File 'lib/upwork/api/client.rb', line 114

def full_url(uri) # :nodoc:
  Upwork::Api::BASE_HOST + '/' + get_uri_with_format(uri);
end

#get(uri, params = {}) ⇒ Object

Run GET request

Arguments:

uri: (String)
param: (Hash)


82
83
84
# File 'lib/upwork/api/client.rb', line 82

def get(uri, params = {})
  send_request(uri, :get, params)
end

#get_access_token(authz_code) ⇒ Object

Finish auth process and get access token

Arguments:

authz_code: (String)


62
63
64
65
66
67
68
69
70
# File 'lib/upwork/api/client.rb', line 62

def get_access_token(authz_code)
  $LOG.i "getting access and refresh token pair"
  @access_token = @oauth2_client.auth_code.get_token(authz_code, :redirect_uri => @config.redirect_uri)
  $LOG.i "got access and refresh token pair", @access_token

  refresh_config_from_access_token

  @access_token
end

#get_actual_configObject

Get actual client config



73
74
75
# File 'lib/upwork/api/client.rb', line 73

def get_actual_config
  @config
end

#get_authorization_urlObject

Start auth process and get authorization URL



52
53
54
55
56
# File 'lib/upwork/api/client.rb', line 52

def get_authorization_url
  $LOG.i "requesting authorization URL"
 
  @oauth2_client.auth_code.authorize_url(:redirect_uri => @config.redirect_uri)
end

#get_uri_with_format(uri) ⇒ Object

Get URI with :format



119
120
121
# File 'lib/upwork/api/client.rb', line 119

def get_uri_with_format(uri) # :nodoc:
  (@epoint) + uri + ((@epoint == 'api') ? '.' + DATA_FORMAT : '')
end

#post(uri, params = {}) ⇒ Object

Run POST request

Arguments:

uri: (String)
param: (Hash)


91
92
93
# File 'lib/upwork/api/client.rb', line 91

def post(uri, params = {})
  send_request(uri, :post, params)
end

#put(uri, params = {}) ⇒ Object

Run PUT request

Arguments:

uri: (String)
param: (Hash)


100
101
102
# File 'lib/upwork/api/client.rb', line 100

def put(uri, params = {})
  send_request(uri, :put, params)
end