Class: OvhDnsup::OvhApi

Inherits:
Object
  • Object
show all
Defined in:
lib/ovh_dnsup/ovh_api.rb

Overview

Access to the OVH rest API. This class manages authentication and signage of API requests.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint: nil, application_key: nil, application_secret: nil, state: nil) ⇒ OvhApi

You have either to provide endpoint, application_key, and application_secret or provide a state.

Parameters:

  • endpoint (defaults to: nil)
  • application_key (defaults to: nil)
  • application_secret (defaults to: nil)
  • state (defaults to: nil)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ovh_dnsup/ovh_api.rb', line 52

def initialize(endpoint: nil, application_key: nil, application_secret: nil, state: nil)
  if state
    self.state = state
    raise 'Invalid arguments' if application_key || application_secret || endpoint
  else
    @endpoint = endpoint
    @application_key = application_key
    @application_secret = application_secret
    @consumer_key = nil

    @base_url = ENDPOINTS[@endpoint] + '1.0/'
    @conn = Faraday.new @base_url
  end
end

Instance Attribute Details

#application_keyObject (readonly)

Returns the value of attribute application_key.



35
36
37
# File 'lib/ovh_dnsup/ovh_api.rb', line 35

def application_key
  @application_key
end

#application_secretObject (readonly)

Returns the value of attribute application_secret.



35
36
37
# File 'lib/ovh_dnsup/ovh_api.rb', line 35

def application_secret
  @application_secret
end

#consumer_keyObject (readonly)

Returns the value of attribute consumer_key.



35
36
37
# File 'lib/ovh_dnsup/ovh_api.rb', line 35

def consumer_key
  @consumer_key
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



35
36
37
# File 'lib/ovh_dnsup/ovh_api.rb', line 35

def endpoint
  @endpoint
end

Class Method Details

.endpoint_url(endpoint) ⇒ Object



41
42
43
# File 'lib/ovh_dnsup/ovh_api.rb', line 41

def self.endpoint_url(endpoint)
  ENDPOINTS[endpoint]
end

.endpointsObject



37
38
39
# File 'lib/ovh_dnsup/ovh_api.rb', line 37

def self.endpoints
  ENDPOINTS.map { |k,v| k }
end

Instance Method Details

#auth_header(method, path, params = nil, body = '') ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ovh_dnsup/ovh_api.rb', line 126

def auth_header(method, path, params = nil, body = '')
  raise 'Only relative paths are allowed' if path.start_with?('/')
  url = @base_url + path
  if params
    url += '?' + Faraday::FlatParamsEncoder.encode(params)
  end
  tstamp = Time.now.to_i
  {
    'Content-type' => 'application/json',
    'X-Ovh-Application' => @application_key,
    'X-Ovh-Timestamp' => tstamp.to_s,
    'X-Ovh-Signature' => sign(method, url, body, tstamp),
    'X-Ovh-Consumer' => @consumer_key
  }
end

#delete(path, params = nil) ⇒ Object



158
159
160
161
# File 'lib/ovh_dnsup/ovh_api.rb', line 158

def delete(path, params=nil)
  response = @conn.delete(path, params, auth_header('DELETE', path))
  return process(response)
end

#get(path, params = nil) ⇒ Object



142
143
144
145
# File 'lib/ovh_dnsup/ovh_api.rb', line 142

def get(path, params = nil)
  response = @conn.get(path, params, auth_header('GET', path, params))
  return process(response)
end

#login(access_rules, verbose = true) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ovh_dnsup/ovh_api.rb', line 85

def (access_rules, verbose=true)
  register unless @application_key && @application_secret

  body = { :accessRules => access_rules }.to_json
  headers = { 'Content-type' => 'application/json',
              'X-Ovh-Application' => @application_key }
  response = @conn.post('auth/credential', body, headers)
  response = process(response)

  validation_url = response['validationUrl']
  @consumer_key = response['consumerKey']

  if verbose
    puts <<~EOS
    To complete the authentication process please open

      #{validation_url}

    and login with your credentials.
    EOS
  end
  return validation_url
end

#post(path, body = '') ⇒ Object



147
148
149
150
# File 'lib/ovh_dnsup/ovh_api.rb', line 147

def post(path, body='')
  response = @conn.post(path, body, auth_header('POST', path, nil, body))
  return process(response)
end

#put(path, obj) ⇒ Object



152
153
154
155
156
# File 'lib/ovh_dnsup/ovh_api.rb', line 152

def put(path, obj)
  body = obj.to_json
  response = @conn.put(path, body, auth_header('PUT', path, nil, body))
  return process(response)
end

#sign(method, url, body, tstamp) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ovh_dnsup/ovh_api.rb', line 109

def sign(method, url, body, tstamp)
  if !@application_key || !@application_secret
    raise OvhException.new('Application key and/or secret missing.')
  end
  if !@consumer_key
    raise OvhException.new('Not logged in')
  end

  "$1$" + Digest::SHA1.hexdigest(
              @application_secret + "+" +
              @consumer_key + "+" +
              method + "+" +
              url + "+" +
              body + "+" +
              tstamp.to_s)
end

#stateObject

The state which can be saved and later restored.



68
69
70
71
72
73
# File 'lib/ovh_dnsup/ovh_api.rb', line 68

def state
  { 'endpoint' => @endpoint,
    'application_key' => @application_key,
    'application_secret' => @application_secret,
    'consumer_key' => @consumer_key }
end

#state=(h) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/ovh_dnsup/ovh_api.rb', line 75

def state= h
  @endpoint = h['endpoint']
  @application_key = h['application_key']
  @application_secret = h['application_secret']
  @consumer_key = h['consumer_key']

  @base_url = ENDPOINTS[@endpoint] + '1.0/'
  @conn = Faraday.new @base_url
end