Skip to content

Commit

Permalink
feat(api): request get an item
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 a27347d commit d358cc8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Source/GlpiRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,23 @@ public class GlpiRequest {
}
}

/**
Request get an item
*/
class public func getAnItem(itemType: ItemType, itemID: Int, queryString: QueryString.GetAnItem?, completion: @escaping (_ result: Any?) -> Void) {

Alamofire.request(Routers.getAnItem(itemType, itemID, queryString))
.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
14 changes: 12 additions & 2 deletions Source/Routers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public enum Routers: URLRequestConvertible {
case getGlpiConfig
/// GET /:itemtype
case getAllItems(ItemType, QueryString.GetAllItems?)
/// GET /:itemtype/:id
case getAnItem(ItemType, Int, QueryString.GetAnItem?)
/// GET /getMultipleItems
case getMultipleItems

Expand All @@ -65,7 +67,7 @@ public enum Routers: URLRequestConvertible {
switch self {
case .initSession, .initSessionByBasicAuth, .killSession, .getMyProfiles, .getActiveProfile,
.getMyEntities, .getActiveEntities, .getFullSession, .getGlpiConfig,
.getMultipleItems, .getAllItems:
.getMultipleItems, .getAllItems, .getAnItem:
return .get
case .changeActiveProfile, .changeActiveEntities:
return .post
Expand Down Expand Up @@ -98,6 +100,8 @@ public enum Routers: URLRequestConvertible {
return "/getGlpiConfig"
case .getAllItems(let itemType, _):
return "/\(itemType)"
case .getAnItem(let itemType, let itemID, _):
return "/\(itemType)/\(itemID)"
case .getMultipleItems:
return "/getMultipleItems"
}
Expand All @@ -117,6 +121,12 @@ public enum Routers: URLRequestConvertible {
} else {
return nil
}
case .getAnItem(_, _, let queryString):
if queryString != nil {
return queryString?.queryString as [String: AnyObject]? ?? [String: AnyObject]()
} else {
return nil
}
}
}

Expand Down Expand Up @@ -172,7 +182,7 @@ public enum Routers: URLRequestConvertible {
switch self {
case .changeActiveProfile(let parameters), .changeActiveEntities(let parameters):
return try Alamofire.JSONEncoding.default.encode(urlRequest, with: parameters)
case .getAllItems:
case .getAllItems, .getAnItem:
return try URLEncoding.init(destination: .queryString).encode(urlRequest, with: query ?? [String: AnyObject]())
default:
return urlRequest
Expand Down

0 comments on commit d358cc8

Please sign in to comment.