});
}
-+ (HMJSONCommand *)commandForAPI:(NSString *)api
++ (HMJSONCommand *)commandForAPIResult:(HMAPIResult *)apiResult
{
- for(Class commandClass in registeredCommands) {
- if([commandClass canExcuteAPI:api]) {
- HMJSONCommand *command = [commandClass new];
- command.api = api;
-#if ENABLE_JSON_LOG_HANDLED_API
- HMJSONViewCommand *viewCommand = [HMJSONViewCommand new];
- viewCommand.api = api;
- command = [HMCompositCommand compositCommandWithCommands:command, viewCommand, nil];
-#endif
- return command;
- }
- }
+ HMJSONCommand *command = nil;
+
#if ENABLE_JSON_LOG
HMJSONViewCommand *viewCommand = [HMJSONViewCommand new];
- viewCommand.api = api;
- return viewCommand;
+ viewCommand.api = apiResult.api;
+ viewCommand.arguments = apiResult.parameter;
+ viewCommand.json = apiResult.json;
+ viewCommand.argumentArray = apiResult.argumentArray;
+ viewCommand.jsonTree = @[[HMJSONNode nodeWithJSON:apiResult.json]];
+ viewCommand.recieveDate = apiResult.date;
+
+ command = viewCommand;
#endif
- return nil;
-}
-+ (HMJSONCommand *)commandForAPIResult:(HMAPIResult *)apiResult
-{
for(Class commandClass in registeredCommands) {
if([commandClass canExcuteAPI:apiResult.api]) {
- HMJSONCommand *command = [commandClass new];
+ command = [commandClass new];
command.api = apiResult.api;
command.arguments = apiResult.parameter;
command.json = apiResult.json;
#if ENABLE_JSON_LOG_HANDLED_API
- HMJSONViewCommand *viewCommand = [HMJSONViewCommand new];
- viewCommand.api = apiResult.api;
- viewCommand.arguments = apiResult.parameter;
- viewCommand.json = apiResult.json;
- viewCommand.argumentArray = apiResult.argumentArray;
- viewCommand.jsonTree = @[[HMJSONNode nodeWithJSON:apiResult.json]];
- viewCommand.recieveDate = apiResult.date;
-
command = [HMCompositCommand compositCommandWithCommands:command, viewCommand, nil];
#endif
return command;
}
}
-#if ENABLE_JSON_LOG
- HMJSONViewCommand *viewCommand = [HMJSONViewCommand new];
- viewCommand.api = apiResult.api;
- viewCommand.arguments = apiResult.parameter;
- viewCommand.json = apiResult.json;
- viewCommand.argumentArray = apiResult.argumentArray;
- viewCommand.jsonTree = @[[HMJSONNode nodeWithJSON:apiResult.json]];
- viewCommand.recieveDate = apiResult.date;
- return viewCommand;
-#endif
- return nil;
+ return command;
}
+ (void)registerClass:(Class)commandClass
[registeredCommands addObject:commandClass];
}
-
-- (void)setArgumentsString:(NSString *)argumentsString
-{
- _argumentsString = [argumentsString copy];
-
- NSString *unescape = [_argumentsString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSArray *pair = [unescape componentsSeparatedByString:@"&"];
- NSMutableDictionary *dict = [NSMutableDictionary new];
- for(NSString *p in pair) {
- NSArray *pp = [p componentsSeparatedByString:@"="];
- if([pp count] != 2) {
- NSLog(@"API (%@): Bad Argument: pair is odd.", self.api);
- continue;
- }
- [dict setObject:pp[1] forKey:pp[0]];
- }
- self.arguments = dict;
-
-#if ENABLE_JSON_LOG
- NSMutableArray *array = [NSMutableArray new];
- for(NSString *p in pair) {
- NSArray *pp = [p componentsSeparatedByString:@"="];
- if([pp count] != 2) {
- NSLog(@"API (%@): Bad Argument: pair is odd.", self.api);
- continue;
- }
- [array addObject:@{@"key": pp[0], @"value": pp[1]}];
- }
- self.argumentArray = array;
-#endif
-}
-- (NSString *)argumentsString
-{
- return [_argumentsString copy];
-}
-
-- (void)setJsonData:(NSData *)jsonData
-{
- NSError *error = nil;
- id json = [NSJSONSerialization JSONObjectWithData:jsonData
- options:NSJSONReadingAllowFragments
- error:&error];
- if(error) {
- HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
- [appDelegate logLineReturn:@"\e[1m\e[31mFail decode JSON data\e[39m\e[22m %@", error];
- return;
- }
- if(![json isKindOfClass:[NSDictionary class]]) {
- [self log:@"JSON is NOT NSDictionary."];
- return;
- }
- if(![[json objectForKey:@"api_result"] isEqual:@1]) {
- [self log:@"API result is fail."];
- return;
- }
- self.json = json;
-
-#if ENABLE_JSON_LOG
- self.jsonTree = @[[HMJSONNode nodeWithJSON:json]];
-#endif
-}
-- (NSData *)jsonData
-{
- return _jsonData;
-}
-
NSString *keyByDeletingPrefix(NSString *key)
{
NSString *newKye = nil;
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[[NSApp delegate] logLineReturn:@"body -> \n%@", string];
#else
-// NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
-// NSArray *elements = [string componentsSeparatedByString:@"="];
-// if([elements count] != 2) {
-// HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
-// [appDelegate logLineReturn:@"\e[1mwe could not compose data. api -> %@. Number of elements:\e[22m %ld", protocol.request.URL.path, [elements count]];
-// [appDelegate logLineReturn:@"Original strings -> %@", string];
-// [self removeDataForProtocol:protocol];
-// return;
-// }
-// NSData *JSONData = [elements[1] dataUsingEncoding:NSUTF8StringEncoding];
-//
-// NSData *requestBodyData = [protocol.request HTTPBody];
-// NSString *requestBodyString = [[NSString alloc] initWithData:requestBodyData encoding:NSUTF8StringEncoding];
HMAPIResult *apiResult = [[HMAPIResult alloc] initWithRequest:protocol.request data:data];
-// NSLog(@"api -> %@", apiResult.api);
-// NSLog(@"success -> %@", apiResult.success ? @"YES" : @"NO");
-// if(apiResult.success) {
-// NSLog(@"json -> %@", apiResult.json);
-// NSLog(@"param -> %@", apiResult.parameter);
-// }
-
-// [self.queueu enqueue:@{@"api" : protocol.request.URL.path, @"argument": requestBodyString, @"json" : JSONData, @"date": [NSDate dateWithTimeIntervalSinceNow:0]}];
[self.queueu enqueue:apiResult];
#endif