Browse Source

Update makeRequestHead to use add(contentsOf:) (#469)

George Barnett 6 years ago
parent
commit
1559827285
3 changed files with 18 additions and 24 deletions
  1. 2 2
      Package.resolved
  2. 1 1
      Package.swift
  3. 15 21
      Sources/SwiftGRPCNIO/ClientCalls/BaseClientCall.swift

+ 2 - 2
Package.resolved

@@ -24,8 +24,8 @@
         "repositoryURL": "https://github.com/apple/swift-nio.git",
         "state": {
           "branch": null,
-          "revision": "b2be49ada3d2cf3824e038505be32230d6e62893",
-          "version": "2.1.1"
+          "revision": "ed50f8a41ece8db20afa8d1188fc0960f95263df",
+          "version": "2.2.0"
         }
       },
       {

+ 1 - 1
Package.swift

@@ -26,7 +26,7 @@ var packageDependencies: [Package.Dependency] = [
 
   // SwiftGRPCNIO dependencies:
   // Main SwiftNIO package
-  .package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
+  .package(url: "https://github.com/apple/swift-nio.git", from: "2.2.0"),
   // HTTP2 via SwiftNIO
   .package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.2.0"),
   // TLS via SwiftNIO

+ 15 - 21
Sources/SwiftGRPCNIO/ClientCalls/BaseClientCall.swift

@@ -247,30 +247,24 @@ extension BaseClientCall {
   ///   - callOptions: options to use when configuring this call.
   /// - Returns: `HTTPRequestHead` configured for this call.
   internal func makeRequestHead(path: String, host: String, callOptions: CallOptions) -> HTTPRequestHead {
-    let method: HTTPMethod = callOptions.cacheable ? .GET : .POST
-    var requestHead = HTTPRequestHead(version: .init(major: 2, minor: 0), method: method, uri: path)
-
-    callOptions.customMetadata.forEach { name, value in
-      requestHead.headers.add(name: name, value: value)
-    }
-
-    // We're dealing with HTTP/1; the NIO HTTP2ToHTTP1Codec replaces "host" with ":authority".
-    requestHead.headers.add(name: "host", value: host)
-
-    requestHead.headers.add(name: "content-type", value: "application/grpc")
-
-    // Used to detect incompatible proxies, as per https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
-    requestHead.headers.add(name: "te", value: "trailers")
-
-    //! FIXME: Add a more specific user-agent, see: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#user-agents
-    requestHead.headers.add(name: "user-agent", value: "grpc-swift-nio")
-
-    requestHead.headers.add(name: GRPCHeaderName.acceptEncoding, value: CompressionMechanism.acceptEncodingHeader)
+    var headers: HTTPHeaders = [
+      // We're dealing with HTTP/1; the NIO HTTP2ToHTTP1Codec replaces "host" with ":authority".
+      "host": host,
+      "content-type": "application/grpc",
+      // Used to detect incompatible proxies, as per https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
+      "te": "trailers",
+      //! FIXME: Add a more specific user-agent, see: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#user-agents
+      "user-agent": "grpc-swift-nio",
+      GRPCHeaderName.acceptEncoding: CompressionMechanism.acceptEncodingHeader,
+    ]
 
     if callOptions.timeout != .infinite {
-      requestHead.headers.add(name: GRPCHeaderName.timeout, value: String(describing: callOptions.timeout))
+      headers.add(name: GRPCHeaderName.timeout, value: String(describing: callOptions.timeout))
     }
 
-    return requestHead
+    headers.add(contentsOf: callOptions.customMetadata)
+
+    let method: HTTPMethod = callOptions.cacheable ? .GET : .POST
+    return HTTPRequestHead(version: .init(major: 2, minor: 0), method: method, uri: path, headers: headers)
   }
 }