Skip to content

Commit

Permalink
feat(api): request delete items
Browse files Browse the repository at this point in the history
  • Loading branch information
Hector Rondon authored and ajsb85 committed Oct 27, 2017
1 parent eab4ebd commit 45f797f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
17 changes: 17 additions & 0 deletions Source/GlpiRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,23 @@ public class GlpiRequest {
}
}

/**
Request Delete Items
*/
class public func deleteItems(itemType: ItemType, itemID: Int?, queryString: QueryString.DeleteItems?, payload: [String: AnyObject], completion: @escaping (_ result: Any?) -> Void) {

Alamofire.request(Routers.deleteItems(itemType, itemID, queryString, payload))
.validate()
.responseJSON { response in
switch response.result {
case .success(let result):
completion(result)
case .failure(_ ):
completion(GlpiRequest.handlerError(response.data))
}
}
}


/**
Request get multiple items
Expand Down
20 changes: 20 additions & 0 deletions Source/QueryString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,24 @@ public struct QueryString {
}
}
}

public struct DeleteItems {
public var forcePurge: Bool?
public var history: Bool?

public init() {}

var queryString: [String: AnyObject] {
get {
var query = [String: AnyObject]()
if forcePurge != nil {
query["force_purge"] = forcePurge as AnyObject
}
if history != nil {
query["history"] = history as AnyObject
}
return query
}
}
}
}
23 changes: 20 additions & 3 deletions Source/Routers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import Alamofire

/// Enumerate endpoints methods
public enum Routers: URLRequestConvertible {

/// GET /initSession
case initSession(String, String)
/// GET /initSession
Expand Down Expand Up @@ -67,6 +67,8 @@ public enum Routers: URLRequestConvertible {
case addItems(ItemType, [String: AnyObject])
/// PUT /:itemtype/:id
case updateItems(ItemType, Int?, [String: AnyObject])
/// DELETE /:itemtype/:id
case deleteItems(ItemType, Int?, QueryString.DeleteItems?, [String: AnyObject])

/// get HTTP Method
var method: Alamofire.HTTPMethod {
Expand All @@ -79,6 +81,8 @@ public enum Routers: URLRequestConvertible {
return .post
case .updateItems:
return .put
case .deleteItems:
return .delete
}
}

Expand Down Expand Up @@ -122,7 +126,12 @@ public enum Routers: URLRequestConvertible {
} else {
return "/\(itemType)"
}

case .deleteItems(let itemType, let itemID, _, _):
if let id = itemID {
return "/\(itemType)/\(id)"
} else {
return "/\(itemType)"
}
}
}

Expand Down Expand Up @@ -152,6 +161,12 @@ public enum Routers: URLRequestConvertible {
} else {
return nil
}
case .deleteItems(_, _, let queryString, _):
if queryString != nil {
return queryString?.queryString
} else {
return nil
}
}
}

Expand Down Expand Up @@ -194,7 +209,6 @@ public enum Routers: URLRequestConvertible {
- returns: A URL request.
*/
public func asURLRequest() throws -> URLRequest {

let url = URL(string: "https://dev.flyve.org/glpi/apirest.php")!
var urlRequest = URLRequest(url: url.appendingPathComponent(path))

Expand All @@ -209,6 +223,9 @@ public enum Routers: URLRequestConvertible {
return try Alamofire.JSONEncoding.default.encode(urlRequest, with: parameters)
case .getAllItems, .getAnItem, .getSubItems:
return try URLEncoding.init(destination: .queryString).encode(urlRequest, with: query ?? [String: AnyObject]())
case .deleteItems(_, _, _, let parameters):
let request = try URLEncoding.init(destination: .queryString).encode(urlRequest, with: query ?? [String: AnyObject]())
return try Alamofire.JSONEncoding.default.encode(request, with: parameters)
default:
return urlRequest
}
Expand Down

0 comments on commit 45f797f

Please sign in to comment.