ruby-anvil 0.0.1.pre.rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +61 -0
- data/LICENSE.txt +21 -0
- data/README.md +802 -0
- data/lib/anvil/auth.rb +66 -0
- data/lib/anvil/client.rb +624 -0
- data/lib/anvil/errors.rb +230 -0
- data/lib/anvil/graphql.rb +270 -0
- data/lib/anvil/multipart.rb +104 -0
- data/lib/anvil/payloads/base.rb +92 -0
- data/lib/anvil/payloads/create_etch_packet.rb +13 -0
- data/lib/anvil/payloads/etch_file.rb +21 -0
- data/lib/anvil/payloads/etch_signer.rb +22 -0
- data/lib/anvil/payloads/fill_pdf.rb +13 -0
- data/lib/anvil/payloads/forge_submit.rb +16 -0
- data/lib/anvil/payloads/generate_pdf.rb +26 -0
- data/lib/anvil/rate_limiter.rb +236 -0
- data/lib/anvil/response.rb +84 -0
- data/lib/anvil/types/base.rb +147 -0
- data/lib/anvil/types/etch_packet.rb +15 -0
- data/lib/anvil/types/generated/graphql.rb +1713 -0
- data/lib/anvil/types/generated/operations.rb +1342 -0
- data/lib/anvil/types/generated/rest.rb +496 -0
- data/lib/anvil/upload_with_options.rb +113 -0
- data/lib/anvil/uploads.rb +119 -0
- data/lib/anvil/version.rb +5 -0
- data/lib/anvil.rb +98 -0
- data/lib/ruby-anvil.rb +5 -0
- metadata +73 -0
data/lib/anvil/errors.rb
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anvil
|
|
4
|
+
# Base class for everything this client raises. Rescue it to catch any
|
|
5
|
+
# client or API failure.
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
|
|
8
|
+
# Raised when the client is constructed or called with unusable arguments,
|
|
9
|
+
# before any request is made.
|
|
10
|
+
class ConfigurationError < Error; end
|
|
11
|
+
|
|
12
|
+
# Raised when a request could not be completed at all: a connection reset, a
|
|
13
|
+
# DNS failure, a timeout, an unparseable response body.
|
|
14
|
+
class ConnectionError < Error
|
|
15
|
+
# @return [Exception, nil] the underlying exception, when there was one
|
|
16
|
+
attr_reader :cause_error
|
|
17
|
+
|
|
18
|
+
def initialize(message, cause_error: nil)
|
|
19
|
+
super(message)
|
|
20
|
+
@cause_error = cause_error
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# A single field-level problem inside an {ErrorDetail}: which field the API
|
|
25
|
+
# rejected, and why.
|
|
26
|
+
class ErrorField
|
|
27
|
+
# @return [String, nil]
|
|
28
|
+
attr_reader :message
|
|
29
|
+
# @return [String, nil] the name of the offending field
|
|
30
|
+
attr_reader :property
|
|
31
|
+
# @return [Hash] the field as the API returned it
|
|
32
|
+
attr_reader :raw
|
|
33
|
+
|
|
34
|
+
def initialize(message: nil, property: nil, raw: {})
|
|
35
|
+
@message = message
|
|
36
|
+
@property = property
|
|
37
|
+
@raw = raw
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @param hash [Hash]
|
|
41
|
+
# @return [ErrorField]
|
|
42
|
+
def self.from_h(hash)
|
|
43
|
+
hash = { "message" => hash.to_s } unless hash.is_a?(Hash)
|
|
44
|
+
new(
|
|
45
|
+
message: hash["message"] || hash[:message],
|
|
46
|
+
property: hash["property"] || hash[:property],
|
|
47
|
+
raw: hash
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def to_s
|
|
52
|
+
property ? "#{property}: #{message}" : message.to_s
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def to_h
|
|
56
|
+
raw
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# One normalized error from an API response. Both GraphQL errors and the
|
|
61
|
+
# REST API's error bodies are normalized into these.
|
|
62
|
+
class ErrorDetail
|
|
63
|
+
# @return [String, nil] the error class the API reported, e.g. "ValidationError"
|
|
64
|
+
attr_reader :name
|
|
65
|
+
# @return [String, nil]
|
|
66
|
+
attr_reader :message
|
|
67
|
+
# @return [Array<ErrorField>] field-level details, empty when there are none
|
|
68
|
+
attr_reader :fields
|
|
69
|
+
# @return [Array<String>, nil] a GraphQL error's path into the query
|
|
70
|
+
attr_reader :path
|
|
71
|
+
# @return [Hash] the error as the API returned it, including any keys not
|
|
72
|
+
# broken out above
|
|
73
|
+
attr_reader :raw
|
|
74
|
+
|
|
75
|
+
def initialize(name: nil, message: nil, fields: [], path: nil, raw: {})
|
|
76
|
+
@name = name
|
|
77
|
+
@message = message
|
|
78
|
+
@fields = fields
|
|
79
|
+
@path = path
|
|
80
|
+
@raw = raw
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# @param hash [Hash, String]
|
|
84
|
+
# @return [ErrorDetail]
|
|
85
|
+
def self.from_h(hash)
|
|
86
|
+
return new(message: hash.to_s, raw: { "message" => hash.to_s }) unless hash.is_a?(Hash)
|
|
87
|
+
|
|
88
|
+
fields = hash["fields"] || hash[:fields] || []
|
|
89
|
+
fields = [] unless fields.is_a?(Array)
|
|
90
|
+
|
|
91
|
+
new(
|
|
92
|
+
name: hash["name"] || hash[:name],
|
|
93
|
+
message: hash["message"] || hash[:message],
|
|
94
|
+
fields: fields.map { |field| ErrorField.from_h(field) },
|
|
95
|
+
path: hash["path"] || hash[:path],
|
|
96
|
+
raw: hash
|
|
97
|
+
)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def to_s
|
|
101
|
+
parts = [name, message].compact.uniq
|
|
102
|
+
base = parts.empty? ? "Unknown Error" : parts.join(": ")
|
|
103
|
+
return base if fields.empty?
|
|
104
|
+
|
|
105
|
+
"#{base} (#{fields.join(", ")})"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def to_h
|
|
109
|
+
raw
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Raised when the API answered but the answer was an error. Carries the HTTP
|
|
114
|
+
# status code and the normalized errors.
|
|
115
|
+
class ResponseError < Error
|
|
116
|
+
# @return [Integer, nil] the HTTP status code
|
|
117
|
+
attr_reader :status_code
|
|
118
|
+
# @return [Array<ErrorDetail>]
|
|
119
|
+
attr_reader :errors
|
|
120
|
+
# @return [Object, nil] the parsed response body, when it was JSON
|
|
121
|
+
attr_reader :body
|
|
122
|
+
# @return [Hash] the response headers, keys downcased
|
|
123
|
+
attr_reader :headers
|
|
124
|
+
|
|
125
|
+
def initialize(message = nil, status_code: nil, errors: [], body: nil, headers: {})
|
|
126
|
+
@status_code = status_code
|
|
127
|
+
@errors = errors
|
|
128
|
+
@body = body
|
|
129
|
+
@headers = headers
|
|
130
|
+
super(message || self.class.summarize(errors, status_code))
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# @return [ErrorDetail, nil] the first error, which is the only one for
|
|
134
|
+
# most REST failures
|
|
135
|
+
def error
|
|
136
|
+
errors.first
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# @return [Array<ErrorField>] every field-level error across all errors
|
|
140
|
+
def fields
|
|
141
|
+
errors.flat_map(&:fields)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# @api private
|
|
145
|
+
def self.summarize(errors, status_code)
|
|
146
|
+
return "HTTP #{status_code || "error"}" if errors.nil? || errors.empty?
|
|
147
|
+
|
|
148
|
+
errors.join("; ")
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# A GraphQL response that carried an `errors` array. GraphQL answers with
|
|
153
|
+
# HTTP 200 even when the query failed, so this can be raised on a 200.
|
|
154
|
+
class GraphQLError < ResponseError; end
|
|
155
|
+
|
|
156
|
+
# A 4xx response.
|
|
157
|
+
class ClientError < ResponseError; end
|
|
158
|
+
|
|
159
|
+
# 401: the API key or access token was missing or not accepted.
|
|
160
|
+
class AuthenticationError < ClientError; end
|
|
161
|
+
|
|
162
|
+
# 403: authenticated, but not allowed to do this.
|
|
163
|
+
class ForbiddenError < ClientError; end
|
|
164
|
+
|
|
165
|
+
# 404: no such resource.
|
|
166
|
+
class NotFoundError < ClientError; end
|
|
167
|
+
|
|
168
|
+
# 400 or 422: the request was understood but rejected. Check {#fields} for
|
|
169
|
+
# which parts of the payload were at fault.
|
|
170
|
+
class ValidationError < ClientError; end
|
|
171
|
+
|
|
172
|
+
# 429, still rate limited after the client's retries were used up.
|
|
173
|
+
class RateLimitError < ClientError
|
|
174
|
+
# @return [Float, nil] the retry-after the API asked for, in seconds
|
|
175
|
+
attr_reader :retry_after
|
|
176
|
+
|
|
177
|
+
def initialize(message = nil, retry_after: nil, **kwargs)
|
|
178
|
+
@retry_after = retry_after
|
|
179
|
+
super(message, **kwargs)
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
# A 5xx response.
|
|
184
|
+
class ServerError < ResponseError; end
|
|
185
|
+
|
|
186
|
+
# Turns the many shapes an Anvil error response can take into {ErrorDetail}s,
|
|
187
|
+
# and picks the exception class for a status code.
|
|
188
|
+
#
|
|
189
|
+
# Ported from node-anvil's `src/errors.js` so both clients treat the same
|
|
190
|
+
# response the same way.
|
|
191
|
+
module Errors
|
|
192
|
+
module_function
|
|
193
|
+
|
|
194
|
+
# @param json [Object, nil] the parsed response body
|
|
195
|
+
# @param status_text [String] the HTTP reason phrase, used when the body
|
|
196
|
+
# says nothing useful
|
|
197
|
+
# @return [Array<ErrorDetail>] always at least one error
|
|
198
|
+
def normalize(json, status_text: "Unknown Error")
|
|
199
|
+
if json.is_a?(Hash)
|
|
200
|
+
# The GraphQL way, and some REST errors
|
|
201
|
+
errors = json["errors"]
|
|
202
|
+
return errors.map { |error| ErrorDetail.from_h(error) } if errors.is_a?(Array) && !errors.empty?
|
|
203
|
+
|
|
204
|
+
# The other REST way:
|
|
205
|
+
# { "name": "AssertionError", "message": "PDF did not generate properly from given HTML!" }
|
|
206
|
+
# or
|
|
207
|
+
# { "name": "ValidationError", "fields": [{ "message": "Required", "property": "data" }] }
|
|
208
|
+
return [ErrorDetail.from_h(json)] if json["message"] || json["name"]
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
[ErrorDetail.new(name: status_text, message: status_text,
|
|
212
|
+
raw: { "name" => status_text, "message" => status_text })]
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# @param status_code [Integer, nil]
|
|
216
|
+
# @return [Class] the {ResponseError} subclass for this status
|
|
217
|
+
def error_class_for(status_code)
|
|
218
|
+
case status_code
|
|
219
|
+
when 400, 422 then ValidationError
|
|
220
|
+
when 401 then AuthenticationError
|
|
221
|
+
when 403 then ForbiddenError
|
|
222
|
+
when 404 then NotFoundError
|
|
223
|
+
when 429 then RateLimitError
|
|
224
|
+
when 500..599 then ServerError
|
|
225
|
+
when 300..499 then ClientError
|
|
226
|
+
else ResponseError
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
end
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anvil
|
|
4
|
+
# The GraphQL operations this client sends.
|
|
5
|
+
#
|
|
6
|
+
# The operation text and the default response queries are kept identical to
|
|
7
|
+
# node-anvil's `src/graphql/` directory, so the same call returns the same
|
|
8
|
+
# fields whichever client you use. Pass your own `response_query` to any of
|
|
9
|
+
# these to select different fields, or your own `mutation` to replace the
|
|
10
|
+
# operation outright.
|
|
11
|
+
module GraphQL
|
|
12
|
+
# The fields each operation selects when you do not ask for others.
|
|
13
|
+
module DefaultResponseQueries
|
|
14
|
+
CREATE_ETCH_PACKET = <<~GRAPHQL.strip
|
|
15
|
+
{
|
|
16
|
+
eid
|
|
17
|
+
name
|
|
18
|
+
status
|
|
19
|
+
isTest
|
|
20
|
+
allowUpdates
|
|
21
|
+
numberRemainingSigners
|
|
22
|
+
detailsURL
|
|
23
|
+
webhookURL
|
|
24
|
+
completedAt
|
|
25
|
+
archivedAt
|
|
26
|
+
createdAt
|
|
27
|
+
updatedAt
|
|
28
|
+
documentGroup {
|
|
29
|
+
eid
|
|
30
|
+
status
|
|
31
|
+
files
|
|
32
|
+
signers {
|
|
33
|
+
eid
|
|
34
|
+
aliasId
|
|
35
|
+
routingOrder
|
|
36
|
+
name
|
|
37
|
+
email
|
|
38
|
+
status
|
|
39
|
+
signActionType
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
GRAPHQL
|
|
44
|
+
|
|
45
|
+
CURRENT_USER = <<~GRAPHQL.strip
|
|
46
|
+
{
|
|
47
|
+
eid
|
|
48
|
+
name
|
|
49
|
+
email
|
|
50
|
+
role
|
|
51
|
+
organizations {
|
|
52
|
+
eid
|
|
53
|
+
name
|
|
54
|
+
slug
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
GRAPHQL
|
|
58
|
+
|
|
59
|
+
ETCH_PACKET = <<~GRAPHQL.strip
|
|
60
|
+
{
|
|
61
|
+
id
|
|
62
|
+
eid
|
|
63
|
+
name
|
|
64
|
+
status
|
|
65
|
+
isTest
|
|
66
|
+
numberRemainingSigners
|
|
67
|
+
webhookURL
|
|
68
|
+
detailsURL
|
|
69
|
+
completedAt
|
|
70
|
+
archivedAt
|
|
71
|
+
createdAt
|
|
72
|
+
updatedAt
|
|
73
|
+
documentGroup {
|
|
74
|
+
id
|
|
75
|
+
eid
|
|
76
|
+
status
|
|
77
|
+
files
|
|
78
|
+
signers {
|
|
79
|
+
id
|
|
80
|
+
eid
|
|
81
|
+
aliasId
|
|
82
|
+
routingOrder
|
|
83
|
+
name
|
|
84
|
+
email
|
|
85
|
+
status
|
|
86
|
+
signActionType
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
GRAPHQL
|
|
91
|
+
|
|
92
|
+
FORGE_SUBMIT = <<~GRAPHQL.strip
|
|
93
|
+
{
|
|
94
|
+
id
|
|
95
|
+
eid
|
|
96
|
+
status
|
|
97
|
+
continueURL
|
|
98
|
+
payloadValue
|
|
99
|
+
currentStep
|
|
100
|
+
completedAt
|
|
101
|
+
createdAt
|
|
102
|
+
updatedAt
|
|
103
|
+
signer {
|
|
104
|
+
name
|
|
105
|
+
email
|
|
106
|
+
status
|
|
107
|
+
routingOrder
|
|
108
|
+
}
|
|
109
|
+
weldData {
|
|
110
|
+
id
|
|
111
|
+
eid
|
|
112
|
+
isTest
|
|
113
|
+
isComplete
|
|
114
|
+
agents
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
GRAPHQL
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# The mutations with a method of their own on {Client}.
|
|
121
|
+
module Mutations
|
|
122
|
+
module_function
|
|
123
|
+
|
|
124
|
+
# @param response_query [String] the fields to select on the new packet
|
|
125
|
+
# @return [String]
|
|
126
|
+
def create_etch_packet(response_query = DefaultResponseQueries::CREATE_ETCH_PACKET)
|
|
127
|
+
<<~GRAPHQL
|
|
128
|
+
mutation CreateEtchPacket(
|
|
129
|
+
$name: String,
|
|
130
|
+
$organizationEid: String,
|
|
131
|
+
$files: [EtchFile!],
|
|
132
|
+
$isDraft: Boolean,
|
|
133
|
+
$isTest: Boolean,
|
|
134
|
+
$signatureEmailSubject: String,
|
|
135
|
+
$signatureEmailBody: String,
|
|
136
|
+
$signatureProvider: String,
|
|
137
|
+
$signaturePageOptions: JSON,
|
|
138
|
+
$signers: [JSON!],
|
|
139
|
+
$data: JSON,
|
|
140
|
+
$webhookURL: String,
|
|
141
|
+
$replyToName: String,
|
|
142
|
+
$replyToEmail: String,
|
|
143
|
+
$enableEmails: JSON,
|
|
144
|
+
$createCastTemplatesFromUploads: Boolean,
|
|
145
|
+
$duplicateCasts: Boolean,
|
|
146
|
+
$mergePDFs: Boolean,
|
|
147
|
+
$allowUpdates: Boolean
|
|
148
|
+
) {
|
|
149
|
+
createEtchPacket(
|
|
150
|
+
name: $name,
|
|
151
|
+
organizationEid: $organizationEid,
|
|
152
|
+
files: $files,
|
|
153
|
+
isDraft: $isDraft,
|
|
154
|
+
isTest: $isTest,
|
|
155
|
+
signatureEmailSubject: $signatureEmailSubject,
|
|
156
|
+
signatureEmailBody: $signatureEmailBody,
|
|
157
|
+
signatureProvider: $signatureProvider,
|
|
158
|
+
signaturePageOptions: $signaturePageOptions,
|
|
159
|
+
signers: $signers,
|
|
160
|
+
data: $data,
|
|
161
|
+
webhookURL: $webhookURL,
|
|
162
|
+
replyToName: $replyToName,
|
|
163
|
+
replyToEmail: $replyToEmail,
|
|
164
|
+
enableEmails: $enableEmails,
|
|
165
|
+
createCastTemplatesFromUploads: $createCastTemplatesFromUploads,
|
|
166
|
+
duplicateCasts: $duplicateCasts,
|
|
167
|
+
mergePDFs: $mergePDFs,
|
|
168
|
+
allowUpdates: $allowUpdates
|
|
169
|
+
) #{response_query}
|
|
170
|
+
}
|
|
171
|
+
GRAPHQL
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# @param response_query [String] the fields to select on the submission
|
|
175
|
+
# @return [String]
|
|
176
|
+
def forge_submit(response_query = DefaultResponseQueries::FORGE_SUBMIT)
|
|
177
|
+
<<~GRAPHQL
|
|
178
|
+
mutation ForgeSubmit(
|
|
179
|
+
$forgeEid: String!,
|
|
180
|
+
$weldDataEid: String,
|
|
181
|
+
$submissionEid: String,
|
|
182
|
+
$payload: JSON!,
|
|
183
|
+
$enforcePayloadValidOnCreate: Boolean,
|
|
184
|
+
$currentStep: Int,
|
|
185
|
+
$complete: Boolean,
|
|
186
|
+
$isTest: Boolean,
|
|
187
|
+
$timezone: String,
|
|
188
|
+
$webhookURL: String,
|
|
189
|
+
$groupArrayId: String,
|
|
190
|
+
$groupArrayIndex: Int
|
|
191
|
+
) {
|
|
192
|
+
forgeSubmit(
|
|
193
|
+
forgeEid: $forgeEid,
|
|
194
|
+
weldDataEid: $weldDataEid,
|
|
195
|
+
submissionEid: $submissionEid,
|
|
196
|
+
payload: $payload,
|
|
197
|
+
enforcePayloadValidOnCreate: $enforcePayloadValidOnCreate,
|
|
198
|
+
currentStep: $currentStep,
|
|
199
|
+
complete: $complete,
|
|
200
|
+
isTest: $isTest,
|
|
201
|
+
timezone: $timezone,
|
|
202
|
+
webhookURL: $webhookURL,
|
|
203
|
+
groupArrayId: $groupArrayId,
|
|
204
|
+
groupArrayIndex: $groupArrayIndex
|
|
205
|
+
) #{response_query}
|
|
206
|
+
}
|
|
207
|
+
GRAPHQL
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# The mutation returns the URL itself, so it selects no fields.
|
|
211
|
+
#
|
|
212
|
+
# @return [String]
|
|
213
|
+
def generate_etch_sign_url
|
|
214
|
+
<<~GRAPHQL
|
|
215
|
+
mutation GenerateEtchSignURL (
|
|
216
|
+
$signerEid: String!,
|
|
217
|
+
$clientUserId: String!,
|
|
218
|
+
) {
|
|
219
|
+
generateEtchSignURL (
|
|
220
|
+
signerEid: $signerEid,
|
|
221
|
+
clientUserId: $clientUserId
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
GRAPHQL
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# @return [String]
|
|
228
|
+
def remove_weld_data
|
|
229
|
+
<<~GRAPHQL
|
|
230
|
+
mutation RemoveWeldData (
|
|
231
|
+
$eid: String!,
|
|
232
|
+
) {
|
|
233
|
+
removeWeldData (
|
|
234
|
+
eid: $eid,
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
GRAPHQL
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# The queries with a method of their own on {Client}.
|
|
242
|
+
module Queries
|
|
243
|
+
module_function
|
|
244
|
+
|
|
245
|
+
# @param response_query [String] the fields to select on the user
|
|
246
|
+
# @return [String]
|
|
247
|
+
def current_user(response_query = DefaultResponseQueries::CURRENT_USER)
|
|
248
|
+
<<~GRAPHQL
|
|
249
|
+
query CurrentUser {
|
|
250
|
+
currentUser #{response_query}
|
|
251
|
+
}
|
|
252
|
+
GRAPHQL
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# @param response_query [String] the fields to select on the packet
|
|
256
|
+
# @return [String]
|
|
257
|
+
def etch_packet(response_query = DefaultResponseQueries::ETCH_PACKET)
|
|
258
|
+
<<~GRAPHQL
|
|
259
|
+
query EtchPacket (
|
|
260
|
+
$eid: String!,
|
|
261
|
+
) {
|
|
262
|
+
etchPacket (
|
|
263
|
+
eid: $eid,
|
|
264
|
+
) #{response_query}
|
|
265
|
+
}
|
|
266
|
+
GRAPHQL
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "tempfile"
|
|
5
|
+
|
|
6
|
+
module Anvil
|
|
7
|
+
# Encodes a GraphQL operation and its files as a multipart/form-data body,
|
|
8
|
+
# following the GraphQL multipart request spec that the Anvil API implements.
|
|
9
|
+
#
|
|
10
|
+
# The body is assembled in a tempfile rather than a String so that uploading a
|
|
11
|
+
# large PDF does not pull the whole thing into memory.
|
|
12
|
+
#
|
|
13
|
+
# @see https://github.com/jaydenseric/graphql-multipart-request-spec
|
|
14
|
+
module Multipart
|
|
15
|
+
CRLF = "\r\n"
|
|
16
|
+
CHUNK_SIZE = 64 * 1024
|
|
17
|
+
|
|
18
|
+
# A built body, ready to hand to Net::HTTP.
|
|
19
|
+
class Body
|
|
20
|
+
# @return [String] the multipart boundary
|
|
21
|
+
attr_reader :boundary
|
|
22
|
+
# @return [IO] the body, positioned at the start
|
|
23
|
+
attr_reader :io
|
|
24
|
+
# @return [Integer] the body's length in bytes
|
|
25
|
+
attr_reader :content_length
|
|
26
|
+
|
|
27
|
+
def initialize(boundary:, io:, content_length:)
|
|
28
|
+
@boundary = boundary
|
|
29
|
+
@io = io
|
|
30
|
+
@content_length = content_length
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# @return [String] the Content-Type header this body must be sent with
|
|
34
|
+
def content_type
|
|
35
|
+
"multipart/form-data; boundary=#{boundary}"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Releases the tempfile backing the body.
|
|
39
|
+
def close
|
|
40
|
+
io.close! if io.respond_to?(:close!)
|
|
41
|
+
rescue IOError
|
|
42
|
+
nil
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
module_function
|
|
47
|
+
|
|
48
|
+
# @param operations [String] the JSON operation, with files nulled out
|
|
49
|
+
# @param map [String] the JSON map of part names to variable paths
|
|
50
|
+
# @param uploads [Array<UploadWithOptions>] in the same order as the map
|
|
51
|
+
# @return [Body]
|
|
52
|
+
def build(operations:, map:, uploads:)
|
|
53
|
+
boundary = "anvil-#{SecureRandom.hex(16)}"
|
|
54
|
+
io = Tempfile.new("anvil-upload")
|
|
55
|
+
io.binmode
|
|
56
|
+
|
|
57
|
+
write_field(io, boundary, "operations", operations)
|
|
58
|
+
write_field(io, boundary, "map", map)
|
|
59
|
+
uploads.each_with_index { |upload, index| write_file(io, boundary, (index + 1).to_s, upload) }
|
|
60
|
+
io.write("--#{boundary}--#{CRLF}")
|
|
61
|
+
|
|
62
|
+
content_length = io.size
|
|
63
|
+
io.rewind
|
|
64
|
+
Body.new(boundary: boundary, io: io, content_length: content_length)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# @api private
|
|
68
|
+
def write_field(io, boundary, name, value)
|
|
69
|
+
io.write("--#{boundary}#{CRLF}")
|
|
70
|
+
io.write(%(Content-Disposition: form-data; name="#{escape(name)}"#{CRLF}))
|
|
71
|
+
io.write(CRLF)
|
|
72
|
+
io.write(value)
|
|
73
|
+
io.write(CRLF)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @api private
|
|
77
|
+
def write_file(io, boundary, name, upload)
|
|
78
|
+
filename = upload.filename
|
|
79
|
+
raise ConfigurationError, "An upload is missing a filename" if filename.nil? || filename.empty?
|
|
80
|
+
|
|
81
|
+
io.write("--#{boundary}#{CRLF}")
|
|
82
|
+
io.write(
|
|
83
|
+
%(Content-Disposition: form-data; name="#{escape(name)}"; filename="#{escape(filename)}"#{CRLF})
|
|
84
|
+
)
|
|
85
|
+
io.write("Content-Type: #{upload.mimetype}#{CRLF}")
|
|
86
|
+
io.write(CRLF)
|
|
87
|
+
|
|
88
|
+
upload.with_io do |source|
|
|
89
|
+
while (chunk = source.read(CHUNK_SIZE))
|
|
90
|
+
io.write(chunk)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
io.write(CRLF)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Header values cannot carry a newline, and a quote would end the field name.
|
|
98
|
+
#
|
|
99
|
+
# @api private
|
|
100
|
+
def escape(value)
|
|
101
|
+
value.to_s.gsub(/[\r\n]/, "").gsub('"', "%22")
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anvil
|
|
4
|
+
# Typed request payloads.
|
|
5
|
+
#
|
|
6
|
+
# Every client method also accepts a plain Hash with the API's own camelCase
|
|
7
|
+
# keys, so these are a convenience, not a requirement. They give you
|
|
8
|
+
# snake_case names, a readable `inspect`, and an error at the call site
|
|
9
|
+
# instead of a 400 when a name is misspelled.
|
|
10
|
+
#
|
|
11
|
+
# Anything the API accepts that a payload class does not declare can still be
|
|
12
|
+
# passed: unknown keyword arguments are camelized and sent through.
|
|
13
|
+
module Payloads
|
|
14
|
+
# The shared behavior of every request payload.
|
|
15
|
+
class Base
|
|
16
|
+
include Types::Attributes
|
|
17
|
+
|
|
18
|
+
# @return [Hash] keys passed to the constructor that this class does not declare
|
|
19
|
+
attr_reader :extra
|
|
20
|
+
|
|
21
|
+
# @param attributes [Hash] declared attributes by their snake_case name,
|
|
22
|
+
# plus any undeclared keys to pass through
|
|
23
|
+
def initialize(**attributes)
|
|
24
|
+
@extra = {}
|
|
25
|
+
|
|
26
|
+
attributes.each do |name, value|
|
|
27
|
+
definition = self.class.attributes[name.to_sym]
|
|
28
|
+
if definition
|
|
29
|
+
public_send(:"#{name}=", value)
|
|
30
|
+
else
|
|
31
|
+
@extra[self.class.camelize(name)] = value
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @param hash [Hash, Base, nil]
|
|
37
|
+
# @return [Base]
|
|
38
|
+
def self.from_h(hash)
|
|
39
|
+
return hash if hash.is_a?(self)
|
|
40
|
+
return new if hash.nil?
|
|
41
|
+
|
|
42
|
+
by_key = attributes.each_value.to_h { |definition| [definition[:key].to_s, definition[:name]] }
|
|
43
|
+
known = attributes.keys.map(&:to_s)
|
|
44
|
+
|
|
45
|
+
new(**hash.to_h.each_with_object({}) do |(key, value), kwargs|
|
|
46
|
+
name = by_key[key.to_s] || (known.include?(key.to_s) ? key.to_sym : key)
|
|
47
|
+
kwargs[name.to_sym] = value
|
|
48
|
+
end)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The payload as the API wants it: camelCase keys, and attributes that
|
|
52
|
+
# were never set left out entirely rather than sent as null.
|
|
53
|
+
#
|
|
54
|
+
# @return [Hash]
|
|
55
|
+
def to_h
|
|
56
|
+
payload = self.class.attributes.each_with_object({}) do |(_name, definition), hash|
|
|
57
|
+
value = public_send(definition[:name])
|
|
58
|
+
next if value.nil?
|
|
59
|
+
|
|
60
|
+
hash[definition[:key]] = self.class.serialize(value)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
payload.merge(self.class.serialize(extra))
|
|
64
|
+
end
|
|
65
|
+
alias to_hash to_h
|
|
66
|
+
|
|
67
|
+
# @api private
|
|
68
|
+
def self.serialize(value)
|
|
69
|
+
case value
|
|
70
|
+
when Base then value.to_h
|
|
71
|
+
when Array then value.map { |element| serialize(element) }
|
|
72
|
+
when Hash then value.each_with_object({}) { |(key, sub), hash| hash[key] = serialize(sub) }
|
|
73
|
+
when Time then value.iso8601
|
|
74
|
+
else value
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def ==(other)
|
|
79
|
+
other.is_a?(self.class) && other.to_h == to_h
|
|
80
|
+
end
|
|
81
|
+
alias eql? ==
|
|
82
|
+
|
|
83
|
+
def hash
|
|
84
|
+
[self.class, to_h].hash
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def inspect
|
|
88
|
+
"#<#{self.class.name} #{to_h.inspect}>"
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anvil
|
|
4
|
+
module Payloads
|
|
5
|
+
# The variables of a {Anvil::Client#create_etch_packet} call. Every argument
|
|
6
|
+
# is generated from the schema into {Anvil::Operations::CreateEtchPacketArgs};
|
|
7
|
+
# this is the name the client's API uses for it.
|
|
8
|
+
#
|
|
9
|
+
# @see https://www.useanvil.com/docs/api/e-signatures
|
|
10
|
+
class CreateEtchPacket < Anvil::Operations::CreateEtchPacketArgs
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|