objectify method
dynamic
objectify(
- dynamic data
Converts a response from the Reddit API into an instance of RedditBase or a container of RedditBase objects.
data
should be one of List or Map, and the return type is one of
RedditBase, List<RedditBase>, or Map<RedditBase> depending on the
response type.
Implementation
dynamic objectify(dynamic data) {
//logger.log(Level.FINE, 'objectifying');
if (data == null) {
return null;
}
if (data is List) {
return _objectifyList(data);
} else if (data is! Map) {
throw DRAWInternalError('data must be of type List or Map, got '
'${data.runtimeType}');
} else if (data.containsKey('kind')) {
final kind = data['kind'];
if (kind == 'Listing') {
//logger.log(Level.FINE, 'parsing Listing');
final listing = data['data']['children'];
final before = data['data']['before'];
final after = data['data']['after'];
final objectifiedListing = _objectifyList(listing);
final result = {
'listing': objectifiedListing,
'before': before,
'after': after
};
return result;
} else if (kind == 'UserList') {
//logger.log(Level.FINE, 'parsing UserList');
final listing = data['data']['children'];
return _objectifyList(listing);
} else if (kind == 'KarmaList') {
//logger.log(Level.FINE, 'parsing KarmaList');
final listing = _objectifyList(data['data']);
final karmaMap = <Subreddit, Map<String, int>>{};
listing.forEach((map) {
karmaMap.addAll(map);
});
return karmaMap;
} else if (kind == 't2') {
// Account information about a redditor who isn't the currently
// authenticated user.
//logger.log(Level.INFO, 'account information for non-current user');
return data['data'];
} else if (kind == 'more') {
//logger.log(Level.INFO, 'parsing MoreComments');
//logger.log(Level.INFO, 'Data: ${DRAWLoggingUtils.jsonify(data["data"])}');
return MoreComments.parse(reddit, data['data']);
} else if (kind == 't6') {
return Trophy.parse(reddit, data['data']);
} else {
//logger.log(Level.INFO, 't2 but not more comments or Redditor');
return _objectifyDictionary(data);
}
} else if (data.containsKey('json')) {
if (data['json'].containsKey('data')) {
// Response from Subreddit.submit.
//logger.log(Level.FINE, 'Subreddit.submit response');
if (data['json']['data'].containsKey('url')) {
return Submission.parse(reddit, data['json']['data']);
} else if (data['json']['data'].containsKey('things')) {
return _objectifyList(data['json']['data']['things']);
} else {
throw DRAWInternalError('Invalid json response: $data');
}
} else if (data['json'].containsKey('errors')) {
const kSubredditNoExist = 'SUBREDDIT_NOEXIST';
const kUserDoesntExist = 'USER_DOESNT_EXIST';
final errors = data['json']['errors'];
//logger.log(Level.SEVERE, 'Error response: $errors');
if (errors is List && errors.isNotEmpty) {
final error = errors[0][0];
final field = errors[0].last;
switch (error) {
case kSubredditNoExist:
throw DRAWInvalidSubredditException(field);
case kUserDoesntExist:
throw DRAWInvalidRedditorException(field);
default:
// TODO(bkonyi): make an actual exception for this.
throw DRAWUnimplementedError('Error response: $errors');
}
}
return null;
} else {
throw DRAWInternalError('Invalid json response: $data');
}
}
return _objectifyDictionary(data);
}