Class: OVHApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ovh-api/client.rb

Overview

Main class

Constant Summary collapse

HOST =
'eu.api.ovh.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject (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_secretObject (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_keyObject (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

Parameters:

  • url (String)

Returns:

  • (Net::HTTPResponse)

    response



138
139
140
141
142
# File 'lib/ovh-api/client.rb', line 138

def delete(url)

  request(url, 'DELETE', '')

end

#get(url) ⇒ Net::HTTPResponse

Make a get request to the OVH api

Parameters:

  • url (String)

Returns:

  • (Net::HTTPResponse)

    response



106
107
108
109
110
# File 'lib/ovh-api/client.rb', line 106

def get(url)

  request(url, 'GET', '')

end

#get_signature(url, method, timestamp, body = "") ⇒ Object

Generate signature

Parameters:

  • url (String)
  • method (String)
  • timestamp (String)
  • body (String) (defaults to: "")


63
64
65
66
# File 'lib/ovh-api/client.rb', line 63

def get_signature(url, method, timestamp, body = "")
  signature = "$1$#{Digest::SHA1.hexdigest("#{application_secret}+#{consumer_key}+#{method}+https://#{HOST}/1.0#{url}+#{body}+#{timestamp}")}"
  signature
end

#post(url, body) ⇒ Net::HTTPResponse

Make a post request to the OVH api

Parameters:

  • url (String)
  • body (String)

Returns:

  • (Net::HTTPResponse)

    response



117
118
119
120
121
# File 'lib/ovh-api/client.rb', line 117

def post(url, body)

  request(url, 'POST', body)

end

#put(url, body) ⇒ Net::HTTPResponse

Make a put request to the OVH api

Parameters:

  • url (String)
  • body (String)

Returns:

  • (Net::HTTPResponse)

    response



128
129
130
131
# File 'lib/ovh-api/client.rb', line 128

def put(url, body)

  request(url, 'PUT', body)
end

#request(url, method, body) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ovh-api/client.rb', line 144

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

  timestamp = Time.now.to_i

  headers = {
    'Host'              => HOST,
    'Accept'            => 'application/json',
    'Content-Type'      => 'application/json',
    'X-Ovh-Application' => application_key,
    'X-Ovh-Timestamp'   => timestamp.to_s,
    'X-Ovh-Signature'   => get_signature(url, method, timestamp.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

Parameters:

  • access_rules (Hash)

Returns:

  • (Hash)

    the JSON response



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

#request_json(method, path, arguments = nil, body = '') ⇒ Hash

Helper to make a request to the OVH api then return the body as parsed JSON tree

If body cannot be parsed, error is rescued and :body is set to nil.

This function raise OVHApiNotImplementedError if method is not valid

Parameters:

  • method (Symbol)

    : :get, :post, :put, :delete

  • path (String)
  • arguments: (Hash)

    : will be encoded to be URL friendly with URI.encode_www_form and then pass as URL parameters

  • body (String) (defaults to: '')

    : function parameters to be JSONified and sent as body in the request

Returns:

  • (Hash)

    { :resp => [Net::HTTPResponse] response, :body => [Hash|NilClass] (:resp body JSON parsed) }

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ovh-api/client.rb', line 80

def request_json(method, path, arguments = nil, body = '')
  raise OVHApiNotImplementedError.new(
    "#{method.to_s} is not implemented. Please refere to documentation."
  ) unless [:get, :post, :delete, :put].include?method
  method_str = method.to_s.upcase
  if arguments.nil? then
    url = path
  else
    url = "#{path}?#{URI.encode_www_form(arguments)}"
  end
  resp = request(url, method_str, body)
  body = nil
  begin
    body = JSON.parse(resp.body)
  rescue
  end
  return {
    :resp => resp,
    :body => body
  }
end