Skip to content

Commit

Permalink
feat(api): request add 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 493281c commit 2fac6ad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
18 changes: 18 additions & 0 deletions Source/GlpiRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
10 changes: 7 additions & 3 deletions Source/Routers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down Expand Up @@ -108,6 +110,8 @@ public enum Routers: URLRequestConvertible {
return "/\(itemType)/\(itemID)/\(subItemType)"
case .getMultipleItems:
return "/getMultipleItems"
case .addItems(let itemType, _):
return "/\(itemType)"
}
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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]())
Expand Down

0 comments on commit 2fac6ad

Please sign in to comment.