From 2fac6adb206df1ee8bb2e9a4900f5674a43e177c Mon Sep 17 00:00:00 2001 From: Hector Rondon Date: Wed, 25 Oct 2017 14:57:57 -0400 Subject: [PATCH] feat(api): request add items --- Source/GlpiRequest.swift | 18 ++++++++++++++++++ Source/Routers.swift | 10 +++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Source/GlpiRequest.swift b/Source/GlpiRequest.swift index 4ef74af..099355b 100644 --- a/Source/GlpiRequest.swift +++ b/Source/GlpiRequest.swift @@ -305,6 +305,24 @@ public class GlpiRequest { } } + /** + Request Add Items + */ + class public func addItems(itemType: ItemType, payload: [String: AnyObject], completion: @escaping (_ result: Any?) -> Void) { + + Alamofire.request(Routers.addItems(itemType, 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 */ diff --git a/Source/Routers.swift b/Source/Routers.swift index 354f740..cb30b7d 100644 --- a/Source/Routers.swift +++ b/Source/Routers.swift @@ -63,6 +63,8 @@ public enum Routers: URLRequestConvertible { case getSubItems(ItemType, Int, ItemType, QueryString.GetSubItems?) /// GET /getMultipleItems case getMultipleItems + /// POST /:itemtype + case addItems(ItemType, [String: AnyObject]) /// get HTTP Method var method: Alamofire.HTTPMethod { @@ -71,7 +73,7 @@ public enum Routers: URLRequestConvertible { .getMyEntities, .getActiveEntities, .getFullSession, .getGlpiConfig, .getMultipleItems, .getAllItems, .getAnItem, .getSubItems: return .get - case .changeActiveProfile, .changeActiveEntities: + case .changeActiveProfile, .changeActiveEntities, .addItems: return .post } } @@ -108,6 +110,8 @@ public enum Routers: URLRequestConvertible { return "/\(itemType)/\(itemID)/\(subItemType)" case .getMultipleItems: return "/getMultipleItems" + case .addItems(let itemType, _): + return "/\(itemType)" } } @@ -117,7 +121,7 @@ public enum Routers: URLRequestConvertible { switch self { case .initSession, .initSessionByBasicAuth, .killSession, .getMyProfiles, .getActiveProfile, .changeActiveProfile, .getMyEntities, .getActiveEntities, .changeActiveEntities, - .getFullSession, .getGlpiConfig, .getMultipleItems: + .getFullSession, .getGlpiConfig, .getMultipleItems, .addItems: return nil case .getAllItems(_, let queryString): if queryString != nil { @@ -190,7 +194,7 @@ public enum Routers: URLRequestConvertible { } switch self { - case .changeActiveProfile(let parameters), .changeActiveEntities(let parameters): + case .changeActiveProfile(let parameters), .changeActiveEntities(let parameters), .addItems(_, let parameters): 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]())