xrpc 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/xrpc/basic.rb +69 -0
- data/lib/xrpc/client.rb +130 -0
- data/lib/xrpc/server.rb +43 -0
- data/lib/xrpc/version.rb +1 -1
- data/lib/xrpc.rb +2 -55
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e48e69127fd8c456bc554411ace1c1eb8226f9ccf771b678f9904e64a0effc9e
|
4
|
+
data.tar.gz: 34c1a233e67351045321b1548bf73c795d0ca158325b6aef8be18dffe0f1d5a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f0d689245a0db39090ed23a78f1be0c42668357cf0bddd93890dc107f899655b22473c94bf53a7b5aa31e92c818c839f57cbe9986cf0dbcefedeb3b1f581999
|
7
|
+
data.tar.gz: 648b7559cf78e5bd53acb20fe6b2d6abb0c9bc4462aa7a46ac752e5c22c463ebfa6f0f9fed65b318a11b8192da8c1188f695aebb5f5e217185918f0ca8dc03a7
|
data/lib/xrpc/basic.rb
ADDED
@@ -0,0 +1,69 @@
|
|
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
|
data/lib/xrpc/client.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
require "uri"
|
2
|
+
require "httparty"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module XRPC
|
6
|
+
class Service
|
7
|
+
def initialize(pds)
|
8
|
+
@pds = pds
|
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) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module XRPC
|
27
|
+
class Endpoint
|
28
|
+
attr_reader :id, :lexicon
|
29
|
+
|
30
|
+
def initialize(id, lexicon)
|
31
|
+
@id = id
|
32
|
+
@lexicon = lexicon
|
33
|
+
end
|
34
|
+
|
35
|
+
def call(service, params = {}, body = nil)
|
36
|
+
service.call(@id, params, body)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module XRPC
|
42
|
+
class Lexicon
|
43
|
+
def initialize(lexicon)
|
44
|
+
@lexicon = lexicon
|
45
|
+
end
|
46
|
+
|
47
|
+
def endpoint(id)
|
48
|
+
Endpoint.new(id, @lexicon)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module XRPC
|
54
|
+
class Client
|
55
|
+
def initialize
|
56
|
+
@lexicons = []
|
57
|
+
end
|
58
|
+
|
59
|
+
def add_lexicon(lexicon)
|
60
|
+
@lexicons << Lexicon.new(lexicon)
|
61
|
+
end
|
62
|
+
|
63
|
+
def service(pds)
|
64
|
+
Service.new(pds)
|
65
|
+
end
|
66
|
+
|
67
|
+
def endpoint(id)
|
68
|
+
@lexicons.each do |lexicon|
|
69
|
+
endpoint = lexicon.endpoint(id)
|
70
|
+
return endpoint if endpoint
|
71
|
+
|
72
|
+
nil
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
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
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "json"
|
2
|
+
require "rack"
|
3
|
+
|
4
|
+
module XRPC
|
5
|
+
class Server
|
6
|
+
def initialize(lexicons)
|
7
|
+
@lexicons = lexicons
|
8
|
+
end
|
9
|
+
|
10
|
+
def method(id)
|
11
|
+
# endpoint = find_endpoint(id)
|
12
|
+
# raise ArgumentError, "Endpoint '#{id}' not found" unless endpoint
|
13
|
+
|
14
|
+
define_singleton_method(id) do |input, **params|
|
15
|
+
endpoint.call(input, params)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def decode_params(nsid, query_params)
|
20
|
+
# logic to decode query parameters based on nsid
|
21
|
+
# For simplicity, let's assume no decoding is needed and return the query params as is
|
22
|
+
query_params
|
23
|
+
end
|
24
|
+
|
25
|
+
def call(nsid, input, **params)
|
26
|
+
endpoint = find_endpoint(nsid)
|
27
|
+
raise ArgumentError, "Endpoint '#{nsid}' not found" unless endpoint
|
28
|
+
|
29
|
+
endpoint.call(input, params)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def find_endpoint(id)
|
35
|
+
@lexicons.each do |lexicon|
|
36
|
+
endpoint = lexicon.endpoint(id)
|
37
|
+
return endpoint if endpoint
|
38
|
+
end
|
39
|
+
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/xrpc/version.rb
CHANGED
data/lib/xrpc.rb
CHANGED
@@ -1,55 +1,2 @@
|
|
1
|
-
|
2
|
-
require "
|
3
|
-
require "httparty"
|
4
|
-
require "json"
|
5
|
-
|
6
|
-
module XRPC
|
7
|
-
def request(pds, endpoint_location, params)
|
8
|
-
Endpoint.new(pds, endpoint_location).get(params)
|
9
|
-
end
|
10
|
-
|
11
|
-
module_function :request
|
12
|
-
|
13
|
-
class Endpoint
|
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
|
-
end
|
53
|
-
|
54
|
-
class Error < StandardError; end
|
55
|
-
end
|
1
|
+
require "xrpc/client"
|
2
|
+
require "xrpc/basic"
|
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.0.
|
4
|
+
version: 0.0.4
|
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-05-
|
11
|
+
date: 2023-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -60,6 +60,9 @@ extensions: []
|
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
62
|
- "./lib/xrpc.rb"
|
63
|
+
- "./lib/xrpc/basic.rb"
|
64
|
+
- "./lib/xrpc/client.rb"
|
65
|
+
- "./lib/xrpc/server.rb"
|
63
66
|
- "./lib/xrpc/version.rb"
|
64
67
|
homepage: https://github.com/ShreyanJain9/xrpc
|
65
68
|
licenses:
|