ovh-api 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69d4f0ac19265bdc055a6fc8b695b1030a812d94
4
+ data.tar.gz: 621aff07a34b942b84a6c976c79e15b7f702ce76
5
+ SHA512:
6
+ metadata.gz: d662f03a606e032984f0fbeaff90ac9a265f0627fd82351e67af2aabf89706163fb7affbd3d97356bcd557d5b32f295cf5eb18617f57a3bf058e42037b8baae5
7
+ data.tar.gz: ddc4902d6c3da93b2304518c0889b28977b88e188f4a5fdc27822fa83b9ed55794a29464c5d44d7a8637e520c21569be85f2c55e085056030ef5c8d207b557cf
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2017 Roland Laurès / Semifir SAS
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+
2
+ module OVHApi
3
+ end
4
+
5
+ require 'ovh-api/version'
6
+ require 'ovh-api/exceptions'
7
+ require 'ovh-api/client'
@@ -0,0 +1,136 @@
1
+ require "net/https"
2
+ require "uri"
3
+ require 'json'
4
+ require 'yaml'
5
+ require 'digest/sha1'
6
+
7
+ # Main module
8
+ module OVHApi
9
+ # Main class
10
+ class Client
11
+
12
+ HOST = 'eu.api.ovh.com'
13
+ attr_reader :application_key, :application_secret, :consumer_key
14
+
15
+ def initialize(application_key: nil, application_secret: nil, consumer_key: nil)
16
+ begin
17
+ conf = YAML.load_file('config/ovh-api.yml')
18
+ @application_key = application_key || conf['application_key']
19
+ @application_secret = application_secret || conf['application_secret']
20
+ @consumer_key = consumer_key || conf['consumer_key']
21
+ rescue SystemCallError
22
+ end
23
+
24
+ @application_key = application_key
25
+ @application_secret = application_secret
26
+ @consumer_key = consumer_key
27
+
28
+ raise OVHApiNotConfiguredError.new(
29
+ "Either instantiate Client.new with application_key and application_secret, or create a YAML file in config/ovh-api.yml with those values set") if @application_key.nil? || @application_secret.nil?
30
+
31
+ end
32
+
33
+ # Request a consumer key
34
+ #
35
+ # @param [Hash] access_rules
36
+ # @return [Hash] the JSON response
37
+ def request_consumerkey(access_rules)
38
+ uri = ::URI.parse("https://#{HOST}")
39
+ http = ::Net::HTTP.new(uri.host, uri.port)
40
+ http.use_ssl = true
41
+
42
+ headers = {
43
+ 'X-Ovh-Application' => @application_key,
44
+ 'Content-type' => 'application/json'
45
+ }
46
+
47
+ resp = http.post('/1.0/auth/credential', access_rules.to_json, headers)
48
+ begin
49
+ body_hash = JSON.parse(resp.body)
50
+ @consumer_key = body_hash['consumerKey']
51
+
52
+ return resp, body_hash["validationUrl"]
53
+ rescue JSON::ParserError
54
+ return resp
55
+ end
56
+ end
57
+
58
+ # Generate signature
59
+ #
60
+ # @param url [String]
61
+ # @param method [String]
62
+ # @param timestamp [String]
63
+ # @param body [String]
64
+ #
65
+ def get_signature(url, method, timestamp, body = "")
66
+ signature = "$1$#{Digest::SHA1.hexdigest("#{application_secret}+#{consumer_key}+#{method}+https://#{HOST}/1.0#{url}+#{body}+#{timestamp}")}"
67
+ signature
68
+ end
69
+
70
+ # Make a get request to the OVH api
71
+ #
72
+ # @param url [String]
73
+ # @return [Net::HTTPResponse] response
74
+ def get(url)
75
+
76
+ request(url, 'GET', '')
77
+
78
+ end
79
+
80
+ # Make a post request to the OVH api
81
+ #
82
+ # @param url [String]
83
+ # @param body [String]
84
+ # @return [Net::HTTPResponse] response
85
+ def post(url, body)
86
+
87
+ request(url, 'POST', body)
88
+
89
+ end
90
+
91
+ # Make a put request to the OVH api
92
+ #
93
+ # @param url [String]
94
+ # @param body [String]
95
+ # @return [Net::HTTPResponse] response
96
+ def put(url, body)
97
+
98
+ request(url, 'PUT', body)
99
+ end
100
+
101
+
102
+ # Make a delete request to the OVH api
103
+ #
104
+ # @param url [String]
105
+ # @return [Net::HTTPResponse] response
106
+ def delete(url)
107
+
108
+ request(url, 'DELETE', '')
109
+
110
+ end
111
+
112
+ def request(url, method, body)
113
+
114
+ raise OVHApiNotConfiguredError.new(
115
+ "You cannot call Client#request without a consumer_key, please use the Client#request_consumerkey method to get one, and validate it with you credential by following the link, and/or save the consumer_key value in the YAML file in config/ovh-api.yml") if @consumer_key.nil?
116
+
117
+ uri = ::URI.parse("https://#{HOST}")
118
+ http = ::Net::HTTP.new(uri.host, uri.port)
119
+ http.use_ssl = true
120
+
121
+ timestamp = Time.now.to_i
122
+
123
+ headers = {
124
+ 'Host' => HOST,
125
+ 'Accept' => 'application/json',
126
+ 'Content-Type' => 'application/json',
127
+ 'X-Ovh-Application' => application_key,
128
+ 'X-Ovh-Timestamp' => timestamp.to_s,
129
+ 'X-Ovh-Signature' => get_signature(url, method, timestamp.to_s, body),
130
+ 'x-Ovh-Consumer' => consumer_key
131
+ }
132
+
133
+ http.send_request(method, "/1.0#{url}", body, headers)
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,4 @@
1
+ module OVHApi
2
+ class OVHApiError < RuntimeError; end
3
+ class OVHApiNotConfiguredError < OVHApiError; end
4
+ end
@@ -0,0 +1,3 @@
1
+ module OVHApi
2
+ VERSION = '1.0.1'
3
+ end
@@ -0,0 +1,97 @@
1
+ # Important note
2
+ This is not an official repository of OVH and this is a work in progress
3
+
4
+ Lightweight wrapper around OVH's APIs. Handles all the hard work including credential creation and requests signing.
5
+
6
+ ```ruby
7
+ require_relative 'client'
8
+
9
+ client = OVH::Client.new(
10
+ application_key: '<app key>',
11
+ application_secret: '<app secret>',
12
+ consumer_key: '<consumer key>'
13
+ )
14
+
15
+ puts client.get('/me').body['firstname']
16
+ ```
17
+
18
+ # Installation
19
+ For now you have to clone the repo and make a bundle.
20
+
21
+ # Example usage
22
+
23
+ ### Use the API on behalf of a user
24
+
25
+ 1. Create an application
26
+
27
+ To interact with the APIs, the SDK needs to identify itself using an application_key and an application_secret. To get them, you need to register your application.
28
+ For now only the OVH Europe API is supported.
29
+ - [OVH Europe](https://eu.api.ovh.com/createApp/)
30
+
31
+ Once created, you will obtain an __application key (AK)__ and an __application secret (AS)__.
32
+
33
+ 2. Configure your application
34
+
35
+ The easiest and safest way to use your application's credentials is create a conf.yml configuration file in application's working directory. Here is how it looks like:
36
+ ```yaml
37
+ application_key: app_key
38
+ application_secret: app_secret
39
+ # consumer_key: consumer_key
40
+ ```
41
+
42
+ 3. Authorize your application to access a customer account
43
+
44
+ To allow your application to access a customer account using the API on your behalf, you need a __consumer key (CK)__.
45
+
46
+ Here is a sample code you can use to allow your application to access a customer's informations:
47
+
48
+ ```ruby
49
+ require_relative 'client'
50
+ # create a client using configuration
51
+ client = OVH::Client.new
52
+
53
+ # Request RO, /me API access
54
+ access_rules = {
55
+ accessRules: [
56
+ {'method' => 'GET', 'path' => '/me'}
57
+ ]
58
+ }
59
+
60
+ # Request token
61
+ validation = JSON.parse(client.request_consumerkey(access_rules).body)
62
+
63
+ puts "Please visit #{validation['validationUrl']}"
64
+ puts "Then press Enter"
65
+ gets
66
+
67
+ puts "Welcome #{JSON.parse(client.get('/me').body)['firstname']}"
68
+ puts "Btw, your consumerKey is #{validation['consumerKey']}"
69
+ ```
70
+
71
+ Returned __consumerKey__ should then be kept to avoid re-authenticating your end-user on each use.
72
+
73
+ To request full and unlimited access to the API, you may use wildcards:
74
+
75
+ ```ruby
76
+ access_rules = [
77
+ {'method': 'GET', 'path': '/*'},
78
+ {'method': 'POST', 'path': '/*'},
79
+ {'method': 'PUT', 'path': '/*'},
80
+ {'method': 'DELETE', 'path': '/*'}
81
+ ]
82
+ ```
83
+
84
+ # Run the tests
85
+ For now the only test is a _Cucumber_ test (integration test).
86
+ More tests will come (cucmber and Rspec)
87
+
88
+ ```
89
+ cucumber
90
+ ```
91
+
92
+ # Build the documentation
93
+ Documentation is managed with _Yard_
94
+
95
+ ```
96
+ yard
97
+ ```
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ovh-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Roland Laurès
8
+ - Benoit Vasseur
9
+ - Zyurs
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2017-10-16 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: cucumber
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 2.0.2
25
+ type: :development
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - "~>"
30
+ - !ruby/object:Gem::Version
31
+ version: '2.0'
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.0.2
35
+ - !ruby/object:Gem::Dependency
36
+ name: webmock
37
+ requirement: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.21'
42
+ type: :development
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.21'
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ requirement: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.3'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.3'
63
+ description: 'Library wrapping OVH API v6 (see: https://api.ovh.com)'
64
+ email: roland.laures@semifir.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - LICENSE
70
+ - lib/ovh-api.rb
71
+ - lib/ovh-api/client.rb
72
+ - lib/ovh-api/exceptions.rb
73
+ - lib/ovh-api/version.rb
74
+ - readme.md
75
+ homepage: https://github.com/ShamoX/ruby-ovh
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.5.1
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: OVH API v6 wrapper
99
+ test_files: []