-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.rb
More file actions
95 lines (80 loc) · 3.23 KB
/
api.rb
File metadata and controls
95 lines (80 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
85
86
87
88
89
90
91
92
93
94
95
require 'net/http'
require 'json'
require 'digest'
require 'yaml'
require_relative 'config'
require_relative 'cache'
require_relative 'errors/http_error'
module CFC
class API
def initialize
@base = 'https://api.cloudflare.com/client/v4/'
@cache = CFC::Cache.new
end
def get(path, params: nil, headers: nil, cache: true, expiry: nil)
request(Net::HTTP::Get, build_uri(path, params), headers: headers, cache: cache, expiry: expiry)
end
def get_json(path, params: nil, headers: nil, cache: true, expiry: nil)
request_json(Net::HTTP::Get, build_uri(path, params), headers: headers, cache: cache, expiry: expiry)
end
def delete(path, data: {}, headers: nil, cache: false, expiry: nil)
request(Net::HTTP::Delete, URI("#{@base}#{path}"), data: data, headers: headers, cache: cache, expiry: expiry)
end
def delete_to_json(path, data: {}, headers: nil, cache: false, expiry: nil)
request_json(Net::HTTP::Delete, URI("#{@base}#{path}"), data: data, headers: headers, cache: cache,
expiry: expiry)
end
[:post, :put, :patch].each do |method|
define_method method do |path, data, headers: nil, cache: false, expiry: nil|
request("Net::HTTP::#{method.capitalize}".constantize, URI("#{@base}#{path}"), data: data, headers: headers,
cache: cache, expiry: expiry)
end
define_method "#{method}_to_json" do |path, data, headers: nil, cache: false, expiry: nil|
request_json(Object.const_get("Net::HTTP::#{method.capitalize}"), URI("#{@base}#{path}"), data: data,
headers: headers, cache: cache, expiry: expiry)
end
end
protected
def request(cls, uri, data: nil, headers: nil, cache: true, expiry: nil)
headers = (headers || {}).merge({ 'Content-Type' => 'application/json' })
@auth_method = CFC::Config.instance.token.nil? ? :key : :token
case @auth_method
when :token
headers.merge!({ 'Authentication' => "Bearer #{CFC::Config.instance.token}" })
when :key
headers.merge!({
'X-Auth-Key' => CFC::Config.instance.api_key,
'X-Auth-Email' => CFC::Config.instance.api_email
})
else
raise CFC::Errors::ConfigurationError, 'Incorrect configuration parameters to set auth method'
end
rq = cls.new(uri, headers)
unless data.nil? || rq.is_a?(Net::HTTP::Head)
rq.body = JSON.dump(data)
end
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
if cache
@cache.fetch(Digest::SHA256.hexdigest("#{uri}|#{JSON.dump(headers)}"), expiry: expiry) do
http.request(rq)
end
else
http.request(rq)
end
end
if response.is_a?(Net::HTTPSuccess)
response
else
raise CFC::Errors::HTTPError.new(rq, response)
end
end
def request_json(cls, path, data: nil, headers: nil, cache: true, expiry: nil)
JSON.parse(request(cls, path, data: data, headers: headers, cache: cache, expiry: expiry).body)
end
def build_uri(path, params)
uri = URI("#{@base}#{path}")
uri.query = URI.encode_www_form((params || {}).to_a)
uri
end
end
end