e621_openapi 1.0.385
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/README.md +35 -0
- data/lib/e621_openapi/client.rb +144 -0
- data/lib/e621_openapi/enums.rb +15 -0
- data/lib/e621_openapi/error.rb +25 -0
- data/lib/e621_openapi/operations.rb +2216 -0
- data/lib/e621_openapi/spec.rb +19 -0
- data/lib/e621_openapi/spec_commit.rb +6 -0
- data/lib/e621_openapi/version.rb +6 -0
- data/lib/e621_openapi.rb +13 -0
- data/sig/e621_openapi/enums.rbs +15 -0
- data/sig/e621_openapi/operations.rbs +1517 -0
- data/sig/e621_openapi/types.rbs +319 -0
- data/sig/e621_openapi.rbs +66 -0
- metadata +56 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c4f608c1f6835c817c0474cd2a3baa968e8330be40853b0bb137656b06cf09ca
|
|
4
|
+
data.tar.gz: 3fa9077884d876d682211433e1851be1894f5e04c096ef88505f97166fcaaffe
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: '058d13aa7c550526f5602a983ed6c8ff957c876812f4808456074085ec4e86304d9dde0847c24fd2970ff73de95403928e03592bb49d339906d62e23e171ebba'
|
|
7
|
+
data.tar.gz: 817fa534182c8a7553fae08c44c5c41dde4dc30ffe01bcd5cfeac764715bb5d9425da39a411178b332230f0a6467f300767bf4a6781ecb15980ae093c63d8c5c
|
data/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# e621_openapi
|
|
2
|
+
|
|
3
|
+
RBS types and a client for the [e621.net](https://e621.net) API, generated from the [E621OpenAPI](https://github.com/DonovanDMC/E621OpenAPI) spec. A new version is published every time the spec is updated.
|
|
4
|
+
|
|
5
|
+
Generated from spec commit [`1450ed9`](https://github.com/DonovanDMC/E621OpenAPI/blob/1450ed9b8dcf4dd775f52fefbebb2a6956150d56/openapi.yaml).
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
require "e621_openapi"
|
|
9
|
+
|
|
10
|
+
client = E621OpenAPI::Client.new(
|
|
11
|
+
user_agent: "MyProject/1.0 (by username on e621)",
|
|
12
|
+
# optional, see https://e621.wiki/authentication/
|
|
13
|
+
username: "username",
|
|
14
|
+
api_key: "api_key"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
posts = client.posts_index(tags: "fox", limit: 10)
|
|
18
|
+
client.uploads_create(upload: { file: File.open("fox.png", "rb"), tag_string: "fox", rating: "s" })
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
There's a method for each operation, named after its operationId (`posts#index` is `posts_index`). Path parameters are positional, and everything else is a keyword argument. Bracketed parameters are nested hashes, so `search[name_matches]` is `search: { name_matches: "..." }`, and arrays like `pool[post_ids][]` take an `Array`. Pass `base_url: "https://e926.net"` to use e926.
|
|
22
|
+
|
|
23
|
+
Responses are returned as hashes with symbol keys. Empty responses and redirects return `nil`, and anything else that isn't a 2xx raises `E621OpenAPI::Error`, which has the `status`, the parsed `body`, and the `request` and `response`.
|
|
24
|
+
|
|
25
|
+
## Types
|
|
26
|
+
|
|
27
|
+
Every operation and schema has an [RBS](https://github.com/ruby/rbs) signature, with the schemas as record types under `E621OpenAPI::Types`, like `E621OpenAPI::Types::basic_post`. They're in the gem's `sig` directory, so [Steep](https://github.com/soutaro/steep) picks them up with `library "e621_openapi"` in your Steepfile.
|
|
28
|
+
|
|
29
|
+
Some responses are a union of the formats they can return, like `posts_show`, which depends on `mode` and `v1`. Check the shape (`post.key?(:post)`) or use a type annotation to narrow them.
|
|
30
|
+
|
|
31
|
+
## Spec
|
|
32
|
+
|
|
33
|
+
The commit the gem was generated from is `E621OpenAPI::SPEC_COMMIT`, and `E621OpenAPI.fetch_spec` returns the spec at that commit as YAML. Pass `dereferenced: true` for the version with every `$ref` inlined. The URLs are `E621OpenAPI::SPEC_URL` and `E621OpenAPI::DEREFERENCED_SPEC_URL`.
|
|
34
|
+
|
|
35
|
+
Issues with endpoints, parameters, or response types should be reported to [E621OpenAPI](https://github.com/DonovanDMC/E621OpenAPI/issues).
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "securerandom"
|
|
6
|
+
require "uri"
|
|
7
|
+
|
|
8
|
+
module E621OpenAPI
|
|
9
|
+
# A client for the e621.net API, with a method for each operation.
|
|
10
|
+
#
|
|
11
|
+
# client = E621OpenAPI::Client.new(user_agent: "MyProject/1.0 (by username on e621)")
|
|
12
|
+
# client.posts_index(tags: "fox", limit: 10)
|
|
13
|
+
class Client
|
|
14
|
+
include Operations
|
|
15
|
+
|
|
16
|
+
DEFAULT_BASE_URL = "https://e621.net"
|
|
17
|
+
|
|
18
|
+
REQUESTS = {
|
|
19
|
+
get: Net::HTTP::Get,
|
|
20
|
+
post: Net::HTTP::Post,
|
|
21
|
+
put: Net::HTTP::Put,
|
|
22
|
+
patch: Net::HTTP::Patch,
|
|
23
|
+
delete: Net::HTTP::Delete
|
|
24
|
+
}.freeze
|
|
25
|
+
|
|
26
|
+
attr_reader :base_url, :user_agent
|
|
27
|
+
|
|
28
|
+
# +user_agent+ is required by e621, see https://e621.wiki/#section/User-Agent.
|
|
29
|
+
# +username+ and +api_key+ are optional, see https://e621.wiki/authentication/.
|
|
30
|
+
def initialize(user_agent:, username: nil, api_key: nil, base_url: DEFAULT_BASE_URL, open_timeout: 10, read_timeout: 30)
|
|
31
|
+
raise ArgumentError, "username and api_key must be given together" if username.nil? != api_key.nil?
|
|
32
|
+
|
|
33
|
+
@user_agent = user_agent
|
|
34
|
+
@username = username
|
|
35
|
+
@api_key = api_key
|
|
36
|
+
@base_url = URI(base_url)
|
|
37
|
+
@host = @base_url.host or raise ArgumentError, "base_url must have a host"
|
|
38
|
+
@open_timeout = open_timeout
|
|
39
|
+
@read_timeout = read_timeout
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def inspect = "#<#{self.class} base_url=#{@base_url} username=#{@username.inspect}>"
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def request(method, path, query: nil, body: nil, body_type: nil)
|
|
47
|
+
uri = @base_url.dup
|
|
48
|
+
uri.path = path
|
|
49
|
+
pairs = flatten(query)
|
|
50
|
+
uri.query = URI.encode_www_form(pairs) unless pairs.empty?
|
|
51
|
+
|
|
52
|
+
req = REQUESTS.fetch(method).new(uri)
|
|
53
|
+
req["User-Agent"] = @user_agent
|
|
54
|
+
req["Accept"] = "application/json"
|
|
55
|
+
username, api_key = @username, @api_key
|
|
56
|
+
req.basic_auth(username, api_key) if username && api_key
|
|
57
|
+
encode_body(req, body, body_type) if body_type
|
|
58
|
+
|
|
59
|
+
response = Net::HTTP.start(@host, uri.port, use_ssl: uri.scheme == "https", open_timeout: @open_timeout, read_timeout: @read_timeout) do |http|
|
|
60
|
+
http.request(req)
|
|
61
|
+
end
|
|
62
|
+
handle(req, response)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def encode_body(req, body, body_type)
|
|
66
|
+
case body_type
|
|
67
|
+
when "application/json"
|
|
68
|
+
req["Content-Type"] = body_type
|
|
69
|
+
req.body = JSON.generate(compact(body))
|
|
70
|
+
when "multipart/form-data"
|
|
71
|
+
boundary = "E621OpenAPI#{SecureRandom.hex(16)}"
|
|
72
|
+
req.content_type = "#{body_type}; boundary=#{boundary}"
|
|
73
|
+
req.body = multipart(flatten(body), boundary)
|
|
74
|
+
else
|
|
75
|
+
req.content_type = body_type
|
|
76
|
+
req.body = URI.encode_www_form(flatten(body))
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def handle(req, response)
|
|
81
|
+
body = parse(response)
|
|
82
|
+
case response
|
|
83
|
+
when Net::HTTPSuccess then body
|
|
84
|
+
# the spec only lists redirects for actions that succeeded
|
|
85
|
+
when Net::HTTPRedirection then nil
|
|
86
|
+
else raise Error.new(req, response, body)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# built as binary, so files with non UTF-8 bytes can be joined with the rest
|
|
91
|
+
def multipart(pairs, boundary)
|
|
92
|
+
body = String.new(encoding: Encoding::BINARY)
|
|
93
|
+
pairs.each do |name, value|
|
|
94
|
+
body << "--#{boundary}\r\nContent-Disposition: form-data; name=\"#{name}\""
|
|
95
|
+
if file?(value)
|
|
96
|
+
body << "; filename=\"#{filename(value)}\"\r\nContent-Type: application/octet-stream\r\n\r\n" << value.read.to_s.b
|
|
97
|
+
else
|
|
98
|
+
body << "\r\n\r\n" << value.to_s.b
|
|
99
|
+
end
|
|
100
|
+
body << "\r\n"
|
|
101
|
+
end
|
|
102
|
+
body << "--#{boundary}--\r\n"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def parse(response)
|
|
106
|
+
text = response.body
|
|
107
|
+
return nil if text.nil? || text.empty?
|
|
108
|
+
return text unless response.content_type == "application/json"
|
|
109
|
+
|
|
110
|
+
JSON.parse(text, symbolize_names: true)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# { search: { id: 1 }, post_ids: [1, 2] } => [["search[id]", "1"], ["post_ids[]", "1"], ["post_ids[]", "2"]]
|
|
114
|
+
def flatten(params, prefix = nil, pairs = [])
|
|
115
|
+
params&.each do |key, value|
|
|
116
|
+
name = prefix ? "#{prefix}[#{key}]" : key.to_s
|
|
117
|
+
case value
|
|
118
|
+
when nil then next
|
|
119
|
+
when Hash then flatten(value, name, pairs)
|
|
120
|
+
when Array then value.each { pairs << ["#{name}[]", param_value(_1)] }
|
|
121
|
+
else pairs << [name, param_value(value)]
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
pairs
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def param_value(value) = file?(value) ? value : value.to_s
|
|
128
|
+
|
|
129
|
+
def compact(value)
|
|
130
|
+
case value
|
|
131
|
+
when Hash then value.filter_map { |k, v| [k, compact(v)] unless v.nil? }.to_h
|
|
132
|
+
when Array then value.map { compact(_1) }
|
|
133
|
+
else value
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def file?(value) = value.respond_to?(:read)
|
|
138
|
+
|
|
139
|
+
# quotes would end the header
|
|
140
|
+
def filename(io) = (io.respond_to?(:path) && io.path ? File.basename(io.path) : "file").delete("\"\r\n")
|
|
141
|
+
|
|
142
|
+
def escape(value) = URI.encode_www_form_component(value.to_s).gsub("+", "%20")
|
|
143
|
+
end
|
|
144
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Generated by scripts/generate.rb from the E621OpenAPI spec, don't edit by hand.
|
|
2
|
+
|
|
3
|
+
module E621OpenAPI
|
|
4
|
+
module TagCategory
|
|
5
|
+
GENERAL = 0
|
|
6
|
+
ARTIST = 1
|
|
7
|
+
CONTRIBUTOR = 2
|
|
8
|
+
COPYRIGHT = 3
|
|
9
|
+
CHARACTER = 4
|
|
10
|
+
SPECIES = 5
|
|
11
|
+
INVALID = 6
|
|
12
|
+
META = 7
|
|
13
|
+
LORE = 8
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module E621OpenAPI
|
|
4
|
+
# Raised for any response that isn't a 2xx or a documented redirect.
|
|
5
|
+
class Error < StandardError
|
|
6
|
+
attr_reader :request, :response, :status, :body
|
|
7
|
+
|
|
8
|
+
def initialize(request, response, body)
|
|
9
|
+
@request = request
|
|
10
|
+
@response = response
|
|
11
|
+
@status = response.code.to_i
|
|
12
|
+
@body = body
|
|
13
|
+
message = message_from(body)
|
|
14
|
+
super("#{request.method} #{request.path} failed with #{@status}#{": #{message}" if message}")
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def message_from(body)
|
|
20
|
+
return body[:message] || body[:reason] || body[:errors]&.to_s if body.is_a?(Hash)
|
|
21
|
+
|
|
22
|
+
body unless body.to_s.empty? || body.to_s.start_with?("<")
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|