Class: Ovh::Client

Inherits:
Object
  • Object
show all
Includes:
Endpoints::Account, Endpoints::Consumer, Endpoints::Ips, Endpoints::Vps
Defined in:
lib/ovh/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Endpoints::Vps

#vps, #vps_ip_countries_available, #vps_ips, #vps_service

Methods included from Endpoints::Ips

#ip_load_balancing, #ip_service, #ip_services, #ips

Methods included from Endpoints::Account

#account_details

Methods included from Endpoints::Consumer

#request_consumer_key

Constructor Details

#initialize(configuration: ::Ovh.configuration) ⇒ Client

Returns a new instance of Client.



5
6
7
8
# File 'lib/ovh/client.rb', line 5

def initialize(configuration: ::Ovh.configuration)
  self.configuration      =   configuration
  self.endpoint           =   "#{self.configuration.api_host}/#{self.configuration.api_version}"
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



3
4
5
# File 'lib/ovh/client.rb', line 3

def configuration
  @configuration
end

#endpointObject

Returns the value of attribute endpoint.



3
4
5
# File 'lib/ovh/client.rb', line 3

def endpoint
  @endpoint
end

Instance Method Details

#auth(uri) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ovh/client.rb', line 35

def auth(uri)
  timestamp   =   Time.now.to_i
  
  signature   =   "$1$#{Digest::SHA1.hexdigest("#{self.configuration.application_secret}+#{self.configuration.consumer_key}+GET+#{uri}++#{timestamp}")}"

  headers     =   {
    'X-Ovh-Timestamp'   => timestamp.to_s,
    'X-Ovh-Signature'   => signature,
    'x-Ovh-Consumer'    => self.configuration.consumer_key
  }
end

#delete(path, params: {}, data: {}, headers: {}, options: {}) ⇒ Object



31
32
33
# File 'lib/ovh/client.rb', line 31

def delete(path, params: {}, data: {}, headers: {}, options: {})
  request path, method: :delete, params: params, data: data, headers: headers, options: options
end

#get(path, params: {}, headers: {}, options: {}) ⇒ Object



19
20
21
# File 'lib/ovh/client.rb', line 19

def get(path, params: {}, headers: {}, options: {})
  request path, method: :get, params: params, headers: headers, options: options
end

#post(path, params: {}, data: {}, headers: {}, options: {}) ⇒ Object



23
24
25
# File 'lib/ovh/client.rb', line 23

def post(path, params: {}, data: {}, headers: {}, options: {})
  request path, method: :post, params: params, data: data, headers: headers, options: options
end

#put(path, params: {}, data: {}, headers: {}, options: {}) ⇒ Object



27
28
29
# File 'lib/ovh/client.rb', line 27

def put(path, params: {}, data: {}, headers: {}, options: {})
  request path, method: :put, params: params, data: data, headers: headers, options: options
end

#request(path, method: :get, params: {}, data: {}, headers: {}, options: {}, retries: 3) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ovh/client.rb', line 47

def request(path, method: :get, params: {}, data: {}, headers: {}, options: {}, retries: 3)
  uri                           =   to_uri(path)
  
  headers["User-Agent"]         =   options.fetch(:user_agent, self.configuration.faraday.fetch(:user_agent, nil))
  headers["X-Ovh-Application"]  =   self.configuration.application_key
  headers                       =   headers.merge(auth(uri)) if options.fetch(:sign_request, true)

  connection    =   ::Faraday.new(url: uri) do |builder|
    builder.headers = headers
    
    builder.request  :json
    builder.response :json
    builder.response :logger if self.configuration.verbose_faraday?
  
    builder.adapter :net_http
  end
  
  response              =   nil
  
  begin
    response            =   case method
      when :get, :head, :options
        connection.send(method) do |request|
          request.params  =   params if params && !params.empty?
        end
      when :post, :put, :patch, :delete
        connection.send(method) do |request|
          request.body    =   data if data && !data.empty?
          request.params  =   params if params && !params.empty?
        end
    end
  rescue Faraday::TimeoutError, Faraday::ConnectionFailed, Faraday::ParsingError => e
    retries -= 1
    retry if retries > 0
  end
  
  return response&.body
end

#to_uri(path) ⇒ Object



15
16
17
# File 'lib/ovh/client.rb', line 15

def to_uri(path)
  "#{self.endpoint}#{path}"
end