DatastoreViewController.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. import AppKit
  34. import gRPC
  35. import QuickProto
  36. let Host = "datastore.googleapis.com:443"
  37. class DatastoreViewController : NSViewController, NSTextFieldDelegate {
  38. @IBOutlet weak var messageField: NSTextField!
  39. @IBOutlet weak var outputField: NSTextField!
  40. private var fileDescriptorSet : FileDescriptorSet
  41. private var client: Client?
  42. private var call: Call?
  43. private var webViewWindowController : WebViewWindowController!
  44. required init?(coder:NSCoder) {
  45. fileDescriptorSet = FileDescriptorSet(filename: "descriptors.out")
  46. super.init(coder:coder)
  47. }
  48. var enabled = false
  49. var authClient : OAuthClient!
  50. @IBAction func messageReturnPressed(sender: NSTextField) {
  51. if enabled {
  52. callServer(address:Host)
  53. }
  54. }
  55. override func viewDidLoad() {
  56. gRPC.initialize()
  57. authClient = OAuthClient()
  58. let url = authClient.authCodeURL(state:"123")!
  59. webViewWindowController = WebViewWindowController(windowNibName:"WebViewWindow")
  60. webViewWindowController.url = url
  61. webViewWindowController.completion = { (code) in
  62. self.authClient.exchangeCode(code:code)
  63. }
  64. webViewWindowController.loadWindow()
  65. }
  66. override func viewDidAppear() {
  67. // prevent the UI from trying to send messages until gRPC is initialized
  68. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  69. self.enabled = true
  70. }
  71. }
  72. func createClient(address: String, host: String) {
  73. client = Client(address:address, certificates:nil, host:nil)
  74. }
  75. func callServer(address:String) {
  76. let requestHost = Host
  77. let authorization = "Bearer " + authClient.token!
  78. let requestMetadata = Metadata(["authorization":authorization])
  79. if let requestMessage = self.fileDescriptorSet.createMessage("RunQueryRequest") {
  80. requestMessage.addField("project_id", value:"hello-86")
  81. let gqlQuery = self.fileDescriptorSet.createMessage("GqlQuery")
  82. var query = self.messageField.stringValue
  83. if query == "" {
  84. query = "select * from Person"
  85. }
  86. gqlQuery?.addField("query_string", value:query)
  87. requestMessage.addField("gql_query", value:gqlQuery)
  88. let requestMessageData = requestMessage.data()
  89. let check = self.fileDescriptorSet.readMessage("RunQueryRequest", data:requestMessageData)
  90. check?.display()
  91. createClient(address:address, host:requestHost)
  92. guard let client = client else {
  93. return
  94. }
  95. call = client.createCall(host: requestHost,
  96. method: "/google.datastore.v1.Datastore/RunQuery",
  97. timeout: 30.0)
  98. guard let call = call else {
  99. return
  100. }
  101. _ = call.performNonStreamingCall(messageData: requestMessageData,
  102. metadata: requestMetadata)
  103. { (callResult) in
  104. print("Received status: \(callResult.statusCode): \(callResult.statusMessage)")
  105. if let messageData = callResult.resultData,
  106. let responseMessage = self.fileDescriptorSet.readMessage("RunQueryResponse",
  107. data:messageData) {
  108. responseMessage.display()
  109. responseMessage.forOneField("text") {(field) in
  110. DispatchQueue.main.async {
  111. self.outputField.stringValue = field.string()
  112. }
  113. }
  114. } else {
  115. DispatchQueue.main.async {
  116. self.outputField.stringValue = "No message received. gRPC Status \(callResult.statusCode): \(callResult.statusMessage)"
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }