@@ -30,11 +30,10 @@ import NIO
30
30
import NIOHTTP1
31
31
32
32
/// An HTTP based client for SCF Runtime Engine. This encapsulates the RESTful methods exposed by the runtime engine:
33
- /// * /runtime/invocation/next
34
- /// * /runtime/invocation/response
35
- /// * /runtime/invocation/error
36
- /// * /runtime/init/ready
37
- /// * /runtime/init/error
33
+ /// - POST /runtime/init/ready
34
+ /// - GET /runtime/invocation/next
35
+ /// - POST /runtime/invocation/response
36
+ /// - POST /runtime/invocation/error
38
37
extension SCF {
39
38
internal struct RuntimeClient {
40
39
private let eventLoop : EventLoop
@@ -48,7 +47,7 @@ extension SCF {
48
47
49
48
/// Requests invocation from the API server.
50
49
func getNextInvocation( logger: Logger ) -> EventLoopFuture < ( Invocation , ByteBuffer ) > {
51
- let url = Consts . getNextInvocationURL
50
+ let url = Endpoint . getNextInvocation
52
51
logger. debug ( " requesting work from scf runtime engine using \( url) " )
53
52
return self . httpClient. get ( url: url, headers: RuntimeClient . defaultHeaders) . flatMapThrowing { response in
54
53
guard response. status == . ok else {
@@ -79,15 +78,12 @@ extension SCF {
79
78
80
79
switch result {
81
80
case . success( let buffer) :
82
- url = Consts . postResponseURL
81
+ url = Endpoint . postResponse
83
82
body = buffer
84
83
headers = RuntimeClient . defaultHeaders
85
84
case . failure( let error) :
86
- url = Consts . postErrorURL
87
- let errorResponse = ErrorResponse ( errorType: Consts . functionError, errorMessage: " \( error) " )
88
- let bytes = errorResponse. toJSONBytes ( )
89
- body = self . allocator. buffer ( capacity: bytes. count)
90
- body!. writeBytes ( bytes)
85
+ url = Endpoint . postError
86
+ body = self . allocator. buffer ( string: " \( error) " )
91
87
headers = RuntimeClient . errorHeaders
92
88
}
93
89
logger. debug ( " reporting results to scf runtime engine using \( url) " )
@@ -110,7 +106,7 @@ extension SCF {
110
106
111
107
/// Reports initialization ready to the runtime engine.
112
108
func reportInitializationReady( logger: Logger ) -> EventLoopFuture < Void > {
113
- let url = Consts . postInitReadyURL
109
+ let url = Endpoint . postInitReady
114
110
logger. info ( " reporting initialization ready to scf runtime engine using \( url) " )
115
111
return self . httpClient. post ( url: url, headers: RuntimeClient . defaultHeaders, body: . init( string: " " ) ) . flatMapThrowing { response in
116
112
guard response. status == . ok else {
@@ -129,31 +125,6 @@ extension SCF {
129
125
}
130
126
}
131
127
132
- /// Reports an initialization error to the runtime engine.
133
- func reportInitializationError( logger: Logger , error: Error ) -> EventLoopFuture < Void > {
134
- let url = Consts . postInitErrorURL
135
- let errorResponse = ErrorResponse ( errorType: Consts . initializationError, errorMessage: " \( error) " )
136
- let bytes = errorResponse. toJSONBytes ( )
137
- var body = self . allocator. buffer ( capacity: bytes. count)
138
- body. writeBytes ( bytes)
139
- logger. warning ( " reporting initialization error to scf runtime engine using \( url) " )
140
- return self . httpClient. post ( url: url, headers: RuntimeClient . errorHeaders, body: body) . flatMapThrowing { response in
141
- guard response. status == . ok else {
142
- throw RuntimeError . badStatusCode ( response. status)
143
- }
144
- return ( )
145
- } . flatMapErrorThrowing { error in
146
- switch error {
147
- case HTTPClient . Errors. timeout:
148
- throw RuntimeError . upstreamError ( " timeout " )
149
- case HTTPClient . Errors. connectionResetByPeer:
150
- throw RuntimeError . upstreamError ( " connectionResetByPeer " )
151
- default :
152
- throw error
153
- }
154
- }
155
- }
156
-
157
128
/// Cancels the current request if one is already running. Only needed for debugging purposes.
158
129
func cancel( ) {
159
130
self . httpClient. cancel ( )
@@ -172,24 +143,6 @@ internal extension SCF {
172
143
}
173
144
}
174
145
175
- internal struct ErrorResponse : Codable {
176
- var errorType : String
177
- var errorMessage : String
178
- }
179
-
180
- internal extension ErrorResponse {
181
- func toJSONBytes( ) -> [ UInt8 ] {
182
- var bytes = [ UInt8] ( )
183
- bytes. append ( UInt8 ( ascii: " { " ) )
184
- bytes. append ( contentsOf: #""errorType":"# . utf8)
185
- self . errorType. encodeAsJSONString ( into: & bytes)
186
- bytes. append ( contentsOf: #","errorMessage":"# . utf8)
187
- self . errorMessage. encodeAsJSONString ( into: & bytes)
188
- bytes. append ( UInt8 ( ascii: " } " ) )
189
- return bytes
190
- }
191
- }
192
-
193
146
extension SCF {
194
147
internal struct Invocation {
195
148
let requestID : String
0 commit comments