Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribe and UnSubscribe User methods added #660

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions swift-sdk/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ enum Const {
enum Http {
static let GET = "GET"
static let POST = "POST"
static let PATCH = "PATCH"
static let DELETE = "DELETE"
}

enum Path {
Expand All @@ -39,6 +41,7 @@ enum Const {
static let updateEmail = "users/updateEmail"
static let updateSubscriptions = "users/updateSubscriptions"
static let getRemoteConfiguration = "mobile/getRemoteConfiguration"
static let subscriptions = "subscriptions/"
}

public enum UserDefault {
Expand Down
10 changes: 10 additions & 0 deletions swift-sdk/Internal/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,14 @@ extension ApiClient: ApiClientProtocol {
let result = createRequestCreator().flatMap { $0.createGetRemoteConfigurationRequest() }
return send(iterableRequestResult: result)
}

func subscribeUser(_email: String?, userId: String?, subscriptionId: String, subscriptionGroup: String) -> Pending<SendRequestValue, SendRequestError> {
let result = createRequestCreator().flatMap { $0.createSubscribeUserRequest(_email: _email, userId: userId, subscriptionId: subscriptionId, subscriptionGroup: subscriptionGroup) }
return send(iterableRequestResult: result)
}

func unSubscribeUser(_email: String?, userId: String?, subscriptionId: String, subscriptionGroup: String) -> Pending<SendRequestValue, SendRequestError> {
let result = createRequestCreator().flatMap { $0.createUnSubscribeUserRequest(_email: _email, userId: userId, subscriptionId: subscriptionId, subscriptionGroup: subscriptionGroup) }
return send(iterableRequestResult: result)
}
}
4 changes: 4 additions & 0 deletions swift-sdk/Internal/ApiClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ protocol ApiClientProtocol: AnyObject {
func disableDevice(forAllUsers allUsers: Bool, hexToken: String) -> Pending<SendRequestValue, SendRequestError>

func getRemoteConfiguration() -> Pending<RemoteConfiguration, SendRequestError>

@discardableResult func subscribeUser(_email: String?, userId: String?, subscriptionId: String, subscriptionGroup: String) -> Pending<SendRequestValue, SendRequestError>

@discardableResult func unSubscribeUser(_email: String?, userId: String?, subscriptionId: String, subscriptionGroup: String) -> Pending<SendRequestValue, SendRequestError>
}
20 changes: 20 additions & 0 deletions swift-sdk/Internal/InternalIterableAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,26 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
onFailure: onFailure)
}

@discardableResult
func subscribeUser(_email: String?,
userId: String?,
subscriptionId: String,
subscriptionGroup: String,
onSuccess: OnSuccessHandler? = nil,
onFailure: OnFailureHandler? = nil) -> Pending<SendRequestValue, SendRequestError> {
requestHandler.subscribeUser(_email: _email, userId: userId, subscriptionId: subscriptionId, subscriptionGroup: subscriptionGroup, onSuccess: onSuccess, onFailure: onFailure)
}

@discardableResult
func unSubscribeUser(_email: String?,
userId: String?,
subscriptionId: String,
subscriptionGroup: String,
onSuccess: OnSuccessHandler? = nil,
onFailure: OnFailureHandler? = nil) -> Pending<SendRequestValue, SendRequestError> {
requestHandler.unSubscribeUser(_email: _email, userId: userId, subscriptionId: subscriptionId, subscriptionGroup: subscriptionGroup, onSuccess: onSuccess, onFailure: onFailure)
}

// MARK: - Private/Internal

private var config: IterableConfig
Expand Down
18 changes: 16 additions & 2 deletions swift-sdk/Internal/IterableAPICallRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ struct IterableAPICallRequest {
case let .post(postRequest):
return IterableRequestUtil.createPostRequest(forApiEndPoint: endpoint,
path: postRequest.path,
headers: createIterableHeaders(sentAt: sentAt,
processorType: processorType),
headers: createIterableHeaders(sentAt: sentAt, processorType: processorType),
args: postRequest.args,
body: postRequest.body)
case let .patch(patchRequest):
return IterableRequestUtil.createPatchRequest(forApiEndPoint: endpoint,
path: patchRequest.path,
headers: createIterableHeaders(sentAt: sentAt, processorType: processorType),
args: patchRequest.args)
case let .delete(deleteRequest):
return IterableRequestUtil.createDeleteRequest(forApiEndPoint: endpoint,
path: deleteRequest.path,
headers: createIterableHeaders(sentAt: sentAt,
processorType: processorType),
args: deleteRequest.args)
}
}

Expand All @@ -42,6 +52,10 @@ struct IterableAPICallRequest {
return request.path
case .post(let request):
return request.path
case .patch(let request):
return request.path
case .delete(let request):
return request.path
}
}

Expand Down
26 changes: 26 additions & 0 deletions swift-sdk/Internal/IterableRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Foundation
enum IterableRequest {
case get(GetRequest)
case post(PostRequest)
case patch(PatchRequest)
case delete(DeleteRequest)
}

extension IterableRequest: Codable {
Expand All @@ -25,9 +27,15 @@ extension IterableRequest: Codable {
case IterableRequest.requestTypeGet:
let request = try container.decode(GetRequest.self, forKey: .value)
self = .get(request)
case IterableRequest.requestTypePatch:
let request = try container.decode(PatchRequest.self, forKey: .value)
self = .patch(request)
case IterableRequest.requestTypePost:
let request = try container.decode(PostRequest.self, forKey: .value)
self = .post(request)
case IterableRequest.requestTypeDelete:
let request = try container.decode(DeleteRequest.self, forKey: .value)
self = .delete(request)
default:
throw IterableError.general(description: "Unknown request type: \(type)")
}
Expand All @@ -39,9 +47,15 @@ extension IterableRequest: Codable {
case let .get(request):
try container.encode(IterableRequest.requestTypeGet, forKey: .type)
try container.encode(request, forKey: .value)
case let .patch(request):
try container.encode(IterableRequest.requestTypePatch, forKey: .type)
try container.encode(request, forKey: .value)
case let .post(request):
try container.encode(IterableRequest.requestTypePost, forKey: .type)
try container.encode(request, forKey: .value)
case let .delete(request):
try container.encode(IterableRequest.requestTypeDelete, forKey: .type)
try container.encode(request, forKey: .value)
}
}

Expand All @@ -54,14 +68,26 @@ extension IterableRequest: Codable {
}

private static let requestTypeGet = "get"
private static let requestTypePatch = "patch"
private static let requestTypePost = "post"
private static let requestTypeDelete = "delete"
}

struct GetRequest: Codable {
let path: String
let args: [String: String]?
}

struct PatchRequest: Codable {
let path: String
let args: [String: String]?
}

struct DeleteRequest: Codable {
let path: String
let args: [String: String]?
}

struct PostRequest {
let path: String
let args: [String: String]?
Expand Down
51 changes: 51 additions & 0 deletions swift-sdk/Internal/IterableRequestUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ struct IterableRequestUtil {
return request
}

static func createPatchRequest(forApiEndPoint apiEndPoint: String,
path: String,
headers: [String: String]? = nil,
args: [String: String]? = nil) -> URLRequest? {
createPatchRequest1(forApiEndPoint: apiEndPoint,
path: path,
headers: headers,
args: args)
}

static func createDeleteRequest(forApiEndPoint apiEndPoint: String,
path: String,
headers: [String: String]? = nil,
args: [String: String]? = nil) -> URLRequest? {
createDeleteRequest1(forApiEndPoint: apiEndPoint,
path: path,
headers: headers,
args: args)
}

static func createPostRequest(forApiEndPoint apiEndPoint: String,
path: String,
headers: [String: String]? = nil,
Expand Down Expand Up @@ -64,6 +84,37 @@ struct IterableRequestUtil {
return request
}

static func createPatchRequest1(forApiEndPoint apiEndPoint: String,
path: String,
headers: [String: String]? = nil,
args: [String: String]? = nil) -> URLRequest? {
guard let url = getUrlComponents(forApiEndPoint: apiEndPoint, path: path, args: args)?.url else {
return nil
}

var request = URLRequest(url: url)
addHeaders(headers: headers, toRequest: &request)
request.httpMethod = Const.Http.PATCH

return request
}

static func createDeleteRequest1(forApiEndPoint apiEndPoint: String,
path: String,
headers: [String: String]? = nil,
args: [String: String]? = nil,
body: Data? = nil) -> URLRequest? {
guard let url = getUrlComponents(forApiEndPoint: apiEndPoint, path: path, args: args)?.url else {
return nil
}

var request = URLRequest(url: url)
addHeaders(headers: headers, toRequest: &request)
request.httpMethod = Const.Http.DELETE

return request
}

static func dictToJsonData(_ dict: [AnyHashable: Any]?) -> Data? {
guard let dict = dict else {
return nil
Expand Down
26 changes: 26 additions & 0 deletions swift-sdk/Internal/OnlineRequestProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,32 @@ struct OnlineRequestProcessor: RequestProcessorProtocol {
requestIdentifier: "inAppConsumeWithSource")
}

@discardableResult
func subscribeUser(_email: String?,
userId: String?,
subscriptionId: String,
subscriptionGroup: String,
onSuccess: OnSuccessHandler? = nil,
onFailure: OnFailureHandler? = nil) -> Pending<SendRequestValue, SendRequestError> {
sendRequest(requestProvider: { apiClient.subscribeUser(_email: _email, userId: userId, subscriptionId: subscriptionId, subscriptionGroup: subscriptionGroup)},
successHandler: onSuccess,
failureHandler: onFailure,
requestIdentifier: "subscribeUser")
}

@discardableResult
func unSubscribeUser(_email: String?,
userId: String?,
subscriptionId: String,
subscriptionGroup: String,
onSuccess: OnSuccessHandler? = nil,
onFailure: OnFailureHandler? = nil) -> Pending<SendRequestValue, SendRequestError> {
sendRequest(requestProvider: { apiClient.unSubscribeUser(_email: _email, userId: userId, subscriptionId: subscriptionId, subscriptionGroup: subscriptionGroup)},
successHandler: onSuccess,
failureHandler: onFailure,
requestIdentifier: "unSubscribeUser")
}

func getRemoteConfiguration() -> Pending<RemoteConfiguration, SendRequestError> {
apiClient.getRemoteConfiguration()
}
Expand Down
44 changes: 44 additions & 0 deletions swift-sdk/Internal/RequestCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,40 @@ struct RequestCreator {
return .success(.get(createGetRequest(forPath: Const.Path.getRemoteConfiguration, withArgs: args as! [String: String])))
}

func createSubscribeUserRequest(_email: String?, userId: String?, subscriptionId: String, subscriptionGroup: String) -> Result<IterableRequest, IterableError> {
if case .none = auth.emailOrUserId {
ITBError(Self.authMissingMessage)
return .failure(IterableError.general(description: Self.authMissingMessage))
}

var endpoint: String

if !userId!.isEmpty {
endpoint = Const.Path.subscriptions + subscriptionGroup + "/" + subscriptionId + "/byUserId/" + userId!
} else {
endpoint = Const.Path.subscriptions + subscriptionGroup + "/" + subscriptionId + "/user/" + _email!
}

return .success(.patch(createPatchRequest(forPath: endpoint, withArgs: [String: String]())))
}

func createUnSubscribeUserRequest(_email: String?, userId: String?, subscriptionId: String, subscriptionGroup: String) -> Result<IterableRequest, IterableError> {
if case .none = auth.emailOrUserId {
ITBError(Self.authMissingMessage)
return .failure(IterableError.general(description: Self.authMissingMessage))
}

var endpoint: String

if !userId!.isEmpty {
endpoint = Const.Path.subscriptions + subscriptionGroup + "/" + subscriptionId + "/byUserId/" + userId!
} else {
endpoint = Const.Path.subscriptions + subscriptionGroup + "/" + subscriptionId + "/user/" + _email!
}

return .success(.delete(createDeleteRequest(forPath: endpoint, withArgs: [String: String]())))
}

// MARK: - PRIVATE

private static let authMissingMessage = "Both email and userId are nil"
Expand All @@ -457,6 +491,16 @@ struct RequestCreator {
args: args)
}

private func createPatchRequest(forPath path: String, withArgs args: [String: String]?) -> PatchRequest {
PatchRequest(path: path,
args: args)
}

private func createDeleteRequest(forPath path: String, withArgs args: [String: String]?) -> DeleteRequest {
DeleteRequest(path: path,
args: args)
}

private static func pushServicePlatformToString(_ pushServicePlatform: PushServicePlatform, apnsType: APNSType) -> String {
switch pushServicePlatform {
case .production:
Expand Down
30 changes: 30 additions & 0 deletions swift-sdk/Internal/RequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,36 @@ class RequestHandler: RequestHandlerProtocol {
}
}

@discardableResult
func subscribeUser(_email: String?,
userId: String?,
subscriptionId: String,
subscriptionGroup: String,
onSuccess: OnSuccessHandler?,
onFailure: OnFailureHandler?) -> Pending<SendRequestValue, SendRequestError> {
onlineProcessor.subscribeUser(_email: _email,
userId: userId,
subscriptionId: subscriptionId,
subscriptionGroup: subscriptionGroup,
onSuccess: onSuccess,
onFailure: onFailure)
}

@discardableResult
func unSubscribeUser(_email: String?,
userId: String?,
subscriptionId: String,
subscriptionGroup: String,
onSuccess: OnSuccessHandler?,
onFailure: OnFailureHandler?) -> Pending<SendRequestValue, SendRequestError> {
onlineProcessor.unSubscribeUser(_email: _email,
userId: userId,
subscriptionId: subscriptionId,
subscriptionGroup: subscriptionGroup,
onSuccess: onSuccess,
onFailure: onFailure)
}

func getRemoteConfiguration() -> Pending<RemoteConfiguration, SendRequestError> {
onlineProcessor.getRemoteConfiguration()
}
Expand Down
16 changes: 16 additions & 0 deletions swift-sdk/Internal/RequestHandlerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ protocol RequestHandlerProtocol: AnyObject {
inboxSessionId: String?,
onSuccess: OnSuccessHandler?,
onFailure: OnFailureHandler?) -> Pending<SendRequestValue, SendRequestError>

@discardableResult
func subscribeUser(_email: String?,
userId: String?,
subscriptionId: String,
subscriptionGroup: String,
onSuccess: OnSuccessHandler?,
onFailure: OnFailureHandler?) -> Pending<SendRequestValue, SendRequestError>

@discardableResult
func unSubscribeUser(_email: String?,
userId: String?,
subscriptionId: String,
subscriptionGroup: String,
onSuccess: OnSuccessHandler?,
onFailure: OnFailureHandler?) -> Pending<SendRequestValue, SendRequestError>

func handleLogout() throws

Expand Down
Loading