Skip to content
This repository has been archived by the owner on Oct 15, 2018. It is now read-only.

Commit

Permalink
Fix crash when API key isn't set
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerzer committed Jan 28, 2018
1 parent 2de034b commit 16535d6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
24 changes: 20 additions & 4 deletions Binary Shark WatchKit Extension/DropletController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ class DropletController: WKInterfaceController {
var doDisablePowerSwitch: Bool!

@IBAction func powerSwitchChanged(_ value: Bool) {
var guardedAPIKey = ""
if InterfaceController.apiKey != nil {
guardedAPIKey = InterfaceController.apiKey
}
var headers: HTTPHeaders = [:]
if let authorizationHeader = Request.authorizationHeader(user: InterfaceController.apiKey, password: "") {
if let authorizationHeader = Request.authorizationHeader(user: guardedAPIKey, password: "") {
headers[authorizationHeader.key] = authorizationHeader.value
}
var actionParameter: String!
Expand All @@ -41,8 +45,12 @@ class DropletController: WKInterfaceController {
}

@IBAction func powerCycleMenuItemSelected() {
var guardedAPIKey = ""
if InterfaceController.apiKey != nil {
guardedAPIKey = InterfaceController.apiKey
}
var headers: HTTPHeaders = [:]
if let authorizationHeader = Request.authorizationHeader(user: InterfaceController.apiKey, password: "") {
if let authorizationHeader = Request.authorizationHeader(user: guardedAPIKey, password: "") {
headers[authorizationHeader.key] = authorizationHeader.value
}
let parameters: Parameters = [
Expand All @@ -59,8 +67,12 @@ class DropletController: WKInterfaceController {
dateFormatter.timeStyle = .short
self.presentTextInputController(withSuggestions: [dateFormatter.string(from: Date())], allowedInputMode: WKTextInputMode.plain) { results in
if let nameParameter = results?.first as? String {
var guardedAPIKey = ""
if InterfaceController.apiKey != nil {
guardedAPIKey = InterfaceController.apiKey
}
var headers: HTTPHeaders = [:]
if let authorizationHeader = Request.authorizationHeader(user: InterfaceController.apiKey, password: "") {
if let authorizationHeader = Request.authorizationHeader(user: guardedAPIKey, password: "") {
headers[authorizationHeader.key] = authorizationHeader.value
}
let parameters: Parameters = [
Expand Down Expand Up @@ -98,8 +110,12 @@ class DropletController: WKInterfaceController {
}

func refreshData() {
var guardedAPIKey = ""
if InterfaceController.apiKey != nil {
guardedAPIKey = InterfaceController.apiKey
}
var headers: HTTPHeaders = [:]
if let authorizationHeader = Request.authorizationHeader(user: InterfaceController.apiKey, password: "") {
if let authorizationHeader = Request.authorizationHeader(user: guardedAPIKey, password: "") {
headers[authorizationHeader.key] = authorizationHeader.value
}
Alamofire.request("https://api.digitalocean.com/v2/droplets/\(String(self.id))", method: .get, headers: headers).responseJSON { response in
Expand Down
6 changes: 5 additions & 1 deletion Binary Shark WatchKit Extension/InterfaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ class InterfaceController: WKInterfaceController {
}

func refreshData() {
var guardedAPIKey = ""
if InterfaceController.apiKey != nil {
guardedAPIKey = InterfaceController.apiKey
}
var headers: HTTPHeaders = [:]
if let authorizationHeader = Request.authorizationHeader(user: InterfaceController.apiKey, password: "") {
if let authorizationHeader = Request.authorizationHeader(user: guardedAPIKey, password: "") {
headers[authorizationHeader.key] = authorizationHeader.value
}
Alamofire.request("https://api.digitalocean.com/v2/account", method: .get, headers: headers).responseJSON { response in
Expand Down
6 changes: 5 additions & 1 deletion Binary Shark/DropletController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ class DropletController: UIViewController, UITableViewDataSource {
let confirmAction = UIAlertAction(title: "Continue", style: .default) { (_) in
let field = alertController.textFields![0] as UITextField
let nameParameter = field.text
var guardedAPIKey = ""
if ViewController.apiKey != nil {
guardedAPIKey = ViewController.apiKey
}
var headers: HTTPHeaders = [:]
if let authorizationHeader = Request.authorizationHeader(user: ViewController.apiKey, password: "") {
if let authorizationHeader = Request.authorizationHeader(user: guardedAPIKey, password: "") {
headers[authorizationHeader.key] = authorizationHeader.value
}
let parameters: Parameters = [
Expand Down
6 changes: 5 additions & 1 deletion Binary Shark/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ class ViewController: UITableViewController {

@objc func refreshData() {
var headers: HTTPHeaders = [:]
if let authorizationHeader = Request.authorizationHeader(user: ViewController.apiKey, password: "") {
var guardedAPIKey = ""
if ViewController.apiKey != nil {
guardedAPIKey = ViewController.apiKey
}
if let authorizationHeader = Request.authorizationHeader(user: guardedAPIKey, password: "") {
headers[authorizationHeader.key] = authorizationHeader.value
}
Alamofire.request("https://api.digitalocean.com/v2/account", method: .get, headers: headers).responseJSON { response in
Expand Down

0 comments on commit 16535d6

Please sign in to comment.