| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- //
- // NightscoutService.swift
- // Loop
- //
- // Created by Nate Racklyeft on 7/3/16.
- // Copyright © 2016 Nathan Racklyeft. All rights reserved.
- //
- import Foundation
- import NightscoutUploadKit
- // Encapsulates a Nightscout site and its authentication
- struct NightscoutService: ServiceAuthentication {
- var credentials: [ServiceCredential]
- let title: String = LocalizedString("Nightscout", comment: "The title of the Nightscout service")
- init(siteURL: URL?, APISecret: String?) {
- credentials = [
- ServiceCredential(
- title: LocalizedString("Site URL", comment: "The title of the nightscout site URL credential"),
- placeholder: LocalizedString("http://mysite.herokuapp.com", comment: "The placeholder text for the nightscout site URL credential"),
- isSecret: false,
- keyboardType: .URL,
- value: siteURL?.absoluteString
- ),
- ServiceCredential(
- title: LocalizedString("API Secret", comment: "The title of the nightscout API secret credential"),
- placeholder: nil,
- isSecret: false,
- keyboardType: .asciiCapable,
- value: APISecret
- )
- ]
- verify { _, _ in }
- }
- // The uploader instance, if credentials are present
- private(set) var uploader: NightscoutUploader? {
- didSet {
- uploader?.errorHandler = { (error: Error, context: String) -> Void in
- print("Error \(error), while \(context)")
- }
- }
- }
- var siteURL: URL? {
- if let URLString = credentials[0].value {
- return URL(string: URLString)
- }
- return nil
- }
- var APISecret: String? {
- return credentials[1].value
- }
- private(set) var isAuthorized: Bool = false
- mutating func verify(_ completion: @escaping (_ success: Bool, _ error: Error?) -> Void) {
-
- guard let siteURL = siteURL, let APISecret = APISecret else {
- completion(false, nil)
- return
- }
- self.uploader = NightscoutUploader(siteURL: siteURL, APISecret: APISecret)
-
- self.uploader?.checkAuth({ (error) in
- if let error = error {
- // self.isAuthorized = false
- completion(false, error)
- } else {
- // self.isAuthorized = true
- completion(true, nil)
- }
- })
- }
- mutating func reset() {
- credentials[0].value = nil
- credentials[1].value = nil
- isAuthorized = false
- uploader = nil
- }
- }
|