5 // Created by Hori,Masaki on 2014/02/09.
6 // Copyright (c) 2014年 Hori,Masaki. All rights reserved.
9 #import "HMJSONReciever.h"
11 #import "CustomHTTPProtocol.h"
12 #import "HMAppDelegate.h"
15 @interface HMJSONReciever ()
16 @property (retain) NSMutableDictionary *recievers;
20 @implementation HMJSONReciever
26 _recievers = [NSMutableDictionary new];
28 [CustomHTTPProtocol setDelegate:self];
33 - (void)setProtocol:(NSURLProtocol *)protocol
35 [self.recievers setObject:[NSMutableData data]
36 forKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]];
38 - (NSMutableData *)dataForProtocol:(NSURLProtocol *)protocol
40 return [self.recievers objectForKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]];
42 - (void)removeDataForProtocol:(NSURLProtocol *)protocol
44 [self.recievers removeObjectForKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]];
47 - (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveResponse:(NSHTTPURLResponse *)response
49 NSURL *url = protocol.request.URL;
50 NSString *path = url.path;
51 while(![path isEqualToString:@"/"]) {
52 if([path hasSuffix:@"kcsapi"]) {
53 [self setProtocol:protocol];
55 path = [path stringByDeletingLastPathComponent];
59 - (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveData:(NSData *)data
61 NSMutableData *loadedData = [self dataForProtocol:protocol];
62 if(!loadedData) return;
64 [loadedData appendData:data];
67 - (void)customHTTPProtocolDidFinishLoading:(CustomHTTPProtocol *)protocol
69 NSData *data = [self dataForProtocol:protocol];
72 #define JSON_LOG_STRING 0
74 NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
75 [self logLineReturn:@"body -> \n%@", string];
77 NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
78 NSArray *elements = [string componentsSeparatedByString:@"="];
79 if([elements count] != 2) {
80 [[NSApp delegate] logLineReturn:@"\e[1mwe could not compose data. api -> %@. Number of elements:\e[22m %ld", protocol.request.URL.path, [elements count]];
81 [[NSApp delegate] logLineReturn:@"Original strings -> %@", string];
82 [self removeDataForProtocol:protocol];
87 NSData *JSONData = [elements[1] dataUsingEncoding:NSUTF8StringEncoding];
89 id json = [NSJSONSerialization JSONObjectWithData:JSONData
90 options:NSJSONReadingAllowFragments
93 [[NSApp delegate] logLineReturn:@"\e[1m\e[31mFail decode JSON data\e[39m\e[22m %@", error];
95 NSData *requestBodyData = [protocol.request HTTPBody];
96 NSString *requestBodyString = [[NSString alloc] initWithData:requestBodyData encoding:NSUTF8StringEncoding];
97 [self.queueu enqueue:@{@"api" : protocol.request.URL.path, @"argument": requestBodyString, @"json" : json}];
102 [self removeDataForProtocol:protocol];
104 - (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didFailWithError:(NSError *)error
106 [self removeDataForProtocol:protocol];
110 //- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat:(NSString *)format arguments:(va_list)argList
112 // [self logLineReturn:[[NSString alloc] initWithFormat:format arguments:argList]];