Class: OVHApi::Client
- Inherits:
-
Object
- Object
- OVHApi::Client
- Defined in:
- lib/ovh-api/client.rb
Overview
Main class
Constant Summary collapse
- HOST =
'eu.api.ovh.com'
Instance Attribute Summary collapse
-
#application_key ⇒ Object
readonly
Returns the value of attribute application_key.
-
#application_secret ⇒ Object
readonly
Returns the value of attribute application_secret.
-
#consumer_key ⇒ Object
readonly
Returns the value of attribute consumer_key.
Instance Method Summary collapse
-
#delete(url) ⇒ Net::HTTPResponse
Make a delete request to the OVH api.
-
#get(url) ⇒ Net::HTTPResponse
Make a get request to the OVH api.
-
#get_signature(url, method, timestamp, body = "") ⇒ Object
Generate signature.
-
#initialize(application_key: nil, application_secret: nil, consumer_key: nil) ⇒ Client
constructor
A new instance of Client.
-
#post(url, body) ⇒ Net::HTTPResponse
Make a post request to the OVH api.
-
#put(url, body) ⇒ Net::HTTPResponse
Make a put request to the OVH api.
- #request(url, method, body) ⇒ Object
-
#request_consumerkey(access_rules) ⇒ Hash
Request a consumer key.
Constructor Details
#initialize(application_key: nil, application_secret: nil, consumer_key: nil) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ovh-api/client.rb', line 15 def initialize(application_key: nil, application_secret: nil, consumer_key: nil) begin conf = YAML.load_file('./config/ovh-api.yml') @application_key = application_key || conf['application_key'] @application_secret = application_secret || conf['application_secret'] @consumer_key = consumer_key || conf['consumer_key'] rescue SystemCallError @application_key = application_key @application_secret = application_secret @consumer_key = consumer_key end raise OVHApiNotConfiguredError.new( "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? end |
Instance Attribute Details
#application_key ⇒ Object (readonly)
Returns the value of attribute application_key.
13 14 15 |
# File 'lib/ovh-api/client.rb', line 13 def application_key @application_key end |
#application_secret ⇒ Object (readonly)
Returns the value of attribute application_secret.
13 14 15 |
# File 'lib/ovh-api/client.rb', line 13 def application_secret @application_secret end |
#consumer_key ⇒ Object (readonly)
Returns the value of attribute consumer_key.
13 14 15 |
# File 'lib/ovh-api/client.rb', line 13 def consumer_key @consumer_key end |
Instance Method Details
#delete(url) ⇒ Net::HTTPResponse
Make a delete request to the OVH api
104 105 106 107 108 |
# File 'lib/ovh-api/client.rb', line 104 def delete(url) request(url, 'DELETE', '') end |
#get(url) ⇒ Net::HTTPResponse
Make a get request to the OVH api
72 73 74 75 76 |
# File 'lib/ovh-api/client.rb', line 72 def get(url) request(url, 'GET', '') end |
#get_signature(url, method, timestamp, body = "") ⇒ Object
Generate signature
63 64 65 66 |
# File 'lib/ovh-api/client.rb', line 63 def get_signature(url, method, , body = "") signature = "$1$#{Digest::SHA1.hexdigest("#{application_secret}+#{consumer_key}+#{method}+https://#{HOST}/1.0#{url}+#{body}+#{}")}" signature end |
#post(url, body) ⇒ Net::HTTPResponse
Make a post request to the OVH api
83 84 85 86 87 |
# File 'lib/ovh-api/client.rb', line 83 def post(url, body) request(url, 'POST', body) end |
#put(url, body) ⇒ Net::HTTPResponse
Make a put request to the OVH api
94 95 96 97 |
# File 'lib/ovh-api/client.rb', line 94 def put(url, body) request(url, 'PUT', body) end |
#request(url, method, body) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/ovh-api/client.rb', line 110 def request(url, method, body) raise OVHApiNotConfiguredError.new( "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? uri = ::URI.parse("https://#{HOST}") http = ::Net::HTTP.new(uri.host, uri.port) http.use_ssl = true = Time.now.to_i headers = { 'Host' => HOST, 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'X-Ovh-Application' => application_key, 'X-Ovh-Timestamp' => .to_s, 'X-Ovh-Signature' => get_signature(url, method, .to_s, body), 'x-Ovh-Consumer' => consumer_key } http.send_request(method, "/1.0#{url}", body, headers) end |
#request_consumerkey(access_rules) ⇒ Hash
Request a consumer key
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ovh-api/client.rb', line 35 def request_consumerkey(access_rules) uri = ::URI.parse("https://#{HOST}") http = ::Net::HTTP.new(uri.host, uri.port) http.use_ssl = true headers = { 'X-Ovh-Application' => @application_key, 'Content-type' => 'application/json' } resp = http.post('/1.0/auth/credential', access_rules.to_json, headers) begin body_hash = JSON.parse(resp.body) @consumer_key = body_hash['consumerKey'] return resp, body_hash["validationUrl"] rescue JSON::ParserError return resp end end |