Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bc64b9c
Basic OAuth implementation down. Still need refresh tokens
pyro2927 Nov 14, 2013
d5b6c8b
Breaking Reddit OAuth Client out into it's own subclass
pyro2927 Nov 14, 2013
a7f5f85
Updating podfile so I can use my fork
pyro2927 Nov 14, 2013
d92c497
Refactoring
pyro2927 Nov 14, 2013
5f0797b
Allowing shared client to be a subclass
pyro2927 Nov 14, 2013
a9cffb5
Fixing slash and encoding redirect URI
pyro2927 Nov 14, 2013
9baf4f2
Making sure to set client ID and secret, idiot
pyro2927 Nov 14, 2013
054f843
Merge branch 'master' of github.com:samsymons/RedditKit
pyro2927 Nov 14, 2013
c320440
Finish OAuth login and account creation
pyro2927 Nov 15, 2013
fe1c4ad
Allowing refresh token to be used to obtain new access token
pyro2927 Nov 15, 2013
ec60551
Refucktoring
pyro2927 Nov 15, 2013
e3d7957
Fixing accessors
pyro2927 Nov 15, 2013
61abad2
Merge branch 'master' of github.com:samsymons/RedditKit
pyro2927 Nov 15, 2013
3cd36b1
Adding in automatic token refresh
pyro2927 Nov 16, 2013
4c5b28c
Pulling out subreddits from comment reply JSON
pyro2927 Nov 17, 2013
5a26e24
Updating target branch
pyro2927 Nov 17, 2013
07ab835
Adding in basic OAuth redirect_uri example
pyro2927 Nov 18, 2013
b08e957
Using correct API endpoint. Better error handling
pyro2927 Mar 3, 2014
0f38213
Merge pull request #1 from pyro2927/comment_subreddit
pyro2927 Mar 3, 2014
0629bc9
Targeting master branch
pyro2927 Mar 3, 2014
4b16c5c
Passing back JSON responses for submissions
pyro2927 Mar 3, 2014
85758cc
Using our existing refresh_token if the API doesn't send back a new one
pyro2927 Mar 5, 2014
60e795b
Switching to non-deprecated methods
pyro2927 Mar 5, 2014
ef73c8c
Merging in updates from @samsymons' master branch
pyro2927 Mar 5, 2014
96ee3b3
Adding README note
pyro2927 Mar 5, 2014
d32d0a8
Merge branch 'fix/post_response'
pyro2927 Mar 5, 2014
c411035
Tweaking README
pyro2927 Mar 5, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Classes/Networking/RKClient+Captcha.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ - (NSURLSessionDataTask *)imageForCaptchaIdentifier:(NSString *)identifier compl
NSParameterAssert(identifier);

NSURL *imageURL = [self URLForCaptchaWithIdentifier:identifier];
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:imageURL.absoluteString parameters:nil];
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:imageURL.absoluteString parameters:nil error:nil];

NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!completion)
Expand Down
8 changes: 4 additions & 4 deletions Classes/Networking/RKClient+Links.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ typedef NS_ENUM(NSUInteger, RKSubredditCategory) {
@param captchaValue The optional value of the CAPTCHA you are submitting with this post.
@param completion An optional block to be executed upon request completion. Its only argument is any error that occurred.
*/
- (NSURLSessionDataTask *)submitLinkPostWithTitle:(NSString *)title subreddit:(RKSubreddit *)subreddit URL:(NSURL *)URL captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKCompletionBlock)completion;
- (NSURLSessionDataTask *)submitLinkPostWithTitle:(NSString *)title subreddit:(RKSubreddit *)subreddit URL:(NSURL *)URL captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKObjectCompletionBlock)completion;

/**
Submits a link post.
Expand All @@ -160,7 +160,7 @@ typedef NS_ENUM(NSUInteger, RKSubredditCategory) {
@param captchaValue The optional value of the CAPTCHA you are submitting with this post.
@param completion An optional block to be executed upon request completion. Its only argument is any error that occurred.
*/
- (NSURLSessionDataTask *)submitLinkPostWithTitle:(NSString *)title subredditName:(NSString *)subredditName URL:(NSURL *)URL captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKCompletionBlock)completion;
- (NSURLSessionDataTask *)submitLinkPostWithTitle:(NSString *)title subredditName:(NSString *)subredditName URL:(NSURL *)URL captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKObjectCompletionBlock)completion;

/**
Submits a self post.
Expand All @@ -172,7 +172,7 @@ typedef NS_ENUM(NSUInteger, RKSubredditCategory) {
@param captchaValue The optional value of the CAPTCHA you are submitting with this post.
@param completion An optional block to be executed upon request completion. Its only argument is any error that occurred.
*/
- (NSURLSessionDataTask *)submitSelfPostWithTitle:(NSString *)title subreddit:(RKSubreddit *)subreddit text:(NSString *)text captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKCompletionBlock)completion;
- (NSURLSessionDataTask *)submitSelfPostWithTitle:(NSString *)title subreddit:(RKSubreddit *)subreddit text:(NSString *)text captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKObjectCompletionBlock)completion;

/**
Submits a self post.
Expand All @@ -184,7 +184,7 @@ typedef NS_ENUM(NSUInteger, RKSubredditCategory) {
@param captchaValue The optional value of the CAPTCHA you are submitting with this post.
@param completion An optional block to be executed upon request completion. Its only argument is any error that occurred.
*/
- (NSURLSessionDataTask *)submitSelfPostWithTitle:(NSString *)title subredditName:(NSString *)subredditName text:(NSString *)text captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKCompletionBlock)completion;
- (NSURLSessionDataTask *)submitSelfPostWithTitle:(NSString *)title subredditName:(NSString *)subredditName text:(NSString *)text captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKObjectCompletionBlock)completion;

#pragma mark - Marking NSFW

Expand Down
12 changes: 6 additions & 6 deletions Classes/Networking/RKClient+Links.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ - (NSURLSessionDataTask *)linkWithFullName:(NSString *)fullName completion:(RKOb

#pragma mark - Submitting

- (NSURLSessionDataTask *)submitLinkPostWithTitle:(NSString *)title subreddit:(RKSubreddit *)subreddit URL:(NSURL *)URL captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKCompletionBlock)completion
- (NSURLSessionDataTask *)submitLinkPostWithTitle:(NSString *)title subreddit:(RKSubreddit *)subreddit URL:(NSURL *)URL captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKObjectCompletionBlock)completion
{
return [self submitLinkPostWithTitle:title subredditName:subreddit.title URL:URL captchaIdentifier:captchaIdentifier captchaValue:captchaValue completion:completion];
}

- (NSURLSessionDataTask *)submitLinkPostWithTitle:(NSString *)title subredditName:(NSString *)subredditName URL:(NSURL *)URL captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKCompletionBlock)completion
- (NSURLSessionDataTask *)submitLinkPostWithTitle:(NSString *)title subredditName:(NSString *)subredditName URL:(NSURL *)URL captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKObjectCompletionBlock)completion
{
NSParameterAssert(title);
NSParameterAssert(subredditName);
Expand All @@ -154,15 +154,15 @@ - (NSURLSessionDataTask *)submitLinkPostWithTitle:(NSString *)title subredditNam

[parameters setObject:@"link" forKey:@"kind"];

return [self basicPostTaskWithPath:@"api/submit" parameters:parameters completion:completion];
return [self basicPostAndResponseTaskWithPath:@"api/submit" parameters:parameters completion:completion];
}

- (NSURLSessionDataTask *)submitSelfPostWithTitle:(NSString *)title subreddit:(RKSubreddit *)subreddit text:(NSString *)text captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKCompletionBlock)completion
- (NSURLSessionDataTask *)submitSelfPostWithTitle:(NSString *)title subreddit:(RKSubreddit *)subreddit text:(NSString *)text captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKObjectCompletionBlock)completion
{
return [self submitSelfPostWithTitle:title subredditName:subreddit.title text:text captchaIdentifier:captchaIdentifier captchaValue:captchaValue completion:completion];
}

- (NSURLSessionDataTask *)submitSelfPostWithTitle:(NSString *)title subredditName:(NSString *)subredditName text:(NSString *)text captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKCompletionBlock)completion
- (NSURLSessionDataTask *)submitSelfPostWithTitle:(NSString *)title subredditName:(NSString *)subredditName text:(NSString *)text captchaIdentifier:(NSString *)captchaIdentifier captchaValue:(NSString *)captchaValue completion:(RKObjectCompletionBlock)completion
{
NSParameterAssert(title);
NSParameterAssert(subredditName);
Expand All @@ -178,7 +178,7 @@ - (NSURLSessionDataTask *)submitSelfPostWithTitle:(NSString *)title subredditNam

[parameters setObject:@"self" forKey:@"kind"];

return [self basicPostTaskWithPath:@"api/submit" parameters:parameters completion:completion];
return [self basicPostAndResponseTaskWithPath:@"api/submit" parameters:parameters completion:completion];
}

#pragma mark - Marking NSFW
Expand Down
10 changes: 10 additions & 0 deletions Classes/Networking/RKClient+Requests.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ typedef void(^RKRequestCompletionBlock)(NSHTTPURLResponse *response, id response

@interface RKClient (Requests)

/**
Many of reddit's API methods require a set of parameters, return an error if they fail, and something when they succeed.
This method eliminates much of the repetition when writing methods around these methods.

@param path The path to request.
@param parameters The parameters to pass with the request.
@param completion A block to execute at the end of the request.
*/
- (NSURLSessionDataTask *)basicPostAndResponseTaskWithPath:(NSString *)path parameters:(NSDictionary *)parameters completion:(RKObjectCompletionBlock)completion;

/**
Many of reddit's API methods require a set of parameters and simply return an error if they fail, and nothing (of value, at least) when they succeed.
This method eliminates much of the repetition when writing methods around these methods.
Expand Down
18 changes: 15 additions & 3 deletions Classes/Networking/RKClient+Requests.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@implementation RKClient (Requests)

- (NSURLSessionDataTask *)basicPostTaskWithPath:(NSString *)path parameters:(NSDictionary *)parameters completion:(RKCompletionBlock)completion
- (NSURLSessionDataTask *)basicPostAndResponseTaskWithPath:(NSString *)path parameters:(NSDictionary *)parameters completion:(RKObjectCompletionBlock)completion
{
NSParameterAssert(path);

Expand All @@ -36,14 +36,26 @@ - (NSURLSessionDataTask *)basicPostTaskWithPath:(NSString *)path parameters:(NSD
dispatch_async(dispatch_get_main_queue(), ^{
if (completion)
{
completion([RKClient authenticationRequiredError]);
completion(nil, [RKClient authenticationRequiredError]);
}
});

return nil;
}

return [self postPath:path parameters:parameters completion:^(NSHTTPURLResponse *response, id responseObject, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (completion)
{
completion(responseObject, error);
}
});
}];
}

- (NSURLSessionDataTask *)basicPostTaskWithPath:(NSString *)path parameters:(NSDictionary *)parameters completion:(RKCompletionBlock)completion
{
return [self basicPostAndResponseTaskWithPath:path parameters:parameters completion:^(id object, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (completion)
{
Expand Down Expand Up @@ -186,7 +198,7 @@ - (NSURLSessionDataTask *)taskWithMethod:(NSString *)method path:(NSString *)pat
[alteredParameters setObject:@"json" forKey:@"api_type"];

NSString *URLString = [[NSURL URLWithString:path relativeToURL:self.baseURL] absoluteString];
NSURLRequest *request = [[self requestSerializer] requestWithMethod:method URLString:URLString parameters:[alteredParameters copy]];
NSURLRequest *request = [[self requestSerializer] requestWithMethod:method URLString:URLString parameters:[alteredParameters copy] error:nil];

NSURLSessionDataTask *task = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down
2 changes: 1 addition & 1 deletion Classes/Networking/RKClient+Users.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "RKClient.h"
#import "RKOAuthClient.h"
#import "RKCompletionBlocks.h"

typedef NS_ENUM(NSUInteger, RKUserContentCategory)
Expand Down
2 changes: 1 addition & 1 deletion Classes/Networking/RKClient+Users.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ @implementation RKClient (Users)

- (NSURLSessionDataTask *)currentUserWithCompletion:(RKObjectCompletionBlock)completion
{
return [self getPath:@"api/me.json" parameters:nil completion:^(NSHTTPURLResponse *response, id responseObject, NSError *error) {
return [self getPath:[[self class] meURLPath] parameters:nil completion:^(NSHTTPURLResponse *response, id responseObject, NSError *error) {
if (responseObject)
{
RKUser *account = [RKObjectBuilder objectFromJSON:responseObject];
Expand Down
5 changes: 5 additions & 0 deletions Classes/Networking/RKClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ extern NSString * const RKClientErrorDomain;
*/
+ (NSURL *)APIBaseHTTPSURL;

/**
The API endpoint the client connects to to get your profile information.
*/
+ (NSString *)meURLPath;

/**
Signs into reddit.

Expand Down
11 changes: 8 additions & 3 deletions Classes/Networking/RKClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ + (instancetype)sharedClient
static RKClient *sharedRKClient = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedRKClient = [[RKClient alloc] init];
sharedRKClient = [[[self class] alloc] init];
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making sure a shared client can be instantiated from a subclass.

});

return sharedRKClient;
Expand Down Expand Up @@ -82,9 +82,14 @@ + (NSURL *)APIBaseHTTPSURL
return [NSURL URLWithString:@"https://ssl.reddit.com/"];
}

+ (NSString *)meURLPath
{
return @"api/me.json";
}

#pragma mark - Authentication

- (NSURLSessionDataTask *)signInWithUsername:(NSString *)username password:(NSString *)password completion:(RKCompletionBlock)completion;
- (NSURLSessionDataTask *)signInWithUsername:(NSString *)username password:(NSString *)password completion:(RKCompletionBlock)completion
{
NSParameterAssert(username);
NSParameterAssert(password);
Expand All @@ -94,7 +99,7 @@ - (NSURLSessionDataTask *)signInWithUsername:(NSString *)username password:(NSSt
NSURL *baseURL = [[self class] APIBaseHTTPSURL];
NSString *URLString = [[NSURL URLWithString:@"api/login" relativeToURL:baseURL] absoluteString];

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];

__weak __typeof(self)weakSelf = self;
NSURLSessionDataTask *authenticationTask = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
Expand Down
55 changes: 55 additions & 0 deletions Classes/Networking/RKOAuthClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// RKOAuthClient.h
// Pods
//
// Created by Joseph Pintozzi on 11/14/13.
//
//

#import "RKClient.h"

/**
The different kinds of scope the OAuth client can request
Explainations found here: http://www.reddit.com/dev/api
*/

static NSString * const kOAuthScopeEdit = @"edit";
static NSString * const kOAuthScopeHistory = @"history";
static NSString * const kOAuthScopeIdentity = @"identity";
static NSString * const kOAuthScopeModConfig = @"modconfig";
static NSString * const kOAuthScopeModFlair = @"modflair";
static NSString * const kOAuthScopeModLog = @"modlog";
static NSString * const kOAuthScopeModPosts = @"modposts";
static NSString * const kOAuthScopeMySubreddits = @"mysubreddits";
static NSString * const kOAuthScopePrivateMessages = @"privatemessages";
static NSString * const kOAuthScopeRead = @"read";
static NSString * const kOAuthScopeSave = @"save";
static NSString * const kOAuthScopeSubmit = @"submit";
static NSString * const kOAuthScopeSubscribe = @"subscribe";
static NSString * const kOAuthScopeVote = @"vote";

@interface RKOAuthClient : RKClient

/**
The current clientId and clientSecret for this app.
Only required if authenticating via OAuth
*/
@property (nonatomic, strong) NSString *clientId;
@property (nonatomic, strong) NSString *clientSecret;
@property (nonatomic, strong) NSString *accessToken;
@property (nonatomic, strong) NSString *refreshToken;

/**
Returns a RKClient ready for OAuth
Get a client ID and secret here: https://ssl.reddit.com/prefs/apps
*/
- (id)initWithClientId:(NSString *)clientId clientSecret:(NSString *)clientSecret;

/**
Signs into reddit via OAuth
*/
- (NSURL *)oauthURLWithRedirectURI:(NSString *)redirectURI state:(NSString *)state scope:(NSArray*)scope;
- (NSURLSessionDataTask *)signInWithAccessCode:(NSString *)accessCode redirectURI:(NSString *)redirectURI state:(NSString *)state completion:(RKCompletionBlock)completion;
- (NSURLSessionDataTask *)refreshAccessToken:(NSString*)refreshToken redirectURI:(NSString *)redirectURI state:(NSString *)state completion:(RKCompletionBlock)completion;

@end
Loading