leaseweb-rest-api 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/leaseweb-rest-api.gemspec +1 -1
- data/lib/leaseweb-rest-api.rb +40 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b83be5bd2fd6f52f68ffdbc06bf81eb2ceaa6cc
|
4
|
+
data.tar.gz: ca30de4fae4eb69c21be20332ef038717d9acbe7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9800eb6c4b1b66ad08873f8b0173f12005e18c93a8f3d888c61a10433826da27356d2cf327195278c64293729149397ec5196f20d2d466a8b5de0a28677eeba
|
7
|
+
data.tar.gz: 74965ed037312892f4759771496c51a7906968c56f9f7bb7eb9f7bfd0db2cede3524ee2fbb3bf07f934d522e896787e18062f30e8db3166662a748d0b6513b52
|
data/leaseweb-rest-api.gemspec
CHANGED
data/lib/leaseweb-rest-api.rb
CHANGED
@@ -13,24 +13,59 @@ class LeasewebAPI
|
|
13
13
|
|
14
14
|
base_uri 'https://api.leaseweb.com'
|
15
15
|
|
16
|
+
def initialize
|
17
|
+
@tmpdir = "/tmp/lsw-rest-api"
|
18
|
+
Dir.mkdir(@tmpdir) unless Dir.exist?(@tmpdir)
|
19
|
+
end
|
20
|
+
|
16
21
|
def apiKeyAuth(apikey)
|
17
22
|
@options = { headers: { 'X-Lsw-Auth' => apikey, 'Content-Type' => 'application/json' } }
|
18
23
|
end
|
19
24
|
|
20
|
-
def getOauthToken(
|
21
|
-
|
22
|
-
|
25
|
+
def getOauthToken(client_id, client_secret)
|
26
|
+
access_token = validate_token(client_id)
|
27
|
+
|
28
|
+
if (access_token == false)
|
29
|
+
response = self.class.post('https://auth.leaseweb.com/token', basic_auth: { username: client_id, password: client_secret }, body: { grant_type: 'client_credentials' })
|
30
|
+
access_token = response.parsed_response['access_token']
|
31
|
+
cache_token(client_id, access_token, response.parsed_response['expires_in'])
|
32
|
+
end
|
23
33
|
|
24
34
|
@options = { headers: { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' } }
|
25
35
|
end
|
26
36
|
|
27
37
|
def getPasswordToken(username, password, client_id, client_secret)
|
28
|
-
|
29
|
-
|
38
|
+
access_token = validate_token(client_id)
|
39
|
+
|
40
|
+
if (access_token == false)
|
41
|
+
response = self.class.post('https://auth.leaseweb.com/token', basic_auth: { username: client_id, password: client_secret }, body: { grant_type: 'password', username: username, password: password })
|
42
|
+
access_token = response.parsed_response['access_token']
|
43
|
+
cache_token(client_id, access_token, response.parsed_response['expires_in'])
|
44
|
+
end
|
30
45
|
|
31
46
|
@options = { headers: { 'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json' } }
|
32
47
|
end
|
33
48
|
|
49
|
+
def validate_token(client_id)
|
50
|
+
file = "#{@tmpdir}/#{client_id}.json"
|
51
|
+
content = JSON.parse(File.read(file))
|
52
|
+
expires_at = DateTime.parse(content['expires_at'])
|
53
|
+
|
54
|
+
if expires_at > DateTime.now
|
55
|
+
return content['access_token']
|
56
|
+
else
|
57
|
+
File.delete(file)
|
58
|
+
end
|
59
|
+
|
60
|
+
return false
|
61
|
+
end
|
62
|
+
|
63
|
+
def cache_token(client_id, access_token, expires_in)
|
64
|
+
file = "#{@tmpdir}/#{client_id}.json"
|
65
|
+
content = { access_token: access_token, expires_at: Time.now.getutc + expires_in }.to_json
|
66
|
+
File.write(file, content)
|
67
|
+
end
|
68
|
+
|
34
69
|
def readPrivateKey(privateKey, password)
|
35
70
|
@private_key = OpenSSL::PKey::RSA.new(File.read(privateKey), password)
|
36
71
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: leaseweb-rest-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arnoud Vermeer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|