Skip to content

Commit 0c54183

Browse files
authored
Merge pull request #107 from pusher/Noobish1-feature/urlrequest-foundation-types
Noobish1 feature/urlrequest foundation types
2 parents 594fa52 + b36e335 commit 0c54183

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
lines changed

Source/PusherClientOptions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public enum PusherHost {
1919
}
2020

2121
@objc public protocol AuthRequestBuilderProtocol {
22-
func requestFor(socketID: String, channel: PusherChannel) -> NSMutableURLRequest?
22+
func requestFor(socketID: String, channel: PusherChannel) -> URLRequest?
2323
}
2424

2525
public enum AuthMethod {

Source/PusherConnection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ open class PusherConnection: NSObject {
524524

525525
case .authRequestBuilder(authRequestBuilder: let builder):
526526
if let request = builder.requestFor(socketID: socketID, channel: channel) {
527-
sendAuthorisationRequest(request: request as URLRequest, channel: channel, callback: callback)
527+
sendAuthorisationRequest(request: request, channel: channel, callback: callback)
528528

529529
return true
530530
} else {
@@ -613,7 +613,7 @@ open class PusherConnection: NSObject {
613613
- parameter socketID: The socketId of the connection's websocket
614614
- parameter channel: The PusherChannel to authenticate subsciption for
615615

616-
- returns: NSURLRequest object to be used by the function making the auth request
616+
- returns: URLRequest object to be used by the function making the auth request
617617
*/
618618
fileprivate func requestForAuthValue(from endpoint: String, socketID: String, channel: PusherChannel) -> URLRequest {
619619
var request = URLRequest(url: URL(string: endpoint)!)

Source/PusherSwift.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ let CLIENT_NAME = "pusher-websocket-swift"
101101
channelName: String,
102102
onMemberAdded: ((PusherPresenceChannelMember) -> ())? = nil,
103103
onMemberRemoved: ((PusherPresenceChannelMember) -> ())? = nil) -> PusherPresenceChannel {
104-
return self.connection.subscribeToPresenceChannel(channelName: channelName, onMemberAdded: onMemberAdded, onMemberRemoved: onMemberRemoved)
104+
return self.connection.subscribeToPresenceChannel(channelName: channelName, onMemberAdded: onMemberAdded, onMemberRemoved: onMemberRemoved)
105105
}
106106

107107
/**

Tests/AuthenticationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ class AuthenticationTests: XCTestCase {
111111
func testAuthorizationUsingSomethingConformingToTheAuthRequestBuilderProtocol() {
112112

113113
class AuthRequestBuilder: AuthRequestBuilderProtocol {
114-
func requestFor(socketID: String, channel: PusherChannel) -> NSMutableURLRequest? {
115-
let request = NSMutableURLRequest(url: URL(string: "http://localhost:9292/builder")!)
114+
func requestFor(socketID: String, channel: PusherChannel) -> URLRequest? {
115+
var request = URLRequest(url: URL(string: "http://localhost:9292/builder")!)
116116
request.httpMethod = "POST"
117117
request.httpBody = "socket_id=\(socketID)&channel_name=\(channel.name)".data(using: String.Encoding.utf8)
118118
request.addValue("myToken", forHTTPHeaderField: "Authorization")

iOS Example Obj-C/iOS Example Obj-C/ViewController.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@
88

99
#import "ViewController.h"
1010

11+
@interface AuthRequestBuilder : NSObject <AuthRequestBuilderProtocol>
12+
13+
- (NSMutableURLRequest*)requestForSocketID:(NSString *)socketID channel:(PusherChannel *)channel;
14+
15+
@end
16+
17+
@implementation AuthRequestBuilder
18+
19+
- (NSMutableURLRequest*)requestForSocketID:(NSString *)socketID channel:(PusherChannel *)channel {
20+
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [[NSURL alloc] initWithString:@"http://localhost:9292/pusher/auth"]];
21+
NSString *dataStr = [NSString stringWithFormat: @"socket_id=%@&channel_name=%@", socketID, [channel name]];
22+
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
23+
request.HTTPBody = data;
24+
request.HTTPMethod = @"POST";
25+
return request;
26+
}
27+
28+
@end
29+
1130
@interface ViewController ()
1231

1332
@end
@@ -20,6 +39,10 @@ - (void)viewDidLoad {
2039
OCAuthMethod *authMethod = [[OCAuthMethod alloc] initWithSecret:@"YOUR_APP_SECRET"];
2140
PusherClientOptions *options = [[PusherClientOptions alloc] initWithAuthMethod:authMethod];
2241

42+
// Use this if you want to try out your auth Endpoint
43+
// OCAuthMethod *endpointAuthMethod = [[OCAuthMethod alloc] initWithAuthRequestBuilder:[[AuthRequestBuilder alloc] init]];
44+
// PusherClientOptions *optionsWithEndpoint = [[PusherClientOptions alloc] initWithAuthMethod:endpointAuthMethod];
45+
2346
self.client = [[Pusher alloc] initWithAppKey:@"YOUR_APP_KEY" options:options];
2447
self.client.connection.delegate = self;
2548

@@ -88,3 +111,4 @@ - (void)didReceiveMemoryWarning {
88111
}
89112

90113
@end
114+

iOS Example Swift/iOS Example Swift/ViewController.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ class ViewController: UIViewController, PusherDelegate {
2525

2626
// Only use your secret here for testing or if you're sure that there's
2727
// no security risk
28-
let pusherClientOptions = PusherClientOptions(authMethod: .inline(secret: "daef58559fdd0aba8b63"))
29-
pusher = Pusher(key: "568d5a3851502158a022", options: pusherClientOptions)
28+
let pusherClientOptions = PusherClientOptions(authMethod: .inline(secret: "YOUR_APP_SECRET"))
29+
pusher = Pusher(key: "YOUR_APP_KEY", options: pusherClientOptions)
30+
31+
// Use this if you want to try out your auth endpoint
32+
// let optionsWithEndpoint = PusherClientOptions(
33+
// authMethod: AuthMethod.authRequestBuilder(authRequestBuilder: AuthRequestBuilder())
34+
// )
35+
// pusher = Pusher(key: "YOUR_APP_KEY", options: optionsWithEndpoint)
3036

3137
pusher.delegate = self
3238

@@ -77,3 +83,11 @@ class ViewController: UIViewController, PusherDelegate {
7783
}
7884
}
7985

86+
class AuthRequestBuilder: AuthRequestBuilderProtocol {
87+
func requestFor(socketID: String, channel: PusherChannel) -> URLRequest? {
88+
var request = URLRequest(url: URL(string: "http://localhost:9292/pusher/auth")!)
89+
request.httpMethod = "POST"
90+
request.httpBody = "socket_id=\(socketID)&channel_name=\(channel.name)".data(using: String.Encoding.utf8)
91+
return request
92+
}
93+
}

0 commit comments

Comments
 (0)