AppDelegate.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright 2018, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import UIKit
  17. import SwiftGRPC
  18. let GOOGLE_API_KEY = ""
  19. @UIApplicationMain
  20. class AppDelegate: UIResponder, UIApplicationDelegate {
  21. var window: UIWindow?
  22. var service: Google_Cloud_Language_V1_LanguageServiceServiceClient!
  23. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  24. // Set up an empty window.
  25. self.window = UIWindow(frame:UIScreen.main.bounds);
  26. self.window?.rootViewController = UIViewController();
  27. self.window?.makeKeyAndVisible();
  28. // Signal test start.
  29. signal(UIColor.yellow);
  30. // Prepare the API client.
  31. service = Google_Cloud_Language_V1_LanguageServiceServiceClient(address: "language.googleapis.com")
  32. service.metadata = Metadata(["x-goog-api-key": GOOGLE_API_KEY])
  33. // Call the API.
  34. var document = Google_Cloud_Language_V1_Document()
  35. document.type = .plainText
  36. document.content = "The Caterpillar and Alice looked at each other for some time in silence: at last the Caterpillar took the hookah out of its mouth, and addressed her in a languid, sleepy voice. `Who are you?' said the Caterpillar."
  37. var features = Google_Cloud_Language_V1_AnnotateTextRequest.Features()
  38. features.extractSyntax = true
  39. features.extractEntities = true
  40. features.extractDocumentSentiment = true
  41. features.extractEntitySentiment = true
  42. features.classifyText = true
  43. var request = Google_Cloud_Language_V1_AnnotateTextRequest()
  44. request.document = document
  45. request.features = features
  46. print("REQUEST: \(request)")
  47. do {
  48. let _ = try service.annotateText(request) {response, callresult in
  49. print("RESULT: \(callresult)")
  50. if let response = response {
  51. print("RESPONSE: \(response)")
  52. self.signal(UIColor.green);
  53. } else {
  54. self.signal(UIColor.red);
  55. }
  56. }
  57. } catch {
  58. print("ERROR: \(error)")
  59. signal(UIColor.red)
  60. }
  61. return true
  62. }
  63. func signal(_ c : UIColor) {
  64. DispatchQueue.main.async {
  65. self.window?.rootViewController?.view?.backgroundColor = c;
  66. }
  67. }
  68. }