SHARE
TWEET

Untitled

a guest Oct 4th, 2017 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Copyright IBM Corporation 2016,2017
  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.  
  17. import Kitura
  18. import SwiftyJSON
  19. import LoggerAPI
  20. import CloudEnvironment
  21. import Health
  22. import PersonalityInsightsV3
  23. import Foundation
  24. import Alamofire
  25.  
  26. public class Controller {
  27.  
  28.   public let router: Router
  29.   let cloudEnv: CloudEnv
  30.   let health: Health
  31.  
  32.   public var port: Int {
  33.     get { return cloudEnv.port }
  34.   }
  35.  
  36.   public var url: String {
  37.     get { return cloudEnv.url }
  38.   }
  39.  
  40.   public init() {
  41.     // Create CloudEnv instance
  42.     cloudEnv = CloudEnv()
  43.  
  44.     // All web apps need a Router instance to define routes
  45.     router = Router()
  46.    
  47.     // Instance of health for reporting heath check values
  48.     health = Health()
  49.  
  50.     // Serve static content from "public"
  51.     router.all("/", middleware: StaticFileServer())
  52.  
  53.     // Basic GET request
  54.     router.get("/hello", handler: getHello)
  55.  
  56.     // Basic POST request
  57.     router.post("/hello", handler: postHello)
  58.  
  59.     // JSON Get request
  60.     router.get("/json", handler: getJSON)
  61.    
  62.     // Basic application health check
  63.     router.get("/health", handler: getHealthCheck)
  64.   }
  65.  
  66.   /**
  67.   * Handler for getting a text/plain response.
  68.   */
  69.   public func getHello(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws {
  70.    
  71. let headers: HTTPHeaders = [
  72.     "Authorization": "Basic NDFkNGI3MzctYzdiMi00N2U3LWIxOTctZTQ4ZDAyM2NlZWQ3OkhKa25xdkRUNnYzMQ==",
  73.     "Content-Type": "application/json"
  74. ]
  75. let parameters: Parameters = [
  76.     "text": "While GE’s Predix platform has been promoted on TV and covered in the New York Times and other leading publications, none of the press coverage has explained concretely what technology GE is building with Predix. This story will do just that based on my visit to the recent Predix Transform conference, interviews with people building applications using Predix, and work done creating content to explain Predix for GE. While GE’s Predix platform has been promoted on TV and covered in the New York Times and other leading publications, none of the press coverage has explained concretely what technology GE is building with Predix. This story will do just that based on my visit to the recent Predix Transform conference, interviews with people building applications using Predix, and work done creating content to explain Predix for GE."
  77. ]
  78. Alamofire.request("https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21",method: post, headers: headers,parameters:parameters).responseJSON { response in
  79.     print(response)
  80. }
  81. // let username = "a86fdfae-0aeb-45e2-a5ee-2abcef27e879"
  82. // let password = "CbwTsQxxGCZQ"
  83. // let version = "2017-10-04" // use today's date for the most recent version
  84. // let personalityInsights = PersonalityInsights(username: username, password: password, version: version)
  85. // let text = "While GE’s Predix platform has been promoted on TV and covered in the New York Times and other leading publications, none of the press coverage has explained concretely what technology GE is building with Predix. This story will do just that based on my visit to the recent Predix Transform conference, interviews with people building applications using Predix, and work done creating content to explain Predix for GE. While GE’s Predix platform has been promoted on TV and covered in the New York Times and other leading publications, none of the press coverage has explained concretely what technology GE is building with Predix. This story will do just that based on my visit to the recent Predix Transform conference, interviews with people building applications using Predix, and work done creating content to explain Predix for GE."
  86. // let failure = { (error: Error) in print(error) }
  87. // personalityInsights.getProfile(fromText: text, failure: failure) {
  88. //   profile in print(profile)
  89. //   }
  90. // print (profile)
  91. // let headers = [
  92. //   "content-type": "application/json",
  93. //   "authorization": "Basic NDFkNGI3MzctYzdiMi00N2U3LWIxOTctZTQ4ZDAyM2NlZWQ3OkhKa25xdkRUNnYzMQ==",
  94. //   "cache-control": "no-cache",
  95. //   "postman-token": "ac299ba9-6e27-6bed-4b34-843b4a669e06"
  96. // ]
  97. // let parameters = ["text": "The first thing to understand about Predix is that it is the next phase of a decades long evolution of industrial automation. The beginning of this journey was about computer control of machinery and industrial processes. Systems described with acronyms like SCADA (Supervisory Control And Data Acquisition) ruled the day and the goal was to increase the efficiency and performance of technology directly controlling high value industrial equipment.The first thing to understand about Predix is that it is the next phase of a decades long evolution of industrial automation. The beginning of this journey was about computer control of machinery and industrial processes. Systems described with acronyms like SCADA"] as [String : Any]
  98.  
  99. // let postData = JSONSerialization.data(withJSONObject: parameters, options: [])
  100. // let request = NSMutableURLRequest(url: NSURL(string: "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
  101. // try response.status(.OK).send("hello").end()
  102.  
  103.  
  104. //     let username = "a86fdfae-0aeb-45e2-a5ee-2abcef27e879"
  105. //     let password = "CbwTsQxxGCZQ"
  106. //     let version = "2017-10-04" // use today's date for the most recent version
  107. //     let personalityInsights = PersonalityInsights(username: username, password: password, version: version)
  108. //     let text = "While GE’s Predix platform has been promoted on TV and covered in the New York Times and other leading publications, none of the press coverage has explained concretely what technology GE is building with Predix. This story will do just that based on my visit to the recent Predix Transform conference, interviews with people building applications using Predix, and work done creating content to explain Predix for GE. While GE’s Predix platform has been promoted on TV and covered in the New York Times and other leading publications, none of the press coverage has explained concretely what technology GE is building with Predix. This story will do just that based on my visit to the recent Predix Transform conference, interviews with people building applications using Predix, and work done creating content to explain Predix for GE."
  109. //     let failure = { (error: Error) in print(error) }
  110. //     personalityInsights.getProfile(fromText: text, failure: failure) {
  111. //         profile in print(profile)
  112. //       // let json = try? JSONSerialization.jsonObject(with: profile, options: [])
  113. //       // let jsonString = try? JSONSerialization.jsonObject(with: profile, options: []) as! [String: Any]
  114. //         print (profile)
  115. //         response.headers["Content-Type"] = "application/json; charset=utf-8"
  116. //         var jsonResponse = JSON([:])
  117. // //         jsonResponse["framework"].stringValue = "Kitura"
  118. // //         jsonResponse["applicationName"].stringValue = "Kitura-Starter"
  119. // //         jsonResponse["company"].stringValue = "IBM"
  120. // //         jsonResponse["organization"].stringValue = "Swift @ IBM"
  121. // //         jsonResponse["location"].stringValue = "Austin, Texas"
  122. //         try response.status(.OK).send(json: profile).end()
  123. //       }
  124.     try response.status(.OK).send("u").end()
  125.   }
  126.  
  127.   /**
  128.   * Handler for posting the name of the entity to say hello to (a text/plain response).
  129.   */
  130.   public func postHello(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws {
  131.     Log.debug("POST - /hello route handler...")
  132.     response.headers["Content-Type"] = "text/plain; charset=utf-8"
  133.     if let name = try request.readString() {
  134.       try response.status(.OK).send("Hello \(name), from Kitura-Starter!").end()
  135.     } else {
  136.       try response.status(.OK).send("Kitura-Starter received a POST request!").end()
  137.     }
  138.   }
  139.  
  140.   /**
  141.   * Handler for getting an application/json response.
  142.   */
  143.   public func getJSON(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws {
  144.     Log.debug("GET - /json route handler...")
  145.     response.headers["Content-Type"] = "application/json; charset=utf-8"
  146.     var jsonResponse = JSON([:])
  147.     jsonResponse["framework"].stringValue = "Kitura"
  148.     jsonResponse["applicationName"].stringValue = "Kitura-Starter"
  149.     jsonResponse["company"].stringValue = "IBM"
  150.     jsonResponse["organization"].stringValue = "Swift @ IBM"
  151.     jsonResponse["location"].stringValue = "Austin, Texas"
  152.     try response.status(.OK).send(json: jsonResponse).end()
  153.   }
  154.    
  155.   /**
  156.    * Handler for getting a text/plain response of application health status.
  157.    */
  158.   public func getHealthCheck(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws {
  159.     Log.debug("GET - /health route handler...")
  160.     let result = health.status.toSimpleDictionary()
  161.     if health.status.state == .UP {
  162.         try response.send(json: result).end()
  163.     } else {
  164.         try response.status(.serviceUnavailable).send(json: result).end()
  165.     }
  166.   }
  167.  
  168. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top