- Yandex Disk is a cloud storage service with 26 million users storing 6 billion files totaling 14 million added per day. It offers 10GB of free storage that can be increased through invites or partner offers.
- It has clients for web, desktop, mobile, and 3rd parties via SDKs, WebDAV, and a REST API. The REST API allows more functionality than WebDAV like managing public files and the trash.
- The document provides code examples for implementing a Disk class in Swift to interface with the Yandex Disk REST API, including uploading and deleting files. It emphasizes best practices like test-driven development.
24. Getting Started
- Register application
- Register dev accounts
- Get token for development
- Полигон
- Code
25. Code example
What we try to achieve:
import Foundation
!
public class Disk {
…
}
!
let disk = Disk(token: "d8edc4f3a698473fbc87634c41b2ca81")
var fileURL : NSURL
!
disk.uploadURL(fileURL, toPath: fileURL.lastPathComponent, overwrite: true) {
// handle errors
}
!
disk.deletePath(fileURL.lastPathComponent, permanently: false) {
// handle response
}
26. Code example
Basic properties in Disk class
public class Disk {
public let token : String
public let baseURL = "https://cloud-api.yandex.net:443"
!
var additionalHTTPHeaders : [String:String] {
return [
"Accept" : "application/json",
"Authorization" : "OAuth (token)",
"User-Agent" : "Mobile Camp Demo"
]
}
}
27. Code example
Complete basic Disk class
public class Disk {
public let token : String
public let baseURL = "https://cloud-api.yandex.net:443"
!
var additionalHTTPHeaders : [String:String] {
return [
"Accept" : "application/json",
"Authorization" : "OAuth (token)",
"User-Agent" : "Mobile Camp Demo"
]
}
!
public lazy var session : NSURLSession = {
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
config.HTTPAdditionalHeaders = self.additionalHTTPHeaders
!
return NSURLSession(configuration: config)
}()
!
public init(token:String) {
self.token = token
}
}
28. Code example
Disk extension for de-serializing JSON data
extension Disk {
class func JSONDictionaryWithData(data:NSData!, onError:(NSError!)->Void)
-> NSDictionary? {
!
var error: NSError?
let root = NSJSONSerialization.JSONObjectWithData(data,
options: nil, error: &error) as? NSDictionary
!
if root == nil {
onError(NSError(…))
return nil
}
!
return root
}
}
29. Code example
NSURLSessing extension for doing JSON requests
extension NSURLSession {
func jsonTaskWithMethod(method:String, url: NSURL!, onError: ((NSError!) -> Void)!,
onCompletion: ((NSDictionary, NSHTTPURLResponse) -> Void)!)
-> NSURLSessionDataTask! {
!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = method
!
return dataTaskWithRequest(request) {
(data, response, error) -> Void in
!
let jsonRoot = Disk.JSONDictionaryWithData(data, onError: onError)
!
if let jsonRoot = jsonRoot {
switch response.statusCode {
case 400...599:
return onError(…)
default:
return onCompletion(jsonRoot, response)
}
} else {
return // handler already called from JSONDictionaryWithData
}
}
}
}
31. Code example
Disk extension for processing common JSON object
extension Disk {
class func hrefMethodTemplatedWithDictionary(dict:NSDictionary?)
-> (href:String, method:String, templated:Bool) {
var href = ""
var method = ""
var templated = false
!
if let json = dict {
if let str = json["href"] as? String {
href = str
}
if let str = json["method"] as? String {
method = str
}
if let nr = json["templated"] as? NSNumber {
templated = nr.boolValue
}
}
!
return (href, method, templated)
}
}
32. Code example
Disk extension implementing file upload
extension Disk {
public func uploadURL(fileURL:NSURL, toPath path:String, overwrite:Bool?,
handler:(NSError!) -> Void) -> Void {
!
var url = "(baseURL)/v1/disk/resources/upload?path=(path.urlEncoded())"
!
if let overwrite = overwrite {
url += "&overwrite=(overwrite)"
}
let error = { handler($0) }
!
session.jsonTaskWithMethod("GET", url: NSURL(string: url), onError: error) {
(jsonRoot, response)->Void in
!
let (href, method, templated) = Disk.hrefMethodTemplatedWithDictionary(jsonRoot)
!
let request = NSMutableURLRequest(URL: NSURL(string: href))
request.HTTPMethod = method
!
self.session.uploadTaskWithRequest(request, fromFile: fileURL) {
(data, response, trasferError)->Void in
!
return error(trasferError)
}.resume()
}.resume()
}
}