Skip to content

Commit

Permalink
WooAPI -> check for empty json array before mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Brianna Lee committed Jun 11, 2018
1 parent 56df8ab commit ebea672
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions WooOS/WooAPI/WooAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class WooAPI {


/// The shared SessionManager instance for Alamofire.
public let alamofireManager: SessionManager = {
let alamofireManager: SessionManager = {

// Build the session manager with the configured session.
let manager = Alamofire.SessionManager(configuration: .default)
Expand Down Expand Up @@ -90,7 +90,9 @@ public class WooAPI {
/// - type: The type of object being requested, with associated slug.
/// - id: The unique ID of the object being requested.
/// - complete: Asynchronous callback containing a success flag, the object that was requested, and an error string if failed. If unsuccessful: success = false, object = nil, error = String.
func getObject<T: Mappable>(type request: WooRequestConvertible, then complete: WooCompletion.Object<T>? = nil) {
func getObject<T: Mappable>(type request: WooRequestConvertible,
then complete: WooCompletion.Object<T>? = nil)
{

// Get manager credentials if they exist
let credentials = (try? managerURLCredential()) ?? URLCredential()
Expand Down Expand Up @@ -137,8 +139,6 @@ public class WooAPI {

// Perform request
alamofireManager.request(request)
// Authenticate the session.
// .authenticate(user: consumerKey, password: consumerSecret)
// Handle response from request
.responseJSON { jsonResponse in

Expand All @@ -165,6 +165,11 @@ public class WooAPI {
return
}

guard !json.isEmpty else {
complete?(false, nil, .noObjectsFoundForGivenRequest(description: "No data was returned, no objects were found!"))
return
}

// Map JSON Array to native type using Mappable protocol.
let objects = Mapper<T>().mapArray(JSONArray: json)

Expand Down

0 comments on commit ebea672

Please sign in to comment.