Skip to content

Commit

Permalink
http request body conversion + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SalehAlbuga committed Nov 23, 2019
1 parent c29a758 commit 3645893
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
9 changes: 3 additions & 6 deletions Sources/AzureFunctions/Bindings/http/HttpRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions Sources/AzureFunctions/Bindings/http/HttpResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 29 additions & 8 deletions Sources/AzureFunctions/RpcConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

}

0 comments on commit 3645893

Please sign in to comment.