Skip to content

Instantly share code, notes, and snippets.

View nderkach's full-sized avatar
✏️

Nikolay Derkach nderkach

✏️
View GitHub Profile
let jsonDecoder = JSONDecoder()
let jsonDateFormatter = DateFormatter()
jsonDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.A"
jsonDecoder.dateDecodingStrategy = .formatted(jsonDateFormatter)
import Siesta
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AwesomeAPI.expenses().addObserver(self)
}
override func viewWillAppear(_ animated: Bool) {
service.configure("**") {
// Retry requests on auth failure
$0.decorateRequests {
self.refreshTokenOnAuthFailure(request: $1)
}
}
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?
@nderkach
nderkach / recognizer.py
Last active August 2, 2025 04:57
Facebook photo upload and photo tagging
#!/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'
@nderkach
nderkach / healthkit.swift
Last active September 14, 2018 17:28
Authorize HealthKit
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!)") }
}
}
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)!
])
}
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
}
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 {
/// 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
}