Created
March 14, 2017 05:16
-
-
Save venkatchm/32fa52a885b3c9d985536df428b609ef to your computer and use it in GitHub Desktop.
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
enum CloudinaryMedia: String { | |
case image | |
case video | |
} | |
//MARK: Deleting multiple resources | |
func deleteResources(of child: Child, | |
completionBlock: @escaping (_ success: Bool, _ error: DescriptiveErrorType?) -> Void) { | |
deleteResources(of: child, type: .image, parameter: nil) { (success, error) in | |
self.deleteResources(of: child, type: .video, parameter: nil, completionBlock: completionBlock) | |
} | |
} | |
func deleteResources(of child: Child, type: CloudinaryMedia, parameter: Dictionary<String, String>? = nil, | |
completionBlock: @escaping (_ success: Bool, _ error: DescriptiveErrorType?) -> Void) { | |
guard let _childId = child.childID else { | |
completionBlock(false, Error.unknownError) | |
return | |
} | |
var parameters = [CloudinaryConstants.invalidate : CloudinaryConstants.trueValue, | |
CloudinaryConstants.keepOriginal: CloudinaryConstants.falseValue] | |
if let _parameter = parameter { | |
parameters += _parameter | |
} | |
do { | |
let cloudinaryDeleteEndpoint = "https://1234567891:[email protected]/v1_1/cloud_name/resources/%@/tags/" | |
let finalURL = String.localizedStringWithFormat(cloudinaryDeleteEndpoint,type.rawValue) + _childId | |
delete(finalURL, child: child, type: type, parameters: parameters, completionBlock: completionBlock) | |
} | |
catch { | |
completionBlock(false, Error.unknownError) | |
} | |
} | |
func delete(_ url: String, child: Child, type: CloudinaryMedia, parameters: Dictionary<String, String>, | |
completionBlock: @escaping (_ success: Bool, _ error: DescriptiveErrorType?) -> Void) { | |
cloudinaryClient.delete(with: url, | |
parameter: parameters, | |
mapper: CloudinaryResourceDeleteMapper.self, | |
completionBlock: { (response) in | |
switch response { | |
case .success(let response): | |
if let _cloudinaryResponse = response.first { | |
guard let _partial = _cloudinaryResponse.partial, | |
_partial == true else { | |
completionBlock(true, nil) | |
return | |
} | |
guard let _nextCursor = _cloudinaryResponse.nextCursor else { | |
completionBlock(true, nil) | |
return | |
} | |
let dictionay = [CloudinaryConstants.nextCursor : _nextCursor] | |
self.deleteResources(of: child, | |
type: type, | |
parameter: dictionay, | |
completionBlock: completionBlock) | |
} else { | |
completionBlock(false, nil) | |
} | |
case .failure(let error): | |
completionBlock(false, error) | |
} | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment