Skip to content

Instantly share code, notes, and snippets.

@mstukalo
Created April 24, 2014 06:36
Show Gist options
  • Save mstukalo/11243808 to your computer and use it in GitHub Desktop.
Save mstukalo/11243808 to your computer and use it in GitHub Desktop.
Simple JSONAPI implementation for Bukkit servers
//
// JSONAOPIClient.h
//
// Created by Mykola Stukalo on 10.04.14.
//
#import <Foundation/Foundation.h>
@interface JSONAPIClient : NSObject
- (void)runServerCommandWithArguments:(NSArray*)arguments;
- (void)runAPIMethod:(NSString*)command arguments:(NSArray*)arguments;
@end
//
// JSONAOPIClient.m
//
// Created by Mykola Stukalo on 10.04.14.
//
#import "JSONAPIClient.h"
#import "AFNetworking.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>
static NSString * const kHost = @"hostname";
NSInteger const kPort = 20059;
static NSString * const kUsername = @"username";
static NSString * const kPassword = @"password";
static NSString * const kURLFormat = @"http://%@:%li/api/2/call?json=%@";
@implementation JSONAPIClient
- (void)runServerCommandWithArguments:(NSArray*)arguments;
{
[self runAPIMethod:@"server.run_command" arguments:arguments];
}
- (void)runAPIMethod:(NSString*)command arguments:(NSArray*)arguments
{
NSURL *URL = [self URLforMethod:command arguments:arguments];
NSLog(@"url %@", URL);
if (!URL)
{
NSLog(@"Failed to create URL for server command with arguments %@", arguments);
return;
}
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:URL.absoluteString parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Failure %@", error);
}];
}
- (NSString*)keyForMethod:(NSString*)method
{
NSString *keyString = [NSString stringWithFormat:@"%@%@%@", kUsername, method, kPassword];
return [self sha256StringFromString:keyString];
}
- (NSString*)sha256StringFromString:(NSString*)aString
{
const char *string = aString.UTF8String;
int length = (int)strlen(string);
unsigned char bytes[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(string, length, bytes);
return [self stringFromBytes:bytes length:CC_SHA256_DIGEST_LENGTH];
}
- (NSString *)stringFromBytes:(unsigned char *)bytes length:(int)length
{
NSMutableString *mutableString = @"".mutableCopy;
for (int i = 0; i < length; i++)
[mutableString appendFormat:@"%02x", bytes[i]];
return [NSString stringWithString:mutableString];
}
- (NSDictionary*)jsonCallForMethod:(NSString*)method arguments:(NSArray*)arguments
{
NSMutableDictionary *dictionary = [@{@"name" : method,
@"key" : [self keyForMethod:method],
@"username" : kUsername} mutableCopy];
if (arguments)
{
dictionary[@"arguments"] = arguments;
}
else
{
dictionary[@"arguments"] = [NSArray array];
}
return dictionary;
}
- (NSURL*)URLforMethod:(NSString*)method arguments:(NSArray*)arguments
{
NSDictionary *jsonCall = [self jsonCallForMethod:method arguments:arguments];
if (![NSJSONSerialization isValidJSONObject:jsonCall]) return nil;
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonCall options:0 error:&error];
if (error)
{
NSLog(@"Failed to serialize JSON data %@ with error %@", jsonCall, error);
return nil;
}
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"jsonString %@", jsonString);
NSMutableCharacterSet *characterSet = [NSMutableCharacterSet alphanumericCharacterSet];
[characterSet addCharactersInString:@"="];
jsonString = [jsonString stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
NSString *URLString = [NSString stringWithFormat:kURLFormat, kHost, (long)kPort, jsonString];
return [NSURL URLWithString:URLString];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment