Browse Source

Merge pull request #152 from grpc/example-updates

Update examples to build with the current version of SwiftGRPC.
Tim Burks 7 years ago
parent
commit
81d522e1de

+ 1 - 2
.travis.yml

@@ -43,5 +43,4 @@ script:
   - make test
   - cd Plugin
   - make test
-  - cd ../ExampleTests
-  - make
+  - cd ..

+ 0 - 11
ExampleTests/Makefile

@@ -1,11 +0,0 @@
-
-all:
-	swift build -c release
-
-project:
-	swift package generate-xcodeproj
-
-clean :
-	rm -rf Packages googleapis .build
-	rm -f Package.pins
-	rm -rf Package.resolved ExampleTests.xcodeproj ExampleTests

+ 0 - 34
ExampleTests/Package.swift

@@ -1,34 +0,0 @@
-// swift-tools-version:4.0
-
-/*
- * Copyright 2017, gRPC Authors 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.
- */
-import PackageDescription
-
-let package = Package(
-  name: "ExampleTests",
-  dependencies: [
-    .package(url: "../", .branch("HEAD")),
-    .package(url: "https://github.com/apple/swift-protobuf.git", from: "1.0.2"),
-    .package(url: "https://github.com/kylef/Commander.git", from: "0.8.0")
-  ],
-  targets: [
-    .target(name: "Echo",
-            dependencies: [
-              "SwiftGRPC",
-              "SwiftProtobuf",
-              "Commander"
-            ])
-  ])

+ 0 - 1
ExampleTests/Sources/Echo

@@ -1 +0,0 @@
-../../Examples/Echo/PackageManager/Sources/

+ 20 - 9
Examples/Echo2/Package.swift

@@ -1,5 +1,7 @@
+// swift-tools-version:4.0
+
 /*
- * Copyright 2016, gRPC Authors All rights reserved.
+ * Copyright 2017, gRPC Authors 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.
@@ -14,11 +16,20 @@
  * limitations under the License.
  */
 import PackageDescription
-let package = Package (
-    name: "Echo",
-    dependencies: [
-        .Package(url: "https://github.com/grpc/grpc-swift.git", Version(0,3,3)),
-        .Package(url: "https://github.com/apple/swift-protobuf.git", Version(1,0,2)),
-        .Package(url: "https://github.com/kylef/Commander.git", Version(0,8,0)),
-    ]
-)
+
+let package = Package(
+  name: "Echo",
+  dependencies: [
+    .package(url: "../..", .branch("HEAD")),
+    .package(url: "https://github.com/apple/swift-protobuf.git", from: "1.0.2"),
+    .package(url: "https://github.com/kylef/Commander.git", from: "0.8.0")
+  ],
+  targets: [
+    .target(name: "Echo",
+            dependencies: [
+              "SwiftGRPC",
+              "SwiftProtobuf",
+              "Commander"
+            ],
+	    path: "Sources")
+  ])

+ 8 - 5
Examples/Echo2/Sources/main.swift

@@ -33,17 +33,17 @@ let messageOption = Option("message",
 
 // Helper function for client actions
 func buildEchoService(_ ssl: Bool, _ address: String, _ port: String, _: String)
-  -> Echo_EchoService {
-  var service: Echo_EchoService
+  -> Echo_EchoServiceClient {
+  var service: Echo_EchoServiceClient
   if ssl {
     let certificateURL = URL(fileURLWithPath: "ssl.crt")
     let certificates = try! String(contentsOf: certificateURL)
-    service = Echo_EchoService(address: address + ":" + port,
+    service = Echo_EchoServiceClient(address: address + ":" + port,
                                certificates: certificates,
                                host: "example.com")
     service.host = "example.com"
   } else {
-    service = Echo_EchoService(address: address + ":" + port, secure: false)
+    service = Echo_EchoServiceClient(address: address + ":" + port, secure: false)
   }
   service.metadata = Metadata([
     "x-goog-api-key": "YOUR_API_KEY",
@@ -53,7 +53,10 @@ func buildEchoService(_ ssl: Bool, _ address: String, _ port: String, _: String)
 }
 
 Group {
-  $0.command("serve", sslFlag, addressOption("0.0.0.0"), portOption,
+  $0.command("serve",
+             sslFlag,
+             addressOption("0.0.0.0"),
+             portOption,
              description: "Run an echo server.") { ssl, address, port in
     let sem = DispatchSemaphore(value: 0)
     let echoProvider = EchoProvider()

+ 21 - 9
Examples/Google/Datastore/Package.swift

@@ -1,3 +1,5 @@
+// swift-tools-version:4.0
+
 /*
  * Copyright 2017, gRPC Authors All rights reserved.
  *
@@ -14,12 +16,22 @@
  * limitations under the License.
  */
 import PackageDescription
-let package = Package (
-    name: "Datastore",
-    dependencies: [
-        .Package(url: "https://github.com/grpc/grpc-swift.git", Version(0,3,3)),
-        .Package(url: "https://github.com/apple/swift-protobuf.git", Version(1,0,2)),
-        .Package(url: "https://github.com/google/auth-library-swift.git", Version(0,3,6)),
-	.Package(url: "https://github.com/kylef/Commander.git", Version(0,8,0)),
-    ]
-)
+
+let package = Package(
+  name: "Datastore",
+  dependencies: [
+    .package(url: "../../..", .branch("HEAD")),
+    .package(url: "https://github.com/apple/swift-protobuf.git", from: "1.0.2"),
+    .package(url: "https://github.com/kylef/Commander.git", from: "0.8.0"),
+    .package(url: "https://github.com/google/auth-library-swift.git", from: "0.3.6")
+  ],
+  targets: [
+    .target(name: "Datastore",
+            dependencies: [
+              "SwiftGRPC",
+              "SwiftProtobuf",
+              "Commander",
+	      "OAuth2"
+            ],
+	    path: "Sources")
+  ])

+ 4 - 1
Examples/Google/Datastore/RUNME

@@ -8,7 +8,10 @@
 # is installed in $HOME/local/bin.
 
 if [ ! -d "googleapis" ]; then
-  git clone http://github.com/googleapis/googleapis
+  curl -L -O https://github.com/googleapis/googleapis/archive/master.zip
+  unzip master.zip
+  rm -f master.zip
+  mv googleapis-master googleapis
 fi
 
 protoc \

+ 2 - 2
Examples/Google/Datastore/Sources/main.swift

@@ -49,7 +49,7 @@ class PropertiesDecoder {
 // a Swift interface to the Google Cloud Datastore API
 class Datastore {
   var projectID: String
-  var service: Google_Datastore_V1_DatastoreService!
+  var service: Google_Datastore_V1_DatastoreServiceClient!
 
   let scopes = ["https://www.googleapis.com/auth/datastore"]
 
@@ -75,7 +75,7 @@ class Datastore {
     }
     // Initialize gRPC service
     gRPC.initialize()
-    service = Google_Datastore_V1_DatastoreService(address: "datastore.googleapis.com")
+    service = Google_Datastore_V1_DatastoreServiceClient(address: "datastore.googleapis.com")
     service.metadata = Metadata(["authorization": "Bearer " + authToken])
   }
 

+ 21 - 8
Examples/Google/NaturalLanguage/Package.swift

@@ -1,3 +1,5 @@
+// swift-tools-version:4.0
+
 /*
  * Copyright 2017, gRPC Authors All rights reserved.
  *
@@ -14,11 +16,22 @@
  * limitations under the License.
  */
 import PackageDescription
-let package = Package (
-    name: "NaturalLanguage",
-    dependencies: [
-        .Package(url: "https://github.com/grpc/grpc-swift.git", Version(0,3,3)),
-        .Package(url: "https://github.com/apple/swift-protobuf.git", Version(1,0,2)),
-        .Package(url: "https://github.com/google/auth-library-swift.git", Version(0,3,6)),
-    ]
-)
+
+let package = Package(
+  name: "NaturalLanguage",
+  dependencies: [
+    .package(url: "../../..", .branch("HEAD")),
+    .package(url: "https://github.com/apple/swift-protobuf.git", from: "1.0.2"),
+    .package(url: "https://github.com/kylef/Commander.git", from: "0.8.0"),
+    .package(url: "https://github.com/google/auth-library-swift.git", from: "0.3.6")
+  ],
+  targets: [
+    .target(name: "NaturalLanguage",
+            dependencies: [
+              "SwiftGRPC",
+              "SwiftProtobuf",
+              "Commander",
+	      "OAuth2"
+            ],
+	    path: "Sources")
+  ])

+ 4 - 1
Examples/Google/NaturalLanguage/RUNME

@@ -8,7 +8,10 @@
 # is installed in $HOME/local/bin.
 
 if [ ! -d "googleapis" ]; then
-  git clone http://github.com/googleapis/googleapis
+  curl -L -O https://github.com/googleapis/googleapis/archive/master.zip
+  unzip master.zip
+  rm -f master.zip
+  mv googleapis-master googleapis
 fi
 
 protoc \

+ 1 - 1
Examples/Google/NaturalLanguage/Sources/main.swift

@@ -31,7 +31,7 @@ if let provider = DefaultTokenProvider(scopes: scopes) {
         exit(-1)
       }
 
-      let service = Google_Cloud_Language_V1_LanguageServiceService(address: "language.googleapis.com")
+      let service = Google_Cloud_Language_V1_LanguageServiceServiceClient(address: "language.googleapis.com")
 
       service.metadata = Metadata(["authorization": "Bearer " + authToken])
 

+ 21 - 9
Examples/Google/Spanner/Package.swift

@@ -1,3 +1,5 @@
+// swift-tools-version:4.0
+
 /*
  * Copyright 2017, gRPC Authors All rights reserved.
  *
@@ -14,12 +16,22 @@
  * limitations under the License.
  */
 import PackageDescription
-let package = Package (
-    name: "Spanner",
-    dependencies: [
-        .Package(url: "https://github.com/grpc/grpc-swift.git", Version(0,3,3)),
-        .Package(url: "https://github.com/apple/swift-protobuf.git", Version(1,0,2)),
-        .Package(url: "https://github.com/google/auth-library-swift.git", Version(0,3,6)),
-	.Package(url: "https://github.com/kylef/Commander.git", Version(0,8,0)),
-    ]
-)
+
+let package = Package(
+  name: "Spanner",
+  dependencies: [
+    .package(url: "../../..", .branch("HEAD")),
+    .package(url: "https://github.com/apple/swift-protobuf.git", from: "1.0.2"),
+    .package(url: "https://github.com/kylef/Commander.git", from: "0.8.0"),
+    .package(url: "https://github.com/google/auth-library-swift.git", from: "0.3.6")
+  ],
+  targets: [
+    .target(name: "Spanner",
+            dependencies: [
+              "SwiftGRPC",
+              "SwiftProtobuf",
+              "Commander",
+	      "OAuth2"
+            ],
+	    path: "Sources")
+  ])

+ 4 - 1
Examples/Google/Spanner/RUNME

@@ -8,7 +8,10 @@
 # is installed in $HOME/local/bin.
 
 if [ ! -d "googleapis" ]; then
-  git clone http://github.com/googleapis/googleapis
+  curl -L -O https://github.com/googleapis/googleapis/archive/master.zip
+  unzip master.zip
+  rm -f master.zip
+  mv googleapis-master googleapis
 fi
 
 protoc \

+ 18 - 8
Examples/Simple/PackageManager/Package.swift

@@ -1,5 +1,7 @@
+// swift-tools-version:4.0
+
 /*
- * Copyright 2016, gRPC Authors All rights reserved.
+ * Copyright 2017, gRPC Authors 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.
@@ -14,10 +16,18 @@
  * limitations under the License.
  */
 import PackageDescription
-let package = Package (
-    name: "Simple",
-    dependencies: [
-        .Package(url: "https://github.com/grpc/grpc-swift.git", Version(0,3,3)),
-        .Package(url: "https://github.com/kylef/Commander.git", Version(0,8,0)),
-    ]
-)
+
+let package = Package(
+  name: "Simple",
+  dependencies: [
+    .package(url: "../../..", .branch("HEAD")),
+    .package(url: "https://github.com/kylef/Commander.git", from: "0.8.0")
+  ],
+  targets: [
+    .target(name: "Simple",
+            dependencies: [
+              "SwiftGRPC",
+              "Commander"
+            ],
+	    path: ".")
+  ])

+ 20 - 20
Examples/Simple/PackageManager/main.swift

@@ -23,7 +23,7 @@ let host = "foo.test.google.fr"
 
 func client() throws {
   let message = "hello, server!".data(using: .utf8)
-  let c = gRPC.Channel(address: address, secure: false)
+  let c = Channel(address: address, secure: false)
   let steps = 3
   for i in 0..<steps {
     let sem = DispatchSemaphore(value: 0)
@@ -33,9 +33,9 @@ func client() throws {
     let call = c.makeCall(method)
 
     let metadata = Metadata([
-      ["x": "xylophone"],
-      ["y": "yu"],
-      ["z": "zither"]
+      "x": "xylophone",
+      "y": "yu",
+      "z": "zither"
     ])
 
     try! call.start(.unary, metadata: metadata, message: message) {
@@ -48,12 +48,12 @@ func client() throws {
 
       let initialMetadata = response.initialMetadata!
       for i in 0..<initialMetadata.count() {
-        print("INITIAL METADATA ->", initialMetadata.key(i), ":", initialMetadata.value(i))
+        print("INITIAL METADATA ->", initialMetadata.key(i)!, ":", initialMetadata.value(i)!)
       }
 
       let trailingMetadata = response.trailingMetadata!
       for i in 0..<trailingMetadata.count() {
-        print("TRAILING METADATA ->", trailingMetadata.key(i), ":", trailingMetadata.value(i))
+        print("TRAILING METADATA ->", trailingMetadata.key(i)!, ":", trailingMetadata.value(i)!)
       }
       sem.signal()
     }
@@ -63,7 +63,7 @@ func client() throws {
 }
 
 func server() throws {
-  let server = gRPC.Server(address: address)
+  let server = Server(address: address)
   var requestCount = 0
 
   let sem = DispatchSemaphore(value: 0)
@@ -73,20 +73,20 @@ func server() throws {
     do {
       requestCount += 1
 
-      print("\(requestCount): Received request " + requestHandler.host
-        + " " + requestHandler.method
-        + " from " + requestHandler.caller)
+      print("\(requestCount): Received request " + requestHandler.host!
+          + " " + String(describing:requestHandler.method) 
+          + " from " + String(describing:requestHandler.caller))
 
       let initialMetadata = requestHandler.requestMetadata
       for i in 0..<initialMetadata.count() {
-        print("\(requestCount): Received initial metadata -> " + initialMetadata.key(i)
-          + ":" + initialMetadata.value(i))
+        print("\(requestCount): Received initial metadata -> " + initialMetadata.key(i)!
+          + ":" + initialMetadata.value(i)!)
       }
 
       let initialMetadataToSend = Metadata([
-        ["a": "Apple"],
-        ["b": "Banana"],
-        ["c": "Cherry"]
+        "a": "Apple",
+        "b": "Banana",
+        "c": "Cherry"
       ])
       try requestHandler.receiveMessage(initialMetadata: initialMetadataToSend) { messageData in
         let messageString = String(data: messageData!, encoding: .utf8)
@@ -100,9 +100,9 @@ func server() throws {
 
       let replyMessage = "hello, client!"
       let trailingMetadataToSend = Metadata([
-        ["0": "zero"],
-        ["1": "one"],
-        ["2": "two"]
+        "0": "zero",
+        "1": "one",
+        "2": "two"
       ])
       try requestHandler.sendResponse(message: replyMessage.data(using: .utf8)!,
                                       statusCode: .ok,
@@ -125,13 +125,13 @@ func server() throws {
 Group {
   $0.command("server") {
     gRPC.initialize()
-    print("gRPC version", gRPC.version())
+    print("gRPC version", gRPC.version!)
     try server()
   }
 
   $0.command("client") {
     gRPC.initialize()
-    print("gRPC version", gRPC.version())
+    print("gRPC version", gRPC.version!)
     try client()
   }
 

+ 4 - 0
Makefile

@@ -8,9 +8,13 @@ all:
 test:
 	swift build -v $(CFLAGS)
 	swift test -v $(CFLAGS)
+	cd Examples/Echo/PackageManager; make
+	cd Examples/Simple/PackageManager; make
 
 clean:
 	rm -rf Packages
 	rm -rf .build
 	rm -rf SwiftGRPC.xcodeproj
 	rm -rf Package.pins Package.resolved
+	cd Examples/Echo/PackageManager; make clean
+	cd Examples/Simple/PackageManager; make clean