typedef void (^MessageCallback)(id _Nullable result, NSError * _Nullable error);

@implementation MessageTrackAPI

+ (void)fetchTrackForSomeIdId:(NSString *)someId callback:(nullable MessageCallback)callback
{
        NSString *queryString = [NSString stringWithFormat:@"https://api.xxx.com/v1/tracks/%@",someId];
        NSURL *url = [NSURL URLWithString:queryString];
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
            NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

            // print as json
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&error];
            NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

            NSLog(@"%@", jsonString);
            MessageTrack *track = [MessageTrackParser parseJson:json];
            callback(track, nil);
        }];
    
        [task resume];
}

+ (void)printJson:(NSMutableArray *)json error:(NSError *)error
{
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", jsonString);
}
Advertisements