Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// Lychee.swift
// Lychee
//
// Created by home on 11/15/14.
// Copyright (c) 2014 home. All rights reserved.
//
import Foundation
import UIKit
class Lychee {
var url = "http://someurl/"
var user = "yolo"
var password = "b61df6e8c051a8c15ec51105248cd293"
let manager = AFHTTPRequestOperationManager()
init(){
manager.responseSerializer = AFHTTPResponseSerializer()
}
func getAlbums(success: ([Album]) -> Void){
self.login({
let parameters = ["function":"getAlbums"]
self.manager.POST(self.getApiUrl(), parameters: parameters,
success: {operation, response in
var albums: [Album] = []
let json = self.parseJSON((operation.responseString as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
//Creating unsorted album
var unsorted = Album();
unsorted.title = "Unsorted"
unsorted.id = "0"
if let thumb = json["unsortedThumb0"] as? String{
unsorted.thumb0 = thumb
}
if let thumb = json["unsortedThumb1"] as? String{
unsorted.thumb1 = thumb
}
if let thumb = json["unsortedThumb2"] as? String{
unsorted.thumb2 = thumb
}
//Starred album
var starred = Album();
starred.title = "Starred"
starred.id = "f"
if let thumb = json["starredThumb0"] as? String{
starred.thumb0 = thumb
}
if let thumb = json["starredThumb1"] as? String{
starred.thumb1 = thumb
}
if let thumb = json["starredThumb2"] as? String{
starred.thumb2 = thumb
}
//public album
var publicAlbum = Album();
publicAlbum.title = "Public"
publicAlbum.id = "s"
if let thumb = json["publicThumb0"] as? String{
publicAlbum.thumb0 = thumb
}
if let thumb = json["publicThumb1"] as? String{
publicAlbum.thumb1 = thumb
}
if let thumb = json["publicThumb2"] as? String{
publicAlbum.thumb2 = thumb
}
//recent album
var recent = Album();
recent.title = "Recent"
recent.id = "r"
if let thumb = json["recentThumb0"] as? String{
recent.thumb0 = thumb
}
if let thumb = json["recentThumb1"] as? String{
recent.thumb1 = thumb
}
if let thumb = json["recentThumb2"] as? String{
recent.thumb2 = thumb
}
if let content = json["content"] as? NSDictionary{
for (albumId,album) in content{
var toAdd = Album(json: album as NSDictionary)
albums.append(toAdd)
}
}
albums.sort{
$0.id > $1.id
}
albums.insert(unsorted, atIndex: 0)
albums.insert(starred, atIndex: 1)
albums.insert(publicAlbum, atIndex: 2)
albums.insert(recent, atIndex: 3)
success(albums);
},
failure: nil)
})
}
func getAlbum(albumId: String, success: (Album) -> Void){
self.login({
let params = ["function":"getAlbum","albumID":albumId];
self.manager.POST(self.getApiUrl(), parameters: params,
success: {operation, response in
let json = self.parseJSON((operation.responseString as NSString).dataUsingEncoding(NSUTF8StringEncoding)!)
success(Album(json: json))
},
failure: nil)
})
}
func addAlbum(name: String, success: (Void) -> Void){
self.login({
var params = ["function":"addAlbum","title":name]
self.manager.POST(self.getApiUrl(), parameters: params, success: {
operation, response in
success()
}, failure: nil)
})
}
func login(success: (Void) ->Void){
let parameters = ["function":"login", "user":user, "password":password]
manager.POST(self.getApiUrl(), parameters: parameters,
success: {operation, response in
//println("Response: \(operation.responseString)");
if(operation.responseString == "1"){
success()
}
},
failure: nil)
}
func getApiUrl() -> String{
if(!url.hasSuffix("/")){
url += "/"
}
return url + "php/api.php"
}
func getPictureThumbUrl(picture: Picture) -> String{
return self.url+picture.thumbUrl
}
func getPictureUrl(picture: Picture) -> String{
return self.url+picture.url;
}
func getAlbumThumbUrl(album: Album) -> String{
if(!album.thumb0.isEmpty){
return self.url+album.thumb0
}else{
return ""
}
}
func deletePicture(pictureId: String, success: (Void) -> Void){
println("Deleting picture #\(pictureId)")
self.login({
let parameters = ["function":"deletePhoto", "photoIDs":pictureId]
self.manager.POST(self.getApiUrl(), parameters: parameters, success: {
operation, response in
success();
}, failure: nil)
});
}
func sendPicture(image: UIImage, albumID: String, tags: String, success: (String) -> Void){
self.login({
let parameters = ["function":"upload", "albumID": albumID, "tags":tags]
let jpegImage = UIImageJPEGRepresentation(image, 1.0)
self.manager.POST(self.getApiUrl(), parameters: parameters,
constructingBodyWithBlock: { data in
data.appendPartWithFileData(jpegImage, name: "image", fileName: "image.jpg", mimeType: "image/jpeg")
},
success: { operation, response in
success(operation.responseString)
println("[success] response: \(response.responseString)")
}, failure: { operation, error in
println("[fail] operation: \(operation), error: \(error)")
}
)
})
}
// MARK: - HTTP Requests
private func postImageRequest(postUrl: String, parameters: [String:String], image: UIImage, success: (String) -> Void){
}
// MARK: - JSON functions
private func parseJSON(inputData: NSData) -> NSDictionary{
var error: NSError?
if let boardsDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary{
return boardsDictionary
}else{
return NSDictionary()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.