cloudflare_client_rb 4.0.0 → 4.1.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97207869fc9f1586e9c8f2d51b1e2467d615020c
|
4
|
+
data.tar.gz: b3c6f6042a41012d3629f2e93364bbc34c0b4051
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49a932c5de6eeb9601d598fe49acdef3d234c20c70cd22f24fab7a4807f8b653d1610be67372ff13c55bb59e0f7ab918ff11ab8cad39c056bef0da909bdd3e15
|
7
|
+
data.tar.gz: e97e7da2f1201ed21693d4c87879d962dba9101fcdf16b1f70d518a592f493a9f76af60d82fd88b44966ac1544fb3de1dab5176427abf5451a665a70f3d86bc9
|
data/lib/cloudflare_client.rb
CHANGED
@@ -6,6 +6,7 @@ class CloudflareClient
|
|
6
6
|
require 'date'
|
7
7
|
require 'byebug'
|
8
8
|
Dir[File.expand_path('../cloudflare_client/*.rb', __FILE__)].each { |f| require f }
|
9
|
+
require 'cloudflare_client/middleware/response/raise_error'
|
9
10
|
|
10
11
|
API_BASE = 'https://api.cloudflare.com/client/v4'.freeze
|
11
12
|
VALID_BUNDLE_METHODS = %w[ubiquitous optimal force].freeze
|
@@ -136,6 +137,7 @@ class CloudflareClient
|
|
136
137
|
client = Faraday.new(url: API_BASE) do |conn|
|
137
138
|
conn.request :multipart
|
138
139
|
conn.request :url_encoded
|
140
|
+
conn.use Middleware::Response::RaiseError
|
139
141
|
conn.adapter :net_http
|
140
142
|
end
|
141
143
|
client.headers['X-Auth-Key'] = params[:auth_key]
|
@@ -0,0 +1,109 @@
|
|
1
|
+
class CloudflareClient
|
2
|
+
class ResponseError < StandardError
|
3
|
+
# The Faraday::Response object that caused the exception to be raised.
|
4
|
+
attr_reader :response, :method, :uri, :url
|
5
|
+
|
6
|
+
def initialize(message = nil, response = nil, method = nil, uri = nil, url = nil)
|
7
|
+
super(message)
|
8
|
+
@response = response
|
9
|
+
@method = method
|
10
|
+
@uri = uri
|
11
|
+
@url = url
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# An exception that is raised when a response status in the 4xx range is
|
16
|
+
# encountered. There's a number of subclasses that allow you to granularly
|
17
|
+
# handle specific status codes.
|
18
|
+
class ClientError < ResponseError; end
|
19
|
+
|
20
|
+
# Client errors:
|
21
|
+
class BadRequest < ClientError; end
|
22
|
+
class Unauthorized < ClientError; end
|
23
|
+
class Forbidden < ClientError; end
|
24
|
+
class ResourceNotFound < ClientError; end
|
25
|
+
class Conflict < ClientError; end
|
26
|
+
class Gone < ClientError; end
|
27
|
+
class PreconditionFailed < ClientError; end
|
28
|
+
class UnprocessableEntity < ClientError; end
|
29
|
+
class TooManyRequests < ClientError; end
|
30
|
+
class Locked < ClientError; end
|
31
|
+
|
32
|
+
# An exception that is raised when a response status in the 5xx range is
|
33
|
+
# encountered. There's a number of subclasses that allow you to granularly
|
34
|
+
# handle specific status codes.
|
35
|
+
class ServerError < ResponseError; end
|
36
|
+
|
37
|
+
# Server errors:
|
38
|
+
class InternalServerError < ServerError; end
|
39
|
+
class BadGateway < ServerError; end
|
40
|
+
class ServiceUnavailable < ServerError; end
|
41
|
+
class GatewayTimeout < ServerError; end
|
42
|
+
|
43
|
+
module Middleware
|
44
|
+
module Response
|
45
|
+
# Raises ResponseError exceptions when a response status in either the
|
46
|
+
# 4xx range or the 5xx range is encountered. There are a number of specific
|
47
|
+
# exception mappings as well as general exception types covering the
|
48
|
+
# respective ranges.
|
49
|
+
class RaiseError
|
50
|
+
CLIENT_ERRORS = 400...500
|
51
|
+
SERVER_ERRORS = 500...600
|
52
|
+
|
53
|
+
def initialize(app)
|
54
|
+
@app = app
|
55
|
+
end
|
56
|
+
|
57
|
+
def call(env)
|
58
|
+
response = @app.call(env)
|
59
|
+
handle_status(response, env.method, env.url.request_uri, env.url.to_s)
|
60
|
+
response
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def handle_status(response, method, uri, url)
|
66
|
+
case response.status
|
67
|
+
when 400
|
68
|
+
raise CloudflareClient::BadRequest.new('400 Bad Request', response, method, uri, url)
|
69
|
+
when 401
|
70
|
+
raise CloudflareClient::Unauthorized.new('401 Unauthorized', response, method, uri, url)
|
71
|
+
when 403
|
72
|
+
raise CloudflareClient::Forbidden.new('403 Forbidden', response, method, uri, url)
|
73
|
+
when 404
|
74
|
+
raise CloudflareClient::ResourceNotFound.new('404 Not Found', response, method, uri, url)
|
75
|
+
when 407
|
76
|
+
# Mimic the behavior that we get with proxy requests with HTTPS. We still
|
77
|
+
# use Faraday exceptions for network errors, as it's difficult to reliably
|
78
|
+
# wrap these in CloudflareClient without losing information.
|
79
|
+
raise Faraday::ConnectionFailed.new('407 Proxy Authentication Required', response)
|
80
|
+
when 409
|
81
|
+
raise CloudflareClient::Conflict.new('409 Conflict', response, method, uri, url)
|
82
|
+
when 410
|
83
|
+
raise CloudflareClient::Gone.new('410 Gone', response, method, uri, url)
|
84
|
+
when 412
|
85
|
+
raise CloudflareClient::PreconditionFailed.new('412 Precondition Failed', response, method, uri, url)
|
86
|
+
when 422
|
87
|
+
raise CloudflareClient::UnprocessableEntity.new('422 Unprocessable Entity', response, method, uri, url)
|
88
|
+
when 423
|
89
|
+
raise CloudflareClient::Locked.new('423 Locked', response, method, uri, url)
|
90
|
+
when 429
|
91
|
+
raise CloudflareClient::TooManyRequests.new('429 Too Many Requests', response, method, uri, url)
|
92
|
+
when 500
|
93
|
+
raise CloudflareClient::InternalServerError.new('500 Internal Server Error', response, method, uri, url)
|
94
|
+
when 502
|
95
|
+
raise CloudflareClient::BadGateway.new('502 Bad Gateway', response, method, uri, url)
|
96
|
+
when 503
|
97
|
+
raise CloudflareClient::ServiceUnavailable.new('503 Service Unavailable', response, method, uri, url)
|
98
|
+
when 504
|
99
|
+
raise CloudflareClient::GatewayTimeout.new('504 Gateway Timeout', response, method, uri, url)
|
100
|
+
when CLIENT_ERRORS
|
101
|
+
raise CloudflareClient::ClientError.new(response.status.to_s, response, method, uri, url)
|
102
|
+
when SERVER_ERRORS
|
103
|
+
raise CloudflareClient::ServerError.new(response.status.to_s, response, method, uri, url)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -9,6 +9,8 @@ class CloudflareClient::Zone::DNS < CloudflareClient::Zone::Base
|
|
9
9
|
def create(name:, type:, content:, ttl: nil, proxied: nil)
|
10
10
|
raise ("type must be one of #{VALID_TYPES.flatten}") unless VALID_TYPES.include?(type)
|
11
11
|
data = {name: name, type: type, content: content}
|
12
|
+
data[:ttl] = ttl unless ttl.nil?
|
13
|
+
data[:proxied] = proxied unless proxied.nil?
|
12
14
|
cf_post(path: "/zones/#{zone_id}/dns_records", data: data)
|
13
15
|
end
|
14
16
|
|
@@ -20,7 +20,7 @@ class CloudflareClient::Zone::PageRule < CloudflareClient::Zone::Base
|
|
20
20
|
|
21
21
|
##
|
22
22
|
# list all the page rules for a zone
|
23
|
-
def list(status: '
|
23
|
+
def list(status: 'active', order: 'priority', direction: 'desc', match: 'all')
|
24
24
|
valid_value_check(:status, status, VALID_STATUSES)
|
25
25
|
valid_value_check(:order, order, VALID_ORDERS)
|
26
26
|
valid_value_check(:direction, direction, VALID_DIRECTIONS)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cloudflare_client_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ian waters
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: iwaters@zendesk.com
|
@@ -18,6 +18,7 @@ extra_rdoc_files: []
|
|
18
18
|
files:
|
19
19
|
- lib/cloudflare_client.rb
|
20
20
|
- lib/cloudflare_client/certificate.rb
|
21
|
+
- lib/cloudflare_client/middleware/response/raise_error.rb
|
21
22
|
- lib/cloudflare_client/organization.rb
|
22
23
|
- lib/cloudflare_client/organization/access_rule.rb
|
23
24
|
- lib/cloudflare_client/organization/invite.rb
|