-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontext.rb
More file actions
150 lines (125 loc) · 3.66 KB
/
context.rb
File metadata and controls
150 lines (125 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true
#
# Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "date"
module FDK
# Config looks up values in the env vars
class Config
def [](key)
ENV[key.upcase]
end
end
# Represents inbound HTTP headers
class InHeaders
def initialize(headers, key_fn)
@headers = headers
@key_fn = key_fn
end
def header_key(key)
key = @key_fn.call(key) if @key_fn
key.downcase
end
def [](key)
h = @headers[header_key(key)]
return h[0] unless h.nil?
end
def each(&block)
@headers.each(&block)
end
end
# Represents outbound HTTP headers
class OutHeaders < InHeaders
def initialize(headers, key_in_fn)
headers["Fn-Fdk-Version"] = ["fdk-ruby/#{FDK::VERSION}"]
headers["Fn-Fdk-Runtime"] = ["ruby/#{RUBY_VERSION}"]
super(headers, key_in_fn)
end
def []=(key, value)
if value.is_a? Array
h = []
value.each { |x| h.push(x.to_s) }
@headers[header_key(key)] = h
else
@headers[header_key(key)] = [value.to_s]
end
end
def delete(key)
@headers.delete header_key(key)
end
end
# Represents the Fn context for a function execution
class Context
# FN_APP_ID -the ID of the application that this function is a member of.
# FN_APP_NAME - the name of the application.
# FN_CALL_ID - a unique ID for each function execution.
# FN_FN_ID - the ID of this function
# FN_MEMORY - a number representing the amount of memory available to the call, in MB
# $X - any configuration values you've set for the Application.
# Replace X with the upper cased name of the config variable you set.
# e.g. minio_secret=secret will be exposed via MINIO_SECRET env var.
attr_reader :headers, :response_headers
def initialize(headers_in, headers_out)
@headers = headers_in
@response_headers = headers_out
@config ||= Config.new
end
def call_id
@headers["fn-call-id"]
end
def app_id
@config["FN_APP_ID"]
end
def fn_id
@config["FN_FN_ID"]
end
def deadline
DateTime.iso8601(@headers["fn-deadline"])
end
def memory
@config["FN_MEMORY"].to_i
end
def content_type
@headers["content-type"]
end
def http_context
HTTPContext.new(self)
end
end
# Represents the context data (inbound && outbound)
# for the execution passed as HTTP headers
class HTTPContext
attr_reader :headers, :response_headers
def initialize(ctx)
fn_http_h_ = "fn-http-h-"
@ctx = ctx
http_headers = {}
ctx.headers.each do |k, v|
http_headers[k.sub(fn_http_h_, "")] = v if k.downcase.start_with?(fn_http_h_)
end
@headers = InHeaders.new(http_headers, nil)
@response_headers = OutHeaders.new(ctx.response_headers, ->(s) { fn_http_h_ + s })
end
def request_url
@ctx.headers["fn-http-request-url"]
end
def method
@ctx.headers["fn-http-method"]
end
def status_code=(val)
@ctx.response_headers["fn-http-status"] = val.to_i
end
end
end