scalingo 3.5.0 → 4.0.beta2

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.
Files changed (243) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +1 -1
  3. data/.github/workflows/ruby.yml +1 -1
  4. data/.rubocop.yml +40 -0
  5. data/CHANGELOG.md +22 -1
  6. data/Gemfile +2 -0
  7. data/README.md +13 -62
  8. data/bin/console +11 -1
  9. data/bin/lint +2 -0
  10. data/bin/setup +21 -6
  11. data/bin/specs +2 -0
  12. data/lib/scalingo/api/client.rb +21 -39
  13. data/lib/scalingo/api/endpoint.rb +88 -11
  14. data/lib/scalingo/auth/keys.rb +4 -50
  15. data/lib/scalingo/auth/scm_integrations.rb +4 -51
  16. data/lib/scalingo/auth/tokens.rb +5 -72
  17. data/lib/scalingo/auth/two_factor_auth.rb +4 -55
  18. data/lib/scalingo/auth/user.rb +3 -38
  19. data/lib/scalingo/bearer_token.rb +16 -9
  20. data/lib/scalingo/billing/profile.rb +3 -40
  21. data/lib/scalingo/client.rb +21 -31
  22. data/lib/scalingo/configuration.rb +0 -24
  23. data/lib/scalingo/core_client.rb +9 -29
  24. data/lib/scalingo/database/backups.rb +9 -0
  25. data/lib/scalingo/database/databases.rb +8 -0
  26. data/lib/scalingo/{regional_database.rb → database.rb} +3 -3
  27. data/lib/scalingo/faraday/extract_meta.rb +33 -0
  28. data/lib/scalingo/faraday/extract_root_value.rb +18 -0
  29. data/lib/scalingo/faraday/response.rb +36 -0
  30. data/lib/scalingo/regional/addons.rb +18 -130
  31. data/lib/scalingo/regional/apps.rb +10 -103
  32. data/lib/scalingo/regional/autoscalers.rb +5 -64
  33. data/lib/scalingo/regional/collaborators.rb +4 -51
  34. data/lib/scalingo/regional/containers.rb +4 -51
  35. data/lib/scalingo/regional/deployments.rb +3 -38
  36. data/lib/scalingo/regional/domains.rb +5 -64
  37. data/lib/scalingo/regional/environment.rb +6 -77
  38. data/lib/scalingo/regional/events.rb +5 -50
  39. data/lib/scalingo/regional/logs.rb +10 -28
  40. data/lib/scalingo/regional/metrics.rb +2 -34
  41. data/lib/scalingo/regional/notifiers.rb +7 -90
  42. data/lib/scalingo/regional/operations.rb +5 -18
  43. data/lib/scalingo/regional/scm_repo_links.rb +8 -103
  44. data/lib/scalingo/token_holder.rb +1 -46
  45. data/lib/scalingo/version.rb +1 -1
  46. data/scalingo.gemspec +4 -1
  47. metadata +61 -205
  48. data/lib/scalingo/api/response.rb +0 -69
  49. data/lib/scalingo/regional_database/backups.rb +0 -44
  50. data/lib/scalingo/regional_database/databases.rb +0 -31
  51. data/samples/auth/keys/_meta.json +0 -13
  52. data/samples/auth/keys/all-200.json +0 -62
  53. data/samples/auth/keys/create-201.json +0 -67
  54. data/samples/auth/keys/create-422.json +0 -34
  55. data/samples/auth/keys/destroy-204.json +0 -19
  56. data/samples/auth/keys/destroy-404.json +0 -19
  57. data/samples/auth/keys/show-200.json +0 -60
  58. data/samples/auth/keys/show-404.json +0 -19
  59. data/samples/auth/scm_integrations/_meta.json +0 -14
  60. data/samples/auth/scm_integrations/all-200.json +0 -41
  61. data/samples/auth/scm_integrations/create-201.json +0 -41
  62. data/samples/auth/scm_integrations/create-422.json +0 -36
  63. data/samples/auth/scm_integrations/destroy-204.json +0 -15
  64. data/samples/auth/scm_integrations/destroy-404.json +0 -23
  65. data/samples/auth/scm_integrations/show-200.json +0 -34
  66. data/samples/auth/scm_integrations/show-404.json +0 -23
  67. data/samples/auth/tokens/_meta.json +0 -13
  68. data/samples/auth/tokens/all-200.json +0 -32
  69. data/samples/auth/tokens/create-201.json +0 -37
  70. data/samples/auth/tokens/destroy-204.json +0 -19
  71. data/samples/auth/tokens/destroy-404.json +0 -19
  72. data/samples/auth/tokens/exchange-200.json +0 -25
  73. data/samples/auth/tokens/exchange-401.json +0 -24
  74. data/samples/auth/tokens/renew-200.json +0 -32
  75. data/samples/auth/tokens/renew-404.json +0 -20
  76. data/samples/auth/two_factor_auth/_meta.json +0 -10
  77. data/samples/auth/two_factor_auth/disable-not-initiated.json +0 -23
  78. data/samples/auth/two_factor_auth/disable-success.json +0 -29
  79. data/samples/auth/two_factor_auth/initiate-already-enabled.json +0 -29
  80. data/samples/auth/two_factor_auth/initiate-success.json +0 -36
  81. data/samples/auth/two_factor_auth/initiate-wrong-provider.json +0 -29
  82. data/samples/auth/two_factor_auth/status.json +0 -29
  83. data/samples/auth/two_factor_auth/validate-not-initiated.json +0 -29
  84. data/samples/auth/two_factor_auth/validate-success.json +0 -49
  85. data/samples/auth/two_factor_auth/validate-wrong.json +0 -29
  86. data/samples/auth/user/_meta.json +0 -10
  87. data/samples/auth/user/self.json +0 -54
  88. data/samples/auth/user/stop-free-trial.json +0 -24
  89. data/samples/auth/user/update-200.json +0 -59
  90. data/samples/auth/user/update-422.json +0 -33
  91. data/samples/billing/profile/_meta.json +0 -23
  92. data/samples/billing/profile/create-201.json +0 -50
  93. data/samples/billing/profile/create-400.json +0 -27
  94. data/samples/billing/profile/create-422.json +0 -44
  95. data/samples/billing/profile/show-200.json +0 -41
  96. data/samples/billing/profile/show-404.json +0 -22
  97. data/samples/billing/profile/update-200.json +0 -47
  98. data/samples/billing/profile/update-422.json +0 -32
  99. data/samples/regional/addons/_meta.json +0 -22
  100. data/samples/regional/addons/categories-guest.json +0 -36
  101. data/samples/regional/addons/categories-logged.json +0 -37
  102. data/samples/regional/addons/destroy-204.json +0 -19
  103. data/samples/regional/addons/destroy-404.json +0 -24
  104. data/samples/regional/addons/find-200.json +0 -48
  105. data/samples/regional/addons/find-404.json +0 -24
  106. data/samples/regional/addons/for-200.json +0 -50
  107. data/samples/regional/addons/providers-guest.json +0 -588
  108. data/samples/regional/addons/providers-logged.json +0 -705
  109. data/samples/regional/addons/provision-201.json +0 -58
  110. data/samples/regional/addons/provision-400.json +0 -29
  111. data/samples/regional/addons/sso-200.json +0 -49
  112. data/samples/regional/addons/sso-404.json +0 -24
  113. data/samples/regional/addons/token-200.json +0 -49
  114. data/samples/regional/addons/token-404.json +0 -24
  115. data/samples/regional/addons/update-200.json +0 -58
  116. data/samples/regional/addons/update-404.json +0 -30
  117. data/samples/regional/apps/_meta.json +0 -52
  118. data/samples/regional/apps/all.json +0 -99
  119. data/samples/regional/apps/create-201.json +0 -66
  120. data/samples/regional/apps/create-422.json +0 -34
  121. data/samples/regional/apps/destroy-204.json +0 -19
  122. data/samples/regional/apps/destroy-404.json +0 -24
  123. data/samples/regional/apps/destroy-422.json +0 -27
  124. data/samples/regional/apps/find-200.json +0 -60
  125. data/samples/regional/apps/find-404.json +0 -24
  126. data/samples/regional/apps/logs_url.json +0 -62
  127. data/samples/regional/apps/rename-200.json +0 -65
  128. data/samples/regional/apps/rename-404.json +0 -29
  129. data/samples/regional/apps/rename-422.json +0 -33
  130. data/samples/regional/apps/transfer-200.json +0 -65
  131. data/samples/regional/apps/transfer-404.json +0 -27
  132. data/samples/regional/apps/transfer-422.json +0 -34
  133. data/samples/regional/apps/update-200.json +0 -66
  134. data/samples/regional/apps/update-stack-404.json +0 -30
  135. data/samples/regional/autoscalers/_meta.json +0 -27
  136. data/samples/regional/autoscalers/create-201.json +0 -49
  137. data/samples/regional/autoscalers/create-500.json +0 -32
  138. data/samples/regional/autoscalers/destroy-204.json +0 -20
  139. data/samples/regional/autoscalers/destroy-404.json +0 -25
  140. data/samples/regional/autoscalers/find-200.json +0 -39
  141. data/samples/regional/autoscalers/find-404.json +0 -25
  142. data/samples/regional/autoscalers/for-200.json +0 -41
  143. data/samples/regional/autoscalers/update-200.json +0 -45
  144. data/samples/regional/autoscalers/update-404.json +0 -31
  145. data/samples/regional/autoscalers/update-500.json +0 -30
  146. data/samples/regional/collaborators/_meta.json +0 -17
  147. data/samples/regional/collaborators/accept-200.json +0 -60
  148. data/samples/regional/collaborators/accept-400.json +0 -24
  149. data/samples/regional/collaborators/accept-404.json +0 -24
  150. data/samples/regional/collaborators/destroy-204.json +0 -19
  151. data/samples/regional/collaborators/destroy-404.json +0 -24
  152. data/samples/regional/collaborators/for-200.json +0 -34
  153. data/samples/regional/collaborators/invite-201.json +0 -37
  154. data/samples/regional/collaborators/invite-422.json +0 -34
  155. data/samples/regional/containers/_meta.json +0 -25
  156. data/samples/regional/containers/for-200.json +0 -39
  157. data/samples/regional/containers/restart-202.json +0 -28
  158. data/samples/regional/containers/restart-422.json +0 -33
  159. data/samples/regional/containers/scale-202.json +0 -48
  160. data/samples/regional/containers/scale-422.json +0 -36
  161. data/samples/regional/containers/sizes-guest.json +0 -115
  162. data/samples/regional/containers/sizes-logged.json +0 -116
  163. data/samples/regional/deployments/_meta.json +0 -8
  164. data/samples/regional/deployments/find-200.json +0 -45
  165. data/samples/regional/deployments/find-404.json +0 -24
  166. data/samples/regional/deployments/for-with-paging.json +0 -35
  167. data/samples/regional/deployments/for-without-pages.json +0 -56
  168. data/samples/regional/deployments/logs-200.json +0 -22
  169. data/samples/regional/deployments/logs-404.json +0 -24
  170. data/samples/regional/domains/_meta.json +0 -21
  171. data/samples/regional/domains/create-201.json +0 -44
  172. data/samples/regional/domains/create-422.json +0 -33
  173. data/samples/regional/domains/destroy-204.json +0 -19
  174. data/samples/regional/domains/destroy-404.json +0 -24
  175. data/samples/regional/domains/find-200.json +0 -38
  176. data/samples/regional/domains/find-404.json +0 -24
  177. data/samples/regional/domains/for-200.json +0 -40
  178. data/samples/regional/domains/update-200.json +0 -44
  179. data/samples/regional/domains/update-404.json +0 -30
  180. data/samples/regional/domains/update-422.json +0 -33
  181. data/samples/regional/environment/_meta.json +0 -43
  182. data/samples/regional/environment/bulk-destroy-204.json +0 -19
  183. data/samples/regional/environment/bulk-update-200.json +0 -70
  184. data/samples/regional/environment/create-201.json +0 -36
  185. data/samples/regional/environment/create-422.json +0 -37
  186. data/samples/regional/environment/destroy-204.json +0 -19
  187. data/samples/regional/environment/destroy-404.json +0 -24
  188. data/samples/regional/environment/for-200.json +0 -31
  189. data/samples/regional/environment/update-200.json +0 -35
  190. data/samples/regional/environment/update-404.json +0 -30
  191. data/samples/regional/events/_meta.json +0 -3
  192. data/samples/regional/events/all-200.json +0 -619
  193. data/samples/regional/events/categories-guest.json +0 -66
  194. data/samples/regional/events/categories-logged.json +0 -67
  195. data/samples/regional/events/for-200.json +0 -404
  196. data/samples/regional/events/types-guest.json +0 -288
  197. data/samples/regional/events/types-logged.json +0 -289
  198. data/samples/regional/logs/_meta.json +0 -11
  199. data/samples/regional/logs/archives-200.json +0 -28
  200. data/samples/regional/logs/get-guest-200.json +0 -18
  201. data/samples/regional/logs/get-logged-200.json +0 -19
  202. data/samples/regional/logs/get-with-limit-200.json +0 -18
  203. data/samples/regional/metrics/_meta.json +0 -15
  204. data/samples/regional/metrics/for-invalid-400.json +0 -23
  205. data/samples/regional/metrics/for-valid-cpu-200.json +0 -747
  206. data/samples/regional/metrics/for-valid-router-404.json +0 -23
  207. data/samples/regional/metrics/types-guest.json +0 -66
  208. data/samples/regional/metrics/types-logged.json +0 -67
  209. data/samples/regional/notifiers/_meta.json +0 -23
  210. data/samples/regional/notifiers/create-201.json +0 -55
  211. data/samples/regional/notifiers/create-404.json +0 -30
  212. data/samples/regional/notifiers/create-422.json +0 -36
  213. data/samples/regional/notifiers/destroy-204.json +0 -19
  214. data/samples/regional/notifiers/destroy-404.json +0 -24
  215. data/samples/regional/notifiers/find-200.json +0 -47
  216. data/samples/regional/notifiers/find-404.json +0 -24
  217. data/samples/regional/notifiers/for-200.json +0 -49
  218. data/samples/regional/notifiers/platforms-guest.json +0 -184
  219. data/samples/regional/notifiers/platforms-logged.json +0 -185
  220. data/samples/regional/notifiers/test-200.json +0 -22
  221. data/samples/regional/notifiers/test-404.json +0 -25
  222. data/samples/regional/notifiers/update-200.json +0 -53
  223. data/samples/regional/operations/_meta.json +0 -5
  224. data/samples/regional/operations/find-200.json +0 -31
  225. data/samples/regional/operations/find-404.json +0 -24
  226. data/samples/regional/scm_repo_links/_meta.json +0 -22
  227. data/samples/regional/scm_repo_links/create-201.json +0 -54
  228. data/samples/regional/scm_repo_links/destroy-204.json +0 -15
  229. data/samples/regional/scm_repo_links/manual-deploy-200.json +0 -32
  230. data/samples/regional/scm_repo_links/show-200.json +0 -43
  231. data/samples/regional/scm_repo_links/update-200.json +0 -50
  232. data/samples/regional_database/backups/_meta.json +0 -4
  233. data/samples/regional_database/backups/archive-200.json +0 -24
  234. data/samples/regional_database/backups/archive-400.json +0 -24
  235. data/samples/regional_database/backups/create-201.json +0 -32
  236. data/samples/regional_database/backups/create-400.json +0 -24
  237. data/samples/regional_database/backups/for-200.json +0 -52
  238. data/samples/regional_database/backups/for-400.json +0 -24
  239. data/samples/regional_database/databases/_meta.json +0 -3
  240. data/samples/regional_database/databases/find-200.json +0 -47
  241. data/samples/regional_database/databases/find-400.json +0 -24
  242. data/samples/regional_database/databases/upgrade-202.json +0 -39
  243. data/samples/regional_database/databases/upgrade-400.json +0 -24
@@ -2,56 +2,9 @@ require "scalingo/api/endpoint"
2
2
 
3
3
  module Scalingo
4
4
  class Auth::ScmIntegrations < API::Endpoint
5
- def all(headers = nil, &block)
6
- data = nil
7
-
8
- response = connection.get(
9
- "scm_integrations",
10
- data,
11
- headers,
12
- &block
13
- )
14
-
15
- unpack(:scm_integrations) { response }
16
- end
17
-
18
- def show(id, headers = nil, &block)
19
- data = nil
20
-
21
- response = connection.get(
22
- "scm_integrations/#{id}",
23
- data,
24
- headers,
25
- &block
26
- )
27
-
28
- unpack(:scm_integration) { response }
29
- end
30
-
31
- def create(payload, headers = nil, &block)
32
- data = {scm_integration: payload}
33
-
34
- response = connection.post(
35
- "scm_integrations",
36
- data,
37
- headers,
38
- &block
39
- )
40
-
41
- unpack(:scm_integration) { response }
42
- end
43
-
44
- def destroy(id, headers = nil, &block)
45
- data = nil
46
-
47
- response = connection.delete(
48
- "scm_integrations/#{id}",
49
- data,
50
- headers,
51
- &block
52
- )
53
-
54
- unpack { response }
55
- end
5
+ get :list, "scm_integrations"
6
+ get :find, "scm_integrations/{id}"
7
+ post :create, "scm_integrations", root_key: :scm_integration
8
+ delete :delete, "scm_integrations/{id}"
56
9
  end
57
10
  end
@@ -2,77 +2,10 @@ require "scalingo/api/endpoint"
2
2
 
3
3
  module Scalingo
4
4
  class Auth::Tokens < API::Endpoint
5
- def exchange(token, headers = nil, &block)
6
- data = nil
7
-
8
- authorization = Faraday::Utils.basic_header_from("", token)
9
-
10
- request_headers = {
11
- Faraday::Request::Authorization::KEY => authorization
12
- }
13
-
14
- request_headers.update(headers) if headers
15
-
16
- response = client.unauthenticated_connection.post(
17
- "tokens/exchange",
18
- data,
19
- request_headers,
20
- &block
21
- )
22
-
23
- unpack { response }
24
- end
25
-
26
- def all(headers = nil, &block)
27
- data = nil
28
-
29
- response = connection.get(
30
- "tokens",
31
- data,
32
- headers,
33
- &block
34
- )
35
-
36
- unpack(:tokens) { response }
37
- end
38
-
39
- def create(payload, headers = nil, &block)
40
- data = {token: payload}
41
-
42
- response = connection.post(
43
- "tokens",
44
- data,
45
- headers,
46
- &block
47
- )
48
-
49
- unpack(:token) { response }
50
- end
51
-
52
- def renew(id, headers = nil, &block)
53
- data = nil
54
-
55
- response = connection.patch(
56
- "tokens/#{id}/renew",
57
- data,
58
- headers,
59
- &block
60
- )
61
-
62
- unpack(:token) { response }
63
- end
64
-
65
- def destroy(id, headers = nil, &block)
66
- data = nil
67
-
68
- response = connection.delete(
69
- "tokens/#{id}",
70
- data,
71
- headers,
72
- &block
73
- )
74
-
75
- unpack { response }
76
- end
5
+ get :list, "tokens"
6
+ post :create, "tokens", root_key: :token
7
+ delete :delete, "tokens/{id}"
8
+ post :exchange, "tokens/exchange", connected: false
9
+ put :renew, "tokens/{id}/renew"
77
10
  end
78
11
  end
@@ -2,60 +2,9 @@ require "scalingo/api/endpoint"
2
2
 
3
3
  module Scalingo
4
4
  class Auth::TwoFactorAuth < API::Endpoint
5
- TOTP_PROVIDER = "totp"
6
- DEFAULT_PROVIDER = TOTP_PROVIDER
7
- SUPPORTED_PROVIDERS = [TOTP_PROVIDER]
8
-
9
- def status(headers = nil, &block)
10
- data = nil
11
-
12
- response = connection.get(
13
- "client/tfa",
14
- data,
15
- headers,
16
- &block
17
- )
18
-
19
- unpack(:tfa) { response }
20
- end
21
-
22
- def initiate(provider = DEFAULT_PROVIDER, headers = nil, &block)
23
- data = {tfa: {provider: provider}}
24
-
25
- response = connection.post(
26
- "client/tfa",
27
- data,
28
- headers,
29
- &block
30
- )
31
-
32
- unpack(:tfa) { response }
33
- end
34
-
35
- def validate(attempt, headers = nil, &block)
36
- data = {tfa: {attempt: attempt}}
37
-
38
- response = connection.post(
39
- "client/tfa/validate",
40
- data,
41
- headers,
42
- &block
43
- )
44
-
45
- unpack(:tfa) { response }
46
- end
47
-
48
- def disable(headers = nil, &block)
49
- data = nil
50
-
51
- response = connection.delete(
52
- "client/tfa",
53
- data,
54
- headers,
55
- &block
56
- )
57
-
58
- unpack(:tfa) { response }
59
- end
5
+ get :status, "client/tfa"
6
+ post :initiate, "client/tfa", root_key: :tfa
7
+ post :validate, "client/tfa/validate", root_key: :tfa
8
+ delete :disable, "client/tfa"
60
9
  end
61
10
  end
@@ -2,43 +2,8 @@ require "scalingo/api/endpoint"
2
2
 
3
3
  module Scalingo
4
4
  class Auth::User < API::Endpoint
5
- def self(headers = nil, &block)
6
- data = nil
7
-
8
- response = connection.get(
9
- "users/self",
10
- data,
11
- headers,
12
- &block
13
- )
14
-
15
- unpack(:user) { response }
16
- end
17
-
18
- def update(payload, headers = nil, &block)
19
- data = {user: payload}
20
-
21
- response = connection.put(
22
- "users/account",
23
- data,
24
- headers,
25
- &block
26
- )
27
-
28
- unpack(:user) { response }
29
- end
30
-
31
- def stop_free_trial(headers = nil, &block)
32
- data = nil
33
-
34
- response = connection.post(
35
- "users/stop_free_trial",
36
- data,
37
- headers,
38
- &block
39
- )
40
-
41
- unpack { response }
42
- end
5
+ get :find, "users/self"
6
+ put :update, "users/account", root_key: :user
7
+ post :stop_free_trial, "users/stop_free_trial"
43
8
  end
44
9
  end
@@ -1,14 +1,16 @@
1
+ require "jwt"
2
+
1
3
  module Scalingo
2
4
  class BearerToken
3
5
  attr_reader :expires_at
4
- attr_writer :raise_on_expired
5
6
 
6
- def initialize(value, expires_at: nil, raise_on_expired: false)
7
+ def initialize(value)
7
8
  @value = value
8
- @expires_at = expires_at if expires_at
9
- @raise_on_expired = raise_on_expired
9
+
10
+ read_expiration!
10
11
  end
11
12
 
13
+ # :nocov:
12
14
  def inspect
13
15
  str = "<#{self.class}:0x#{object_id.to_s(16)} "
14
16
 
@@ -23,19 +25,24 @@ module Scalingo
23
25
  str << %(value:"#{value}">)
24
26
  str
25
27
  end
26
-
27
- def raise_on_expired?
28
- @raise_on_expired
29
- end
28
+ # :nocov:
30
29
 
31
30
  def expired?
32
31
  expires_at && expires_at <= Time.now
33
32
  end
34
33
 
35
34
  def value
36
- raise Error::ExpiredToken if expired? && raise_on_expired?
35
+ raise Error::ExpiredToken if expired?
37
36
 
38
37
  @value
39
38
  end
39
+
40
+ private
41
+
42
+ def read_expiration!
43
+ payload, _ = JWT.decode(@value, nil, false)
44
+
45
+ @expires_at = Time.at(payload["exp"]) if payload["exp"]
46
+ end
40
47
  end
41
48
  end
@@ -2,45 +2,8 @@ require "scalingo/api/endpoint"
2
2
 
3
3
  module Scalingo
4
4
  class Billing::Profile < API::Endpoint
5
- def show(headers = nil, &block)
6
- data = nil
7
-
8
- response = connection.get(
9
- "profile",
10
- data,
11
- headers,
12
- &block
13
- )
14
-
15
- unpack(:profile) { response }
16
- end
17
-
18
- def create(payload = {}, headers = nil, &block)
19
- data = {profile: payload}
20
-
21
- response = connection.post(
22
- "profiles",
23
- data,
24
- headers,
25
- &block
26
- )
27
-
28
- unpack(:profile) { response }
29
- end
30
-
31
- def update(id, payload = {}, headers = nil, &block)
32
- data = {profile: payload}
33
-
34
- response = connection.put(
35
- "profiles/#{id}",
36
- data,
37
- headers,
38
- &block
39
- )
40
-
41
- unpack(:profile) { response }
42
- end
43
-
44
- alias_method :self, :show
5
+ get :find, "profile"
6
+ post :create, "profiles", root_key: :profile
7
+ put :update, "profiles/{id}", root_key: :profile
45
8
  end
46
9
  end
@@ -2,53 +2,43 @@ require "scalingo/core_client"
2
2
  require "scalingo/auth"
3
3
  require "scalingo/billing"
4
4
  require "scalingo/regional"
5
- require "scalingo/regional_database"
5
+ require "scalingo/database"
6
6
 
7
7
  module Scalingo
8
8
  class Client < CoreClient
9
+ URLS = {
10
+ auth: "https://auth.scalingo.com/v1",
11
+ billing: "https://cashmachine.scalingo.com",
12
+ regional: {
13
+ osc_fr1: "https://api.osc-fr1.scalingo.com/v1",
14
+ osc_secnum_fr1: "https://api.osc-secnum-fr1.scalingo.com/v1"
15
+ },
16
+ database: {
17
+ osc_fr1: "https://db-api.osc-fr1.scalingo.com/api",
18
+ osc_secnum_fr1: "https://db-api.osc-secnum-fr1.scalingo.com/api"
19
+ }
20
+ }
21
+
9
22
  ## API clients
10
23
  def auth
11
- @auth ||= Auth.new(
12
- "https://auth.scalingo.com/v1",
13
- scalingo: self
14
- )
24
+ @auth ||= Auth.new(URLS[:auth], scalingo: self)
15
25
  end
16
26
 
17
27
  def billing
18
- @billing ||= Billing.new(
19
- "https://cashmachine.scalingo.com",
20
- scalingo: self
21
- )
28
+ @billing ||= Billing.new(URLS[:billing], scalingo: self)
22
29
  end
23
30
 
24
31
  def osc_fr1
25
- @osc_fr1 ||= Regional.new(
26
- "https://api.osc-fr1.scalingo.com/v1",
27
- scalingo: self
28
- )
32
+ @osc_fr1 ||= Regional.new(URLS[:regional][:osc_fr1], scalingo: self, region: :osc_fr1)
29
33
  end
30
- alias_method :apps_api_osc_fr1, :osc_fr1
31
34
 
32
35
  def osc_secnum_fr1
33
- @osc_secnum_fr1 ||= Regional.new(
34
- "https://api.osc-secnum-fr1.scalingo.com/v1",
35
- scalingo: self
36
- )
37
- end
38
- alias_method :apps_api_osc_secnum_fr1, :osc_secnum_fr1
39
-
40
- def db_api_osc_fr1
41
- @db_api_osc_fr1 ||= RegionalDatabase.new(
42
- "https://db-api.osc-fr1.scalingo.com/api",
43
- scalingo: self
44
- )
36
+ @osc_secnum_fr1 ||= Regional.new(URLS[:regional][:osc_secnum_fr1], scalingo: self, region: :osc_secnum_fr1)
45
37
  end
46
38
 
47
- def db_api_osc_secnum_fr1
48
- @db_api_osc_secnum_fr1 ||= RegionalDatabase.new(
49
- "https://db-api.osc-secnum-fr1.scalingo.com/api",
50
- scalingo: self
51
- )
39
+ ## Helpers
40
+ def self
41
+ auth.user.find
52
42
  end
53
43
  end
54
44
  end
@@ -1,37 +1,16 @@
1
1
  require "active_support"
2
2
  require "active_support/core_ext/numeric/time"
3
3
  require "scalingo/version"
4
- require "ostruct"
5
4
 
6
5
  module Scalingo
7
6
  class Configuration
8
7
  ATTRIBUTES = [
9
- # URL to the authentication API
10
- # :auth,
11
-
12
- # URL to the billing API
13
- # :billing,
14
-
15
- # List of regions under the form {"region_id": root_url}
16
- # :regions,
17
-
18
8
  # Default region. Must match a key in `regions`
19
9
  :default_region,
20
10
 
21
11
  # Configure the User Agent header
22
12
  :user_agent,
23
13
 
24
- # For how long is a bearer token considered valid (it will raise passed this delay).
25
- # Set to nil to never raise.
26
- :exchanged_token_validity,
27
-
28
- # Raise an exception when trying to use an authenticated connection without a bearer token set
29
- # Having this setting to true prevents performing requests that would fail due to lack of authentication headers.
30
- :raise_on_missing_authentication,
31
-
32
- # Raise an exception when the bearer token in use is supposed to be invalid
33
- :raise_on_expired_token,
34
-
35
14
  # These headers will be added to every request. Individual methods may override them.
36
15
  # This should be a hash or a callable object that returns a hash.
37
16
  :additional_headers,
@@ -46,9 +25,6 @@ module Scalingo
46
25
  new(
47
26
  default_region: :osc_fr1,
48
27
  user_agent: "Scalingo Ruby Client v#{Scalingo::VERSION}",
49
- exchanged_token_validity: 1.hour,
50
- raise_on_missing_authentication: true,
51
- raise_on_expired_token: false,
52
28
  additional_headers: {}
53
29
  )
54
30
  end
@@ -14,6 +14,7 @@ module Scalingo
14
14
  @config = Configuration.new(attributes, Scalingo.config)
15
15
  end
16
16
 
17
+ # :nocov:
17
18
  def inspect
18
19
  str = %(<#{self.class}:0x#{object_id.to_s(16)} version:"#{Scalingo::VERSION}" authenticated:)
19
20
 
@@ -26,6 +27,7 @@ module Scalingo
26
27
  str << ">"
27
28
  str
28
29
  end
30
+ # :nocov:
29
31
 
30
32
  ## Sub-clients accessors
31
33
  def auth
@@ -40,12 +42,8 @@ module Scalingo
40
42
  public_send(name || config.default_region)
41
43
  end
42
44
 
43
- def database_region(name = nil)
44
- public_send(name || "db_api_#{config.default_region}")
45
- end
46
-
47
45
  ## Authentication helpers / Token management
48
- def authenticate_with(access_token: nil, bearer_token: nil, expires_at: nil)
46
+ def authenticate_with(access_token: nil, bearer_token: nil)
49
47
  if !access_token && !bearer_token
50
48
  raise ArgumentError, "You must supply one of `access_token` or `bearer_token`"
51
49
  end
@@ -54,31 +52,16 @@ module Scalingo
54
52
  raise ArgumentError, "You cannot supply both `access_token` and `bearer_token`"
55
53
  end
56
54
 
57
- if expires_at && !bearer_token
58
- raise ArgumentError, "`expires_at` can only be used with `bearer_token`. `access_token` have a 1 hour expiration."
59
- end
60
-
61
55
  if access_token
62
- expiration = Time.now + config.exchanged_token_validity
63
- response = auth.tokens.exchange(access_token)
64
-
65
- if response.successful?
66
- self.token = BearerToken.new(
67
- response.data[:token],
68
- expires_at: expiration,
69
- raise_on_expired: config.raise_on_expired_token
70
- )
71
- end
72
-
73
- return response.successful?
56
+ response = auth.tokens.exchange(basic: {password: access_token})
57
+
58
+ self.token = response.body if response.success?
59
+
60
+ return response.success?
74
61
  end
75
62
 
76
63
  if bearer_token
77
- authenticate_with_bearer_token(
78
- bearer_token,
79
- expires_at: expires_at,
80
- raise_on_expired_token: config.raise_on_expired_token
81
- )
64
+ self.token = bearer_token
82
65
 
83
66
  true
84
67
  end
@@ -105,8 +88,5 @@ module Scalingo
105
88
  def_delegator :region, :notifiers
106
89
  def_delegator :region, :operations
107
90
  def_delegator :region, :scm_repo_links
108
-
109
- def_delegator :database_region, :databases
110
- def_delegator :database_region, :backups
111
91
  end
112
92
  end
@@ -0,0 +1,9 @@
1
+ require "scalingo/api/endpoint"
2
+
3
+ module Scalingo
4
+ class Database::Backups < API::Endpoint
5
+ get :list, "databases/{addon_id}/backups"
6
+ post :create, "databases/{addon_id}/backups"
7
+ get :archive, "databases/{addon_id}/backups/{id}/archive"
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ require "scalingo/api/endpoint"
2
+
3
+ module Scalingo
4
+ class Database::Databases < API::Endpoint
5
+ get :find, "databases/{id}"
6
+ post :upgrade, "databases/{id}/upgrade"
7
+ end
8
+ end
@@ -1,9 +1,9 @@
1
1
  require "scalingo/api/client"
2
2
 
3
3
  module Scalingo
4
- class RegionalDatabase < API::Client
5
- require "scalingo/regional_database/databases"
6
- require "scalingo/regional_database/backups"
4
+ class Database < API::Client
5
+ require "scalingo/database/databases"
6
+ require "scalingo/database/backups"
7
7
 
8
8
  register_handlers!(
9
9
  databases: Databases,
@@ -0,0 +1,33 @@
1
+ require "faraday"
2
+
3
+ module Scalingo
4
+ class ExtractMeta < Faraday::Middleware
5
+ def on_complete(env)
6
+ return unless env.body.is_a?(Hash)
7
+
8
+ extract_metadata(env)
9
+ extract_cursor_metadata(env)
10
+ end
11
+
12
+ private
13
+
14
+ def extract_metadata(env)
15
+ return unless env.body[:meta]
16
+
17
+ env[:response_meta] = env.body.delete(:meta)
18
+ end
19
+
20
+ def extract_cursor_metadata(env)
21
+ return unless env.body.key?(:next_cursor)
22
+
23
+ env[:response_meta] = {
24
+ cursor_pagination: {
25
+ next_cursor: env.body.delete(:next_cursor),
26
+ has_more: env.body.delete(:has_more)
27
+ }.compact
28
+ }
29
+ end
30
+ end
31
+ end
32
+
33
+ Faraday::Response.register_middleware(extract_meta: Scalingo::ExtractMeta)
@@ -0,0 +1,18 @@
1
+ require "faraday"
2
+
3
+ module Scalingo
4
+ class ExtractRootValue < Faraday::Middleware
5
+ def on_complete(env)
6
+ # We only want to unpack the response for successful responses
7
+ return unless env.response.success?
8
+
9
+ # Only hash-like objects are relevant to "unpack"
10
+ return unless env.body.is_a?(Hash)
11
+
12
+ # Dig the root key if it's the only remaining key in the body
13
+ env.body = env.body.values.first if env.body.size == 1
14
+ end
15
+ end
16
+ end
17
+
18
+ Faraday::Response.register_middleware(extract_root_value: Scalingo::ExtractRootValue)
@@ -0,0 +1,36 @@
1
+ require "faraday"
2
+ require "faraday/response"
3
+
4
+ # Note: the file is scalingo/faraday/response but we're reopning Faraday::Response:
5
+ # we define additional methods for Faraday::Response in order to enhance expressiveness
6
+ module Faraday
7
+ class Response
8
+ def meta
9
+ env[:response_meta]
10
+ end
11
+
12
+ def meta?
13
+ meta.present?
14
+ end
15
+
16
+ def pagination
17
+ return unless meta?
18
+
19
+ meta[:pagination]
20
+ end
21
+
22
+ def paginated?
23
+ pagination.present?
24
+ end
25
+
26
+ def cursor_pagination
27
+ return unless meta?
28
+
29
+ meta[:cursor_pagination]
30
+ end
31
+
32
+ def cursor_paginated?
33
+ cursor_pagination.present?
34
+ end
35
+ end
36
+ end