This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let jsonDecoder = JSONDecoder() | |
| let jsonDateFormatter = DateFormatter() | |
| jsonDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.A" | |
| jsonDecoder.dateDecodingStrategy = .formatted(jsonDateFormatter) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Siesta | |
| class ViewController: UIViewController { | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| AwesomeAPI.expenses().addObserver(self) | |
| } | |
| override func viewWillAppear(_ animated: Bool) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| service.configure("**") { | |
| // Retry requests on auth failure | |
| $0.decorateRequests { | |
| self.refreshTokenOnAuthFailure(request: $1) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func refreshAuth(_ username: String, _ password: String) -> Request { | |
| return self.login(username, password, onSuccess: { | |
| }, onFailure: { error in | |
| }) | |
| } | |
| func refreshTokenOnAuthFailure(request: Siesta.Request) -> Request { | |
| return request.chained { | |
| guard case .failure(let error) = $0.response, // Did request fail… | |
| error.httpStatusCode == 401 else { // …because of expired token? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import requests | |
| import re | |
| import urllib.parse | |
| import sys, os | |
| import json | |
| from requests_toolbelt import MultipartEncoder | |
| BASE_URL = 'https://mbasic.facebook.com' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let healthKitManager = HealthKitManager() | |
| func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
| // Override point for customization after application launch. | |
| healthKitManager.requestAccessWithCompletion() { success, error in | |
| if success{ print("HealthKit access granted") } | |
| else { print("Error requesting access to HealthKit: \(error!)") } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import HealthKit | |
| /// Types of data that this app wishes to read from HealthKit. | |
| /// | |
| /// - returns: A set of HKObjectType. | |
| private func dataTypesToRead() -> Set<HKObjectType> { | |
| return Set([ | |
| HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.sleepAnalysis)!, HKObjectType.categoryType(forIdentifier: HKCategoryTypeIdentifier.mindfulSession)! | |
| ]) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private let healthStore = HKHealthStore() | |
| /// Requests access to all the data types the app wishes to read/write from HealthKit. | |
| /// On success, data is queried immediately and observer queries are set up for background | |
| /// delivery. This is safe to call repeatedly and should be called at least once per launch. | |
| func requestAccessWithCompletion(completion: @escaping AccessRequestCallback) { | |
| guard HKHealthStore.isHealthDataAvailable() else { | |
| debugPrint("Can't request access to HealthKit when it's not supported on the device.") | |
| return | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private var myAnchor: HKQueryAnchor? | |
| /// Sets up the observer queries for background health data delivery. | |
| /// | |
| /// - parameter types: Set of `HKObjectType` to observe changes to. | |
| private func setUpBackgroundDeliveryForDataTypes(types: Set<HKObjectType>) { | |
| for type in types { | |
| guard let sampleType = type as? HKSampleType else { print("ERROR: \(type) is not an HKSampleType"); continue } | |
| if let anchorData = UserDefaults.standard.object(forKey: "anchor") as? Data { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Initiates HK queries for new data based on the given type | |
| /// | |
| /// - parameter type: `HKObjectType` which has new data avilable. | |
| private func handleSample(_ sample: HKSample) { | |
| switch sample.sampleType { | |
| case HKObjectType.categoryType(forIdentifier: .sleepAnalysis): | |
| guard let totalTimeAsleep = sample.metadata?["Asleep"] as? Double else { | |
| return | |
| } |