faastruby-rpc 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/Gemfile.lock +1 -1
- data/README.md +17 -0
- data/lib/faastruby-rpc/function.rb +38 -33
- data/lib/faastruby-rpc/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7ca42876deb3b89a2ce6f08e5076e920622e7e0ff90d02f97e2c27d80000191
|
4
|
+
data.tar.gz: 4d3ca69fd16b80edf8d2a34d8832491b512e358a4a43f9dbaeb9d482d52c685d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cc828cf2f83f4662261948ad038ca5a547e2a88400a5dfbe0deac4e51e62d7e983e4476a05abe01576847ce6e598368c34c3dd0f75869abfeb3c3ffd5a090ab
|
7
|
+
data.tar.gz: 6f46911239ee2a27399cdceac025ba726ee461888dc0dab2cb704fb33abb9474048b17de1220c5b59e73ad55b86847677d428b648711894bb22681bcf330f034
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 0.2.1 - Jan 3rd 2019
|
4
|
+
- Pass a block when calling a function and the code will be executed when the function responds, and the block's return value will replace the function's response body
|
5
|
+
|
3
6
|
## 0.2.0 - Dec 31 2018
|
4
7
|
- Redesigned UX for calling of external functions
|
5
8
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -59,6 +59,23 @@ You can use positional or keyword arguments when calling external functions, as
|
|
59
59
|
|
60
60
|
This gem is already required when you run your functions in FaaStRuby, or using `faastruby server`.
|
61
61
|
|
62
|
+
## Running code when the invoked function responds
|
63
|
+
If you pass a block when you call another function, the block will execute as soon as the response arrives. For example:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
require_function 'my-workspace/echo', as: 'Echo'
|
67
|
+
def handler(event)
|
68
|
+
name = Echo.call(name: 'john doe') do |response|
|
69
|
+
# response.body #=> "john doe"
|
70
|
+
# response.code #=> 200
|
71
|
+
# response.headers #=> {"content-type"=>"text/plain",...}
|
72
|
+
# What you return from the block will be the value of `name` or `name.body`
|
73
|
+
response.body.capitalize
|
74
|
+
end
|
75
|
+
render text: "Hello, #{name}!" # Will render 'John Doe'
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
62
79
|
## Handling errors
|
63
80
|
|
64
81
|
By default, an exception is raised if the invoked function HTTP status code is greater or equal to 400. This is important to make your functions easier to debug, and you will always know what to expect from that function call.
|
@@ -11,6 +11,9 @@ module FaaStRuby
|
|
11
11
|
@headers = headers
|
12
12
|
@klass = klass
|
13
13
|
end
|
14
|
+
def body=(value)
|
15
|
+
@body = value
|
16
|
+
end
|
14
17
|
end
|
15
18
|
class Function
|
16
19
|
def initialize(path, raise_errors: true)
|
@@ -25,46 +28,44 @@ module FaaStRuby
|
|
25
28
|
}
|
26
29
|
@raise_errors = raise_errors
|
27
30
|
end
|
28
|
-
def call_with(*args)
|
29
|
-
execute(req_body: Oj.dump(args), headers: {'Content-Type' => 'application/json', 'Faastruby-Rpc' => 'true'})
|
30
|
-
end
|
31
31
|
|
32
32
|
def call(*args)
|
33
|
-
|
34
|
-
|
33
|
+
@thread = Thread.new do
|
34
|
+
output = args.any? ? call_with(*args) : execute(method: 'get')
|
35
|
+
@response.body = yield(output) if block_given?
|
36
|
+
end
|
37
|
+
self
|
35
38
|
end
|
36
39
|
|
37
40
|
def execute(req_body: nil, query_params: {}, headers: {}, method: 'post')
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
}
|
58
|
-
end
|
41
|
+
url = "#{FAASTRUBY_HOST}/#{@path}#{convert_query_params(query_params)}"
|
42
|
+
uri = URI.parse(url)
|
43
|
+
use_ssl = uri.scheme == 'https' ? true : false
|
44
|
+
response = fetch(use_ssl: use_ssl, uri: uri, headers: headers, method: @methods[method], req_body: req_body)
|
45
|
+
resp_headers = {}
|
46
|
+
response.each{|k,v| resp_headers[k] = v}
|
47
|
+
case resp_headers['content-type']
|
48
|
+
when 'application/json'
|
49
|
+
begin
|
50
|
+
resp_body = Oj.load(response.body)
|
51
|
+
rescue Oj::ParseError => e
|
52
|
+
if response.body.is_a?(String)
|
53
|
+
resp_body = response.body
|
54
|
+
else
|
55
|
+
raise e if @raise_errors
|
56
|
+
resp_body = {
|
57
|
+
'error' => e.message,
|
58
|
+
'location' => e.backtrace&.first
|
59
|
+
}
|
59
60
|
end
|
60
|
-
when 'application/yaml'
|
61
|
-
resp_body = YAML.load(response.body)
|
62
|
-
else
|
63
|
-
resp_body = response.body
|
64
61
|
end
|
65
|
-
|
66
|
-
|
62
|
+
when 'application/yaml'
|
63
|
+
resp_body = YAML.load(response.body)
|
64
|
+
else
|
65
|
+
resp_body = response.body
|
67
66
|
end
|
67
|
+
raise FaaStRuby::RPC::ExecutionError.new("Function #{@path} returned status code #{response.code} - #{resp_body['error']} - #{resp_body['location']}") if response.code.to_i >= 400 && @raise_errors
|
68
|
+
@response = FaaStRuby::RPC::Response.new(resp_body, response.code.to_i, resp_headers)
|
68
69
|
self
|
69
70
|
end
|
70
71
|
|
@@ -78,7 +79,7 @@ module FaaStRuby
|
|
78
79
|
end
|
79
80
|
|
80
81
|
def to_s
|
81
|
-
body || ""
|
82
|
+
body.to_s || ""
|
82
83
|
end
|
83
84
|
|
84
85
|
def body
|
@@ -103,6 +104,10 @@ module FaaStRuby
|
|
103
104
|
|
104
105
|
private
|
105
106
|
|
107
|
+
def call_with(*args)
|
108
|
+
execute(req_body: Oj.dump(args), headers: {'Content-Type' => 'application/json', 'Faastruby-Rpc' => 'true'})
|
109
|
+
end
|
110
|
+
|
106
111
|
def wait
|
107
112
|
@thread.join
|
108
113
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faastruby-rpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paulo Arruda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|