Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1 from WatchBeam/feature/queue-requests
Browse files Browse the repository at this point in the history
Queue requests while waiting for a JWT
  • Loading branch information
jackcook authored Dec 16, 2016
2 parents a152171 + 811c775 commit fb3dced
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions BeamAPI.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Pod::Spec.new do |s|
s.name = "BeamAPI"
s.version = "1.4.7"
s.version = "1.4.8"
s.summary = "An interface to communicate with Beam's backend."
s.homepage = "https://github.com/WatchBeam/beam-client-swift"
s.license = "MIT"
s.author = { "Jack Cook" => "jack@beam.pro" }

s.requires_arc = true
s.ios.deployment_target = "8.2"
s.source = { :git => "https://github.com/WatchBeam/beam-client-swift.git", :tag => "1.4.7" }
s.source = { :git => "https://github.com/WatchBeam/beam-client-swift.git", :tag => "1.4.8" }
s.source_files = "Pod/Classes/**/*"

s.dependency "Starscream", "~> 2.0"
Expand Down
38 changes: 38 additions & 0 deletions Pod/Classes/Utilities/BeamRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ public class BeamRequest {
/// A delegate for the BeamRequest class.
public static var delegate: BeamRequestDelegate?

/// Requests to be executed as soon as a JWT is retrieved.
static var pendingRequests = [BeamRequestParameters]()

/// True if a JWT is currently being requested.
static var requestingJWT = false {
didSet {
if !requestingJWT {
for parameters in pendingRequests {
dataRequest(parameters)
}

pendingRequests = [BeamRequestParameters]()
}
}
}

/// The version of the app, to be used in request user agents.
public static var version = 0.1

Expand Down Expand Up @@ -57,6 +73,15 @@ public class BeamRequest {
}
}

/**
Uses a BeamRequestParameters struct to execute a data request.

:param: parameters The parameters to be passed.
*/
class func dataRequest(_ parameters: BeamRequestParameters) {
dataRequest(parameters.baseURL, requestType: parameters.requestType, headers: parameters.headers, params: parameters.params, body: parameters.body, options: parameters.options, completion: parameters.completion)
}

/**
Retrieves data from Beam's servers.

Expand Down Expand Up @@ -121,6 +146,13 @@ public class BeamRequest {
return
}

guard !requestingJWT else {
let parameters = BeamRequestParameters(baseURL: baseURL, requestType: requestType, headers: headers, params: params, body: body, options: options, completion: completion)
pendingRequests.append(parameters)

return
}

if options.contains(.storeCookies) {
let cookies = HTTPCookie.cookies(withResponseHeaderFields: response.allHeaderFields as! [String : String], for: url)
var storedCookies = [[HTTPCookiePropertyKey: Any]]()
Expand Down Expand Up @@ -188,8 +220,12 @@ public class BeamRequest {
}
case 401:
if json["message"] == "Invalid token" {
requestingJWT = true

if UserDefaults.standard.object(forKey: "Cookies") != nil {
BeamClient.sharedClient.jwt.generateJWTGrant { (error) in
requestingJWT = false

guard error == nil else {
completion?(data, .invalidCredentials)
return
Expand All @@ -199,6 +235,8 @@ public class BeamRequest {
}
} else {
delegate?.requestNewJWT { (error) in
requestingJWT = false

guard error == nil else {
completion?(data, .invalidCredentials)
return
Expand Down
32 changes: 32 additions & 0 deletions Pod/Classes/Utilities/BeamRequestParameters.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// BeamRequestParameters.swift
// Pods
//
// Created by Jack Cook on 16/12/2016.
//
//

/// Parameters that can be passed to BeamRequest.dataRequest
struct BeamRequestParameters {

/// The URL of the data being retrieved
var baseURL: String

/// The type of request being made
var requestType: String

/// The HTTP headers to be used in the request
var headers: [String: String]

/// The URL parameters to be used in the request
var params: [String: String]

/// The request body
var body: AnyObject?

/// Any special operations that should be performed for this request
var options: BeamRequestOptions

/// An optional completion block with retrieved data
var completion: ((_ data: Data?, _ error: BeamRequestError?) -> Void)?
}

0 comments on commit fb3dced

Please sign in to comment.