Skip to content

Commit

Permalink
Merge pull request #9 from abellono/remove_afnetworking
Browse files Browse the repository at this point in the history
Remove afnetworking
  • Loading branch information
heumn authored Oct 7, 2016
2 parents 13794b9 + 94043a1 commit 86d10e9
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
<AdditionalOption
key = "DYLD_PRINT_LIBRARIES"
value = ""
isEnabled = "YES">
</AdditionalOption>
<AdditionalOption
key = "DYLD_PRINT_APIS"
value = ""
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
Expand Down
21 changes: 2 additions & 19 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
PODS:
- AFNetworking (3.0.4):
- AFNetworking/NSURLSession (= 3.0.4)
- AFNetworking/Reachability (= 3.0.4)
- AFNetworking/Security (= 3.0.4)
- AFNetworking/Serialization (= 3.0.4)
- AFNetworking/UIKit (= 3.0.4)
- AFNetworking/NSURLSession (3.0.4):
- AFNetworking/Reachability
- AFNetworking/Security
- AFNetworking/Serialization
- AFNetworking/Reachability (3.0.4)
- AFNetworking/Security (3.0.4)
- AFNetworking/Serialization (3.0.4)
- AFNetworking/UIKit (3.0.4):
- AFNetworking/NSURLSession
- IssueReporter (1.0.0):
- AFNetworking
- IssueReporter (1.0.0)

DEPENDENCIES:
- IssueReporter (from `../`)
Expand All @@ -25,8 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
AFNetworking: a0075feb321559dc78d9d85b55d11caa19eabb93
IssueReporter: 455e06e10296812f8c1d66adb7c9533977c4a1f8
IssueReporter: 14ee7b50b6637afe8891e2a7dde70d61de25cd8e

PODFILE CHECKSUM: bc7958537ae3a08b12b744ea0ff114d992505523

Expand Down
1 change: 0 additions & 1 deletion IssueReporter.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ Pod::Spec.new do |s|
}

s.public_header_files = 'Pod/Classes/**/*.h'
s.dependency 'AFNetworking'
end
5 changes: 1 addition & 4 deletions Pod/Classes/ABEGithubAPIClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
// Copyright (c) 2015 Abello. All rights reserved.
//

#import <AFNetworking/AFNetworking.h>

@class ABEIssue;
@class ABEMilestone;

@interface ABEGithubAPIClient : AFHTTPSessionManager
@interface ABEGithubAPIClient : NSObject

+ (instancetype)sharedClient;

Expand Down
79 changes: 54 additions & 25 deletions Pod/Classes/ABEGithubAPIClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
#import "ABEIssue.h"
#import "ABEReporter.h"

static NSString * const kABEBaseAPIURL = @"https://api.github.com/";

@interface ABEGithubAPIClient ()

@property (nonatomic) NSString *githubToken;
@property (nonatomic) NSMutableURLRequest *baseSaveIssueURLRequest;

@end

Expand All @@ -27,45 +26,75 @@ + (instancetype)sharedClient {
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
NSURL *URL = [NSURL URLWithString:kABEBaseAPIURL];
_sharedClient = [[self alloc] initWithBaseURL:URL];
_sharedClient = [[self alloc] init];
});

return _sharedClient;
}

- (instancetype)initWithBaseURL:(NSURL *)url {
if (self = [super initWithBaseURL:url]) {
self.requestSerializer = [AFJSONRequestSerializer serializer];
}

return self;
}
// TODO : JSON Response serializing

- (void)saveIssue:(ABEIssue *)issue success:(void (^)())success error:(void (^)(NSError *error))errorHandler {

NSMutableString *path1 = @"repos".mutableCopy;
[path1 appendFormat:@"/%@", self.repositoryName];
[path1 appendFormat:@"/issues?access_token=%@", self.githubToken];

NSString *path = [NSString stringWithFormat:@"repos/%@/issues?access_token=%@", self.repositoryName, self.githubToken];
NSURLRequest *saveIssueRequest = [self saveIssueRequestForIssue:issue errorHandler:errorHandler];

NSAssert([path isEqualToString:path1], @"ops");
if (!saveIssueRequest) {
return;
}

[self POST:path parameters:[issue toDictionary] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success();
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (errorHandler) {
[[[NSURLSession sharedSession] dataTaskWithRequest:saveIssueRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"There was an error while saving the issue with Github's API.");
NSLog(@"Error : %@", error);
errorHandler(error);
return;
}
}];

success();
}] resume];
}

- (NSURLRequest *)saveIssueRequestForIssue:(ABEIssue *)issue errorHandler:(void (^)(NSError *error))errorHandler {
NSMutableURLRequest *baseIssueSaveRequest = [[self baseSaveIssueURLRequest] mutableCopy];
NSAssert([NSJSONSerialization isValidJSONObject:[issue toDictionary]], @"JSON Post body generated from issue is not valid JSON.");

NSError *error = nil;
NSData *jsonObject = [NSJSONSerialization dataWithJSONObject:[issue toDictionary] options:0 error:&error];

if (error) {
NSLog(@"There was an error saving the issue to github.");
NSLog(@"Error : %@", error);
errorHandler(error);
return nil;
}

[baseIssueSaveRequest setValue:[NSString stringWithFormat:@"%d", [jsonObject length]] forHTTPHeaderField:@"Content-Length"];
[baseIssueSaveRequest setHTTPBody:jsonObject];

return baseIssueSaveRequest;
}

- (NSMutableURLRequest *)baseSaveIssueURLRequest {
if (!_baseSaveIssueURLRequest) {
NSString *path = [NSString stringWithFormat:@"https://api.github.com/repos/%@/issues?access_token=%@", self.repositoryName, self.githubToken];
NSURL *url = [NSURL URLWithString:path];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

request.HTTPMethod = @"POST";
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request setValue:[NSString stringWithFormat:@"token %@", self.githubToken] forHTTPHeaderField:@"Authorization"];

return request;
}

return _baseSaveIssueURLRequest;
}

- (void)setGithubAPIKey:(NSString *)string {
self.githubToken = string;
[self.requestSerializer setValue:[NSString stringWithFormat:@"token %@", string] forHTTPHeaderField:@"Authorization"];
}

@end
4 changes: 1 addition & 3 deletions Pod/Classes/ABEImgurAPIClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
// Copyright (c) 2015 Abello. All rights reserved.
//

#import <AFNetworking/AFNetworking.h>

@interface ABEImgurAPIClient : AFHTTPSessionManager
@interface ABEImgurAPIClient : NSObject

+ (ABEImgurAPIClient *)sharedClient;

Expand Down
100 changes: 77 additions & 23 deletions Pod/Classes/ABEImgurAPIClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
#import "ABEImgurAPIClient.h"
#import "ABEReporter.h"

static NSString * const kABEBaseAPIURL = @"https://api.imgur.com/3/";
@interface ABEImgurAPIClient ()

@property (nonatomic) NSMutableURLRequest *baseImageUploadRequest;

@end

@implementation ABEImgurAPIClient

Expand All @@ -19,8 +23,7 @@ + (ABEImgurAPIClient *)sharedClient {
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
NSURL *URL = [NSURL URLWithString:kABEBaseAPIURL];
_sharedClient = [[self alloc] initWithBaseURL:URL];
_sharedClient = [[self alloc] init];

});

Expand All @@ -31,43 +34,94 @@ + (BOOL)isAPIKeySet {
return [self sharedClient].imgurAPIKey.length > 0;
}

- (instancetype)initWithBaseURL:(NSURL *)url {
if (self = [super initWithBaseURL:url]) {
self.requestSerializer = [AFJSONRequestSerializer serializer];
}

return self;
}

- (void)uploadImageData:(NSData *)imageData success:(void (^)(NSString *imageURL))success error:(void (^)(NSError *))errorHandler {

if (![ABEImgurAPIClient isAPIKeySet]) {
// TODO : Refactor out error
NSDictionary *userInfo = @{NSLocalizedDescriptionKey : @"Authentication required for Imgur",
NSLocalizedRecoverySuggestionErrorKey : @"Did you set the imgur client key in your appdelegate using `setupWithRepositoryName:gitHubAccessToken:imgurClientID:`? If you do not have a key, go to https://api.imgur.com/oauth2/addclient and create one. This error has also been printed to the console."};

errorHandler([NSError errorWithDomain:@"no.abello.IssueReporter" code:NSURLErrorUserAuthenticationRequired userInfo:userInfo]);
return;
}

NSMutableDictionary *parameters = @{}.mutableCopy;

parameters[@"image"] = [imageData base64EncodedStringWithOptions:kNilOptions];
parameters[@"type"] = @"base64";
NSURLRequest *request = [self imageUploadRequestForImageData:imageData errorHandler:errorHandler];

[self POST:@"upload" parameters:parameters.copy progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject[@"data"][@"link"]);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (errorHandler) {
if (!request) {
return;
}

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"There was an error while uploading the picture with Imgur's API.");
NSLog(@"Error : %@", error);
errorHandler(error);
return;
}

NSError *jsonError;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];

if (error) {
NSLog(@"There was an error parsing the returned data from Imgur's API.");
NSLog(@"Error : %@", jsonError);
}

NSString *urlString = jsonResponse[@"data"][@"link"];

if (![NSURL URLWithString:urlString]) {
NSLog(@"There was an error getting the url of the uploaded image from the response body of the Imgur API.");
NSLog(@"Returned URL : %@", urlString);
errorHandler(nil);
return;
}
}];

success(urlString);
}] resume];
}

- (NSURLRequest *)imageUploadRequestForImageData:(NSData *)imageData errorHandler:(void (^)(NSError *))errorHandler {
NSDictionary *parameters = @{@"image" : [imageData base64EncodedStringWithOptions:kNilOptions],
@"type" : @"base64"};

NSAssert([NSJSONSerialization isValidJSONObject:parameters], @"JSON Post body generated from issue is not valid JSON.");

NSMutableURLRequest *baseRequest = [[self baseImageUploadRequest] copy];

NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:kNilOptions error:&error];

if (error) {
NSLog(@"There was an error serializing the upload image request body.");
NSLog(@"Error : %@", error);
errorHandler(error);
return nil;
}

[baseRequest setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];
[baseRequest setHTTPBody:data];

return baseRequest;
}

- (NSMutableURLRequest *)baseImageUploadRequest {
if (!_baseImageUploadRequest) {
NSURL *url = [NSURL URLWithString:@"https://api.imgur.com/3/upload"];
_baseImageUploadRequest = [[NSMutableURLRequest alloc] initWithURL:url];

_baseImageUploadRequest.HTTPMethod = @"POST";

[_baseImageUploadRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[_baseImageUploadRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[_baseImageUploadRequest setValue:[NSString stringWithFormat:@"Client-ID %@", self.imgurAPIKey] forHTTPHeaderField:@"Authorization"];
}

return _baseImageUploadRequest;
}

- (void)setImgurAPIKey:(NSString *)imgurAPIKey {
_imgurAPIKey = [imgurAPIKey copy];
[self.requestSerializer setValue:[NSString stringWithFormat:@"Client-ID %@", imgurAPIKey] forHTTPHeaderField:@"Authorization"];
}

@end
4 changes: 3 additions & 1 deletion Pod/Classes/ABEReporterViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ - (void)saveIssue {
}

- (IBAction)userDidTryToHideKeyboard:(id)sender {
[self.view endEditing:false];
dispatch_async(dispatch_get_main_queue(), ^{
[self.view endEditing:false];
});
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
Expand Down

0 comments on commit 86d10e9

Please sign in to comment.