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
50
# 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
  @tenant_id = nil

	@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)


121
122
123
# File 'lib/upwork/api/client.rb', line 121

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

#full_url(uri) ⇒ Object

Get full URL



126
127
128
# File 'lib/upwork/api/client.rb', line 126

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)


94
95
96
# File 'lib/upwork/api/client.rb', line 94

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

#get_access_token(authz_code = nil) ⇒ Object

Finish auth process and get access token

Arguments:

authz_code: (String)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/upwork/api/client.rb', line 63

def get_access_token(authz_code = nil)
  if @config.grant_type == 'client_credentials'
    $LOG.i "getting access token"
	  @access_token = @oauth2_client.client_credentials.get_token
    $LOG.i "got access token", @access_token
  else
    $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
  end

	refresh_config_from_access_token

	@access_token
end

#get_actual_configObject

Get actual client config



80
81
82
# File 'lib/upwork/api/client.rb', line 80

def get_actual_config
  @config
end

#get_authorization_urlObject

Start auth process and get authorization URL



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

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



131
132
133
# File 'lib/upwork/api/client.rb', line 131

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

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

Run POST request

Arguments:

uri: (String)
param: (Hash)


103
104
105
# File 'lib/upwork/api/client.rb', line 103

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

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

Run PUT request

Arguments:

uri: (String)
param: (Hash)


112
113
114
# File 'lib/upwork/api/client.rb', line 112

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

#set_org_uid_header(tenant_id) ⇒ Object

Configure X-Upwork-API-TenantId header



85
86
87
# File 'lib/upwork/api/client.rb', line 85

def set_org_uid_header(tenant_id)
  @tenant_id = tenant_id
end