Skip to content

Instantly share code, notes, and snippets.

@bonclay7
Created March 3, 2015 08:54
Show Gist options
  • Select an option

  • Save bonclay7/b3383479ed77ac34586e to your computer and use it in GitHub Desktop.

Select an option

Save bonclay7/b3383479ed77ac34586e to your computer and use it in GitHub Desktop.
//
// AppDelegate.m
// Step1
//
// Created by Ro on 02/03/15.
// Copyright (c) 2015 Ro. All rights reserved.
//
#import "AppDelegate.h"
#define API_BASE_URL @"http://amazon-application-server:8080/MicroBlogging/api"
#define HTTP_OK 200
#define HTTP_CREATED 201
#define HTTP_ACCEPTED 202
@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@property NSString* apiToken;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
// [self allLocalMessages];
printf("%d", 0);
[self getApiToken];
//[self createLocalUser];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
#pragma mark - custom methods
- (void) getApiToken {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/authentication/%@", API_BASE_URL, @":bonclay7"]]];
[request addValue:@"aqwzsxedc" forHTTPHeaderField:@"password"];
[request addValue:@"iOS" forHTTPHeaderField:@"hostID"];
request.HTTPMethod = @"POST";
NSURLSessionTask *urlSessionTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error){
NSLog(@"ERROR :%@", [error localizedDescription]);
}else{
NSError *err = nil;
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
if ([httpResponse statusCode] == HTTP_OK){
id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
_apiToken = [[object valueForKey:@"session"] valueForKey:@"token"];
if (_apiToken) {
NSLog(@"API Token:\n%@ | HTTP %ld", _apiToken, (long) [httpResponse statusCode]);
}else{
NSLog(@"ERROR :%@", [error localizedDescription]);
}
}else {
NSLog(@"Authentication failed");
}
}
}];
[urlSessionTask resume];
}
- (void) createLocalUser {
NSError * err = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/users", API_BASE_URL]]];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
request.HTTPMethod = @"POST";
request.HTTPBody = [NSJSONSerialization dataWithJSONObject:@{@"picture": @"http://img.mogicons.com/l/wtf-155.png", @"handle": @"juju", @"password": @"juju", @"email":@"jujux@jujux.fr"} options:0 error:&err];
NSURLSessionTask *urlSessionTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error){
NSLog(@"ERROR :%@", [error localizedDescription]);
}else{
NSError *err = nil;
id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
if (object) {
NSLog(@"Simple GET:\n%@", object);
}else{
NSLog(@"ERROR :%@", [error localizedDescription]);
}
}
}];
[urlSessionTask resume];
}
- (void) allLocalMessages {
//faire des requetes sur le réseau
//sharedSession gestion du cache, etc
// NSURLSessionTask *urlSessionTask = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/tweets", API_BASE_URL]]
// completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
// NSError *err = nil;
// id objet = [NSJSONSerialization
// JSONObjectWithData:data options:0
// error:&err];
// NSLog(@"simple GET:\n%@", objet);
// }];
NSURLSession *urlSession = [NSURLSession sharedSession];
NSURL *apiURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/tweets", API_BASE_URL]];
NSURLSessionTask *urlSessionTask = [urlSession dataTaskWithURL:apiURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error){
NSLog(@"ERROR :%@", [error localizedDescription]);
}else{
NSError *err = nil;
id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
if (object) {
NSLog(@"Simple GET:\n%@", object);
}else{
NSLog(@"ERROR :%@", [error localizedDescription]);
}
}
}];
[urlSessionTask resume];
}
- (void) allLocalMessages: (NSString*) userHandle{
NSMutableURLRequest *request = nil;
if (userHandle == nil) {
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/tweets/", API_BASE_URL]]];
}else{
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/tweets/%@", API_BASE_URL, userHandle]]];
[request addValue:[NSString stringWithFormat:@"Bearer %@", _apiToken] forHTTPHeaderField:@"Authorization"];
}
request.HTTPMethod = @"GET";
NSURLSessionTask *urlSessionTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error){
NSLog(@"ERROR :%@", [error localizedDescription]);
}else{
NSError *err = nil;
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
if ([httpResponse statusCode] == HTTP_OK) {
id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
if (object) {
NSLog(@"GET Tweets:\n%@", object);
}else{
NSLog(@"ERROR :%@", [error localizedDescription]);
}
}else{
NSLog(@"error : HTTP %ld", (long)[httpResponse statusCode]);
}
}
}];
[urlSessionTask resume];
}
- (void) getReadingList: (NSString*) userHandle{
NSMutableURLRequest *request = nil;
if (userHandle == nil) {
return;
}else{
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/tweets/%@/reading_list", API_BASE_URL, userHandle]]];
[request addValue:[NSString stringWithFormat:@"Bearer %@", _apiToken] forHTTPHeaderField:@"Authorization"];
}
request.HTTPMethod = @"GET";
NSURLSessionTask *urlSessionTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error){
NSLog(@"ERROR :%@", [error localizedDescription]);
}else{
NSError *err = nil;
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
if ([httpResponse statusCode] == HTTP_OK) {
id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
if (object) {
NSLog(@"GET Tweets:\n%@", object);
}else{
NSLog(@"ERROR :%@", [error localizedDescription]);
}
}else{
NSLog(@"error : HTTP %ld", (long)[httpResponse statusCode]);
}
}
}];
[urlSessionTask resume];
}
- (void) postTweet: (NSString*) userHandle tweetContent:(NSDictionary*) tweet{
NSError * err = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/tweets/%@", API_BASE_URL, userHandle]]];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"Bearer %@", _apiToken] forHTTPHeaderField:@"Authorization"];
request.HTTPMethod = @"POST";
request.HTTPBody = [NSJSONSerialization dataWithJSONObject:tweet options:0 error:&err];
NSURLSessionTask *urlSessionTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error){
NSLog(@"ERROR :%@", [error localizedDescription]);
}else{
NSError *err = nil;
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
if ([httpResponse statusCode] == HTTP_OK) {
id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
if (object) {
NSLog(@"POST:\n%@", object);
}else{
NSLog(@"ERROR :%@", [error localizedDescription]);
}
}else{
NSLog(@"error : HTTP %ld", (long)[httpResponse statusCode]);
}
}
}];
[urlSessionTask resume];
}
- (void) disconnect{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/authentication/", API_BASE_URL]]];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"Bearer %@", _apiToken] forHTTPHeaderField:@"Authorization"];
request.HTTPMethod = @"DELETE";
NSURLSessionTask *urlSessionTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error){
NSLog(@"ERROR :%@", [error localizedDescription]);
}else{
NSError *err = nil;
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
if ([httpResponse statusCode] == HTTP_ACCEPTED) {
id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
if (object) {
NSLog(@"POST:\n%@", object);
}else{
NSLog(@"ERROR :%@", [error localizedDescription]);
}
}else{
NSLog(@"error : HTTP %ld", (long)[httpResponse statusCode]);
}
}
}];
[urlSessionTask resume];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment