diff --git a/Sources/AzureFunctions/Bindings/http/HttpRequest.swift b/Sources/AzureFunctions/Bindings/http/HttpRequest.swift index 1297a33..5cac852 100644 --- a/Sources/AzureFunctions/Bindings/http/HttpRequest.swift +++ b/Sources/AzureFunctions/Bindings/http/HttpRequest.swift @@ -37,17 +37,14 @@ public final class HttpRequest : Binding { internal init(fromRpcHttp: AzureFunctionsRpcMessages_RpcHttp) { - self.method = fromRpcHttp.method self.url = fromRpcHttp.url self.headers = fromRpcHttp.headers self.query = fromRpcHttp.query - let data = fromRpcHttp.body.bytes - self.body = data - - let rawData = fromRpcHttp.rawBody.bytes - self.rawBody = rawData + self.body = RpcConverter.fromBodyTypedData(data: fromRpcHttp.body) + self.rawBody = RpcConverter.fromBodyTypedData(data: fromRpcHttp.rawBody) + } struct Keys { diff --git a/Sources/AzureFunctions/Bindings/http/HttpResponse.swift b/Sources/AzureFunctions/Bindings/http/HttpResponse.swift index 9b677d2..eb45772 100644 --- a/Sources/AzureFunctions/Bindings/http/HttpResponse.swift +++ b/Sources/AzureFunctions/Bindings/http/HttpResponse.swift @@ -14,15 +14,16 @@ public final class HttpResponse: Binding { public init () { self.name = "" + self.headers["X-Powered-By"] = "SwiftFunc" } - public init(name: String) { - self.name = name + public init(bindingName: String) { + self.name = bindingName } func toRpcHttp() -> AzureFunctionsRpcMessages_RpcHttp { var rpc = AzureFunctionsRpcMessages_RpcHttp() - rpc.statusCode = "\(statusCode ?? 500)" + rpc.statusCode = "\(statusCode ?? 200)" rpc.headers = self.headers if let data = body { diff --git a/Sources/AzureFunctions/RpcConverter.swift b/Sources/AzureFunctions/RpcConverter.swift index c94ec99..e10ce29 100644 --- a/Sources/AzureFunctions/RpcConverter.swift +++ b/Sources/AzureFunctions/RpcConverter.swift @@ -11,7 +11,7 @@ internal final class RpcConverter { static func toRpcTypedData(obj: Any) -> AzureFunctionsRpcMessages_TypedData { var td = AzureFunctionsRpcMessages_TypedData() -// Logger.log(message: obj) + // Logger.log(message: obj) switch obj { case let string as String: if string.starts(with: "{") || string.starts(with: "[") { // TODO detect JSON in str @@ -72,15 +72,15 @@ internal final class RpcConverter { converted = httpReq break case let .some(.json(jsonStr)): - Logger.log("TD JSON \(jsonStr)") + Logger.log("TD JSON \(jsonStr)") if let data = jsonStr.data(using: .utf8) { do { - converted = try JSONSerialization.jsonObject(with: data, options: []) -// if jsonStr.starts(with: "[") { -// converted = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] -// } else { -// converted = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] -// } + converted = try JSONSerialization.jsonObject(with: data, options: []) + // if jsonStr.starts(with: "[") { + // converted = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] + // } else { + // converted = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] + // } } catch { Logger.log(error.localizedDescription) // throw exception throw FunctionError.JSONSerializationException(error.localizedDescription) @@ -126,4 +126,25 @@ internal final class RpcConverter { } + static func fromBodyTypedData(data: AzureFunctionsRpcMessages_TypedData) -> Data? { + var converted: Data? = nil + + switch data.data { + case let .some(.stream(data)): + converted = data + break + case let .some(.bytes(data)): + converted = data + break + case let .some(.json(str)), let .some(.string(str)): + if let data = str.data(using: .utf8) { + converted = data + break + } + default: + break + } + return converted + } + }