Resource.swift 2.67 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
//  Resource.swift
//  Pods
//
//  Created by Mauro Bender on 13/8/16.
//
//

import Alamofire

public protocol JSONSerializable {
	init( fromJSON json: [ String: AnyObject ] )
	func toJSON() -> [ String: AnyObject ]
}

16
17
18
19
20
21
public enum ApiError: ErrorType {
	case Network( error: NSError, response: NSURLResponse )
	case JSONSerialization(error: NSError)
	case Unknown( error: NSError )
}

22
public class Resource {
23
24
25
26
27
28
29
30
	public let api: Api
	public let path: String
	
	init( api: Api, path: String ) {
		self.api  = api
		self.path = path
	}
	
31
	public func item<T: JSONSerializable>( id: AnyObject, parameters: [ String: String ]? = nil,
32
	                  headers: [ String: String]? = nil, completion: ( ( T?, error: ApiError? ) -> Void ) )
33
34
	{
		api.get( resourceMemberPath( id ), parameters: parameters, headers: headers ) { response in
35
36
37
38
			self.handleItemResult( response, handler: completion )
		}
	}
	
39
	public func create<T: JSONSerializable>( item: T, parameters: [ String: String ]? = nil,
40
41
42
43
44
45
46
47
48
49
50
	                    headers: [ String: String]? = nil, encoding: ParameterEncoding = .JSON,
	                    completion: ( ( T?, error: ApiError? ) -> Void ) )
	{
		var requestParameters = item.toJSON()
		if parameters != nil { requestParameters += parameters! }
		
		api.post( path, parameters: requestParameters, encoding: encoding, headers: headers ) { response in
			self.handleItemResult( response, handler: completion )
		}
	}
	
51
52
53
	func handleItemResult<T: JSONSerializable>( response: Response<AnyObject, NSError>,
	                      handler: ( T?, error: ApiError? ) -> Void )
	{
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
		switch(response.result) {
			case .Success(let JSON):
				handler( T( fromJSON: JSON as! [String : AnyObject] ), error: nil )
			case .Failure(let error):
				handler( nil, error: handleError( response ) )
		}
	}
	
	func handleError( response: Response<AnyObject, NSError> ) -> ApiError {
		let error = response.result.error!
		
		if let statusCode = response.response?.statusCode where !(200...299).contains( statusCode ) {
			return .Network( error: error, response: response.response! )
		} else if error.code == -6006 {
			return .JSONSerialization( error: error )
69
		}
70
71
		
		return .Unknown( error: error )
72
73
	}
	
74
75
76
77
78
79
80
81
82
83
	public func member( memberID: AnyObject ) -> Resource {
		let memberPath = resourceMemberPath( memberID )
		return Resource( api: api, path: memberPath )
	}
	
	public func resource( resourcePath: String ) -> Resource {
		let nestedPath = nestedResourcePath( resourcePath )
		return Resource( api: api, path: nestedPath )
	}
	
84
85
86
	func resourceMemberPath( memberId: AnyObject ) -> String {
		return "\(path)/\(memberId)"
	}
87
88
89
90
91
	
	func nestedResourcePath( nestedResourcePath: String ) -> String {
		return "\(path)/\(nestedResourcePath)"
	}
}