Api.swift 3.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
//
//  Api.swift
//  Pods
//
//  Created by Mauro Bender on 13/8/16.
//
//

import Alamofire

11
public typealias ApiMethod = Alamofire.HTTPMethod
12

13
14
open class Api {
	let baseURL: URL
15
16
	let apiPath: String
	
17
	var apiURL: URL { return baseURL.appendingPathComponent( apiPath ) }
18
	
19
	public init( baseURL: URL, apiPath: String ) {
20
21
22
23
24
		self.baseURL = baseURL
		self.apiPath = apiPath
	}
	
	// RESOURCE
25
	open func resource( _ path: String ) -> Resource {
26
27
28
29
		return Resource( api: self, path: path )
	}
	
	// REQUESTS
30
31
32
	open func request( _ method: ApiMethod, path: String,
		parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = URLEncoding.default,
		headers: [String: String]? = nil, completion: @escaping (DataResponse<Any>) -> Void )
33
	{
34
		Alamofire.request( urlForPath( path ).absoluteString, method: method, parameters: parameters,
35
36
37
		                   encoding: encoding, headers: headers ).responseJSON( completionHandler: completion )
	}
	
38
39
	open func get( _ path: String, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = URLEncoding.default,
	                 headers: [String: String]? = nil, completion: @escaping (DataResponse<Any>) -> Void )
40
	{
41
		return request( .get, path: path, parameters: parameters, encoding: encoding,
42
43
44
		                headers: headers, completion: completion)
	}
	
45
46
	open func post( _ path: String, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = URLEncoding.default,
	                 headers: [String: String]? = nil, completion: @escaping (DataResponse<Any>) -> Void )
47
	{
48
		return request( .post, path: path, parameters: parameters, encoding: encoding,
49
50
51
		                headers: headers, completion: completion)
	}
	
52
53
	open func put( _ path: String, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = URLEncoding.default,
	                  headers: [String: String]? = nil, completion: @escaping (DataResponse<Any>) -> Void )
54
	{
55
		return request( .put, path: path, parameters: parameters, encoding: encoding,
56
57
58
		                headers: headers, completion: completion)
	}
	
59
60
	open func patch( _ path: String, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = URLEncoding.default,
	                  headers: [String: String]? = nil, completion: @escaping (DataResponse<Any>) -> Void )
61
	{
62
		return request( .patch, path: path, parameters: parameters, encoding: encoding,
63
64
65
		                headers: headers, completion: completion)
	}
	
66
67
	open func delete( _ path: String, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = URLEncoding.default,
	                  headers: [String: String]? = nil, completion: @escaping (DataResponse<Any>) -> Void )
68
	{
69
		return request( .delete, path: path, parameters: parameters, encoding: encoding,
70
71
72
		                headers: headers, completion: completion)
	}
	
73
74
	func urlForPath( _ path: String ) -> URL {
		return apiURL.appendingPathComponent( path )
75
76
77
78
79
80
81
82
	}
}

// MARK: Equatable
extension Api: Equatable {}

public func ==(lhs: Api, rhs: Api) -> Bool {
	return lhs.baseURL == rhs.baseURL && lhs.apiPath == rhs.apiPath
83
}