xrpc 0.0.4 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/xrpc/client.rb +28 -110
- data/lib/xrpc/server.rb +37 -36
- data/lib/xrpc/version.rb +1 -1
- data/lib/xrpc.rb +0 -1
- metadata +3 -4
- data/lib/xrpc/basic.rb +0 -69
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7bca4020ba6ae0ace083ebbd62860273590ff6d135d712fd4c221233554913f
|
4
|
+
data.tar.gz: 60ed91a5872c8008a2bb6bd64ea1543e1fdf670c822752eb0b9efe99126e7f96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db08c3afd3532daf9aebc12c2bd32db20512654707a31d32068470f780eba97602e20b6b6bf052eb194bdf08f0d4f469175fe89d824d330ffcf52813c294dd17
|
7
|
+
data.tar.gz: c00e3688d413fceec7ea3638f78d8d6bebc234934861fd2afda79b550551b913206124e4c5dc15eeebe8aeff43a047ac3071713b6293d0113b5f198fe71bc26c
|
data/lib/xrpc/client.rb
CHANGED
@@ -1,130 +1,48 @@
|
|
1
|
-
require "uri"
|
2
1
|
require "httparty"
|
3
2
|
require "json"
|
4
3
|
|
5
4
|
module XRPC
|
6
|
-
class
|
7
|
-
def
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
def call(endpoint, params = {}, body = nil)
|
12
|
-
endpoint_uri = URI("#{@pds}/xrpc/#{endpoint}?#{URI.encode_www_form(params)}")
|
13
|
-
headers = { 'Content-Type': "application/json" }
|
14
|
-
|
15
|
-
response = if body
|
16
|
-
HTTParty.post(endpoint_uri, body: body.to_json, headers: headers)
|
17
|
-
else
|
18
|
-
HTTParty.get(endpoint_uri)
|
19
|
-
end
|
20
|
-
|
21
|
-
{ encoding: response.headers["Content-Type"], body: JSON.parse(response.body) }
|
5
|
+
class << self
|
6
|
+
def client(url)
|
7
|
+
XRPC::Client.new(url)
|
22
8
|
end
|
23
9
|
end
|
24
|
-
end
|
25
|
-
|
26
|
-
module XRPC
|
27
|
-
class Endpoint
|
28
|
-
attr_reader :id, :lexicon
|
29
10
|
|
30
|
-
|
31
|
-
|
32
|
-
@lexicon = lexicon
|
33
|
-
end
|
11
|
+
class Client
|
12
|
+
attr_reader :get, :post
|
34
13
|
|
35
|
-
def
|
36
|
-
|
14
|
+
def initialize(base_url, token = nil)
|
15
|
+
@headers = { :"Content-Type" => "application/json" }
|
16
|
+
@headers = @headers.merge { :Authorization => "Bearer #{token}" } unless token.nil?
|
17
|
+
@base_url = base_url
|
18
|
+
@get = GetRequest.new(base_url, @headers)
|
19
|
+
@post = PostRequest.new(base_url, @headers)
|
37
20
|
end
|
38
|
-
end
|
39
|
-
end
|
40
21
|
|
41
|
-
|
42
|
-
|
43
|
-
def initialize(lexicon)
|
44
|
-
@lexicon = lexicon
|
45
|
-
end
|
22
|
+
class GetRequest
|
23
|
+
include HTTParty
|
46
24
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
module XRPC
|
54
|
-
class Client
|
55
|
-
def initialize
|
56
|
-
@lexicons = []
|
57
|
-
end
|
25
|
+
def initialize(base_url, headers)
|
26
|
+
self.class.base_uri base_url
|
27
|
+
@headers = headers
|
28
|
+
end
|
58
29
|
|
59
|
-
|
60
|
-
|
30
|
+
def method_missing(method_name, **params)
|
31
|
+
JSON.parse self.class.get("/xrpc/#{method_name.to_s.gsub("_", ".")}", query: params, headers: @headers).body
|
32
|
+
end
|
61
33
|
end
|
62
34
|
|
63
|
-
|
64
|
-
|
65
|
-
end
|
35
|
+
class PostRequest
|
36
|
+
include HTTParty
|
66
37
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
38
|
+
def initialize(base_url, headers)
|
39
|
+
self.class.base_uri base_url
|
40
|
+
@headers = headers
|
41
|
+
end
|
71
42
|
|
72
|
-
|
43
|
+
def method_missing(method_name, **params)
|
44
|
+
JSON.parse self.class.post("/xrpc/#{method_name.to_s.gsub("_", ".")}", body: params.to_json, headers: @headers).body
|
73
45
|
end
|
74
46
|
end
|
75
47
|
end
|
76
48
|
end
|
77
|
-
|
78
|
-
# # example usage
|
79
|
-
# client = XRPC::Client.new
|
80
|
-
|
81
|
-
# client.add_lexicon({
|
82
|
-
# lexicon: 1,
|
83
|
-
# id: 'io.example.ping',
|
84
|
-
# defs: {
|
85
|
-
# main: {
|
86
|
-
# type: 'query',
|
87
|
-
# description: 'Ping the server',
|
88
|
-
# parameters: {
|
89
|
-
# type: 'params',
|
90
|
-
# properties: { message: { type: 'string' } }
|
91
|
-
# },
|
92
|
-
# output: {
|
93
|
-
# encoding: 'application/json',
|
94
|
-
# schema: {
|
95
|
-
# type: 'object',
|
96
|
-
# required: ['message'],
|
97
|
-
# properties: { message: { type: 'string' } }
|
98
|
-
# }
|
99
|
-
# }
|
100
|
-
# }
|
101
|
-
# }
|
102
|
-
# })
|
103
|
-
|
104
|
-
# client.add_lexicon({
|
105
|
-
# lexicon: 1,
|
106
|
-
# id: 'io.example.writeJsonFile',
|
107
|
-
# defs: {
|
108
|
-
# main: {
|
109
|
-
# type: 'procedure',
|
110
|
-
# description: 'Write a JSON file',
|
111
|
-
# parameters: {
|
112
|
-
# type: 'params',
|
113
|
-
# properties: { fileName: { type: 'string' } }
|
114
|
-
# },
|
115
|
-
# input: {
|
116
|
-
# encoding: 'application/json'
|
117
|
-
# }
|
118
|
-
# }
|
119
|
-
# }
|
120
|
-
# })
|
121
|
-
|
122
|
-
# # call endpoint with query parameters and input body
|
123
|
-
# res1 = client.service('https://example.com').call('io.example.writeJsonFile', { fileName: 'foo.json' }, { hello: 'world', thisIs: 'the file to write' })
|
124
|
-
# puts res1
|
125
|
-
# # => { encoding: 'application/json', body: nil }
|
126
|
-
|
127
|
-
# # call endpoint with query parameters only
|
128
|
-
# res2 = client.endpoint('io.example.ping').call(client.service('https://example.com'), { message: 'hello world' })
|
129
|
-
# puts res2
|
130
|
-
# # => { encoding: 'application/json', body: { message: 'hello world' } }
|
data/lib/xrpc/server.rb
CHANGED
@@ -1,43 +1,44 @@
|
|
1
|
-
require "
|
2
|
-
|
3
|
-
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
1
|
+
require "sinatra/base"
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
module XRPCRoutes
|
5
|
+
def xrpc_get(lexicon, &block)
|
6
|
+
# Extract the parameter names from the block's parameters
|
7
|
+
parameters = block.parameters.map { |type, name| name.to_s }
|
8
|
+
|
9
|
+
# Define a new route with the provided lexicon and block
|
10
|
+
get "/xrpc/#{lexicon}" do
|
11
|
+
content_type :json
|
12
|
+
# Create a hash of arguments with parameter names as keys
|
13
|
+
args = {}
|
14
|
+
parameters.each do |param|
|
15
|
+
args[param] = params[param] if params[param]
|
16
|
+
end
|
17
|
+
|
18
|
+
# Call the block with the arguments
|
19
|
+
instance_exec(args, &block)
|
16
20
|
end
|
17
21
|
end
|
18
22
|
|
19
|
-
def
|
20
|
-
#
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
def find_endpoint(id)
|
35
|
-
@lexicons.each do |lexicon|
|
36
|
-
endpoint = lexicon.endpoint(id)
|
37
|
-
return endpoint if endpoint
|
23
|
+
def xrpc_post(lexicon, &block)
|
24
|
+
# Extract the parameter names from the block's parameters
|
25
|
+
parameters = block.parameters.map { |type, name| name.to_s }
|
26
|
+
|
27
|
+
# Define a new route with the provided lexicon and block
|
28
|
+
post "/xrpc/#{lexicon}" do
|
29
|
+
content_type :json
|
30
|
+
# Create a hash of arguments with parameter names as keys
|
31
|
+
args = {}
|
32
|
+
parameters.each do |param|
|
33
|
+
args[param] = params[param] if params[param]
|
34
|
+
end
|
35
|
+
|
36
|
+
# Call the block with the arguments
|
37
|
+
instance_exec(args, &block)
|
38
38
|
end
|
39
|
-
|
40
|
-
nil
|
41
39
|
end
|
42
40
|
end
|
41
|
+
|
42
|
+
# Register the module to make it available as a macro
|
43
|
+
register XRPCRoutes
|
43
44
|
end
|
data/lib/xrpc/version.rb
CHANGED
data/lib/xrpc.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xrpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shreyan Jain
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -60,7 +60,6 @@ extensions: []
|
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
62
|
- "./lib/xrpc.rb"
|
63
|
-
- "./lib/xrpc/basic.rb"
|
64
63
|
- "./lib/xrpc/client.rb"
|
65
64
|
- "./lib/xrpc/server.rb"
|
66
65
|
- "./lib/xrpc/version.rb"
|
@@ -83,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
82
|
- !ruby/object:Gem::Version
|
84
83
|
version: '0'
|
85
84
|
requirements: []
|
86
|
-
rubygems_version: 3.4.
|
85
|
+
rubygems_version: 3.4.15
|
87
86
|
signing_key:
|
88
87
|
specification_version: 4
|
89
88
|
summary: Interact with bsky/atproto using Ruby
|
data/lib/xrpc/basic.rb
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
# typed: false
|
2
|
-
require "uri"
|
3
|
-
require "httparty"
|
4
|
-
require "json"
|
5
|
-
|
6
|
-
module XRPC
|
7
|
-
def request(pds, endpoint_location, params)
|
8
|
-
EasyEndpoint.new(pds, endpoint_location).get(params)
|
9
|
-
end
|
10
|
-
|
11
|
-
module_function :request
|
12
|
-
|
13
|
-
class EasyEndpoint
|
14
|
-
attr_reader :request_uri
|
15
|
-
|
16
|
-
def initialize(pds, endpoint_location, authenticated: false, token: nil)
|
17
|
-
@pds = pds
|
18
|
-
@endpoint_location = endpoint_location
|
19
|
-
@authenticated = authenticated
|
20
|
-
@headers = default_headers()
|
21
|
-
if token # Ideally, you shouldn't pass the token when creating the endpoint
|
22
|
-
@headers = default_authenticated_headers(token)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def default_headers
|
27
|
-
{ "Content-Type" => "application/json" }
|
28
|
-
end
|
29
|
-
|
30
|
-
def authenticated?() @authenticated end
|
31
|
-
|
32
|
-
def default_authenticated_headers(access_token)
|
33
|
-
default_headers.merge({
|
34
|
-
Authorization: "Bearer #{access_token}",
|
35
|
-
})
|
36
|
-
end
|
37
|
-
|
38
|
-
def authenticate(token) # This is the proper place to authenticate with a token
|
39
|
-
# This is still a pretty weird way to authenticate, but it works (for now)
|
40
|
-
if not @authenticated == true
|
41
|
-
raise Error, "Non-authenticated endpoint cannot be authenticated"
|
42
|
-
end
|
43
|
-
@headers = default_authenticated_headers(token)
|
44
|
-
end
|
45
|
-
|
46
|
-
def get(params)
|
47
|
-
query_params = URI.encode_www_form(params) # e.g. "foo=bar&baz=qux" from (foo: "bar", baz: "qux")
|
48
|
-
@request_uri = URI("#{@pds}/xrpc/#{@endpoint_location}?#{query_params}")
|
49
|
-
response = HTTParty.get(@request_uri, headers: @headers)
|
50
|
-
JSON.parse(response.body)
|
51
|
-
end
|
52
|
-
|
53
|
-
def post(params)
|
54
|
-
@request_uri = URI("#{@pds}/xrpc/#{@endpoint_location}")
|
55
|
-
response = HTTParty.post(@request_uri, body: params.to_json, headers: @headers)
|
56
|
-
|
57
|
-
if response then JSON.parse(response.body) end
|
58
|
-
end
|
59
|
-
|
60
|
-
def post_without_response(params)
|
61
|
-
@request_uri = URI("#{@pds}/xrpc/#{@endpoint_location}")
|
62
|
-
response = HTTParty.post(@request_uri, body: params.to_json, headers: @headers)
|
63
|
-
|
64
|
-
response.body if response
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
class Error < StandardError; end
|
69
|
-
end
|