Skip to content

Commit

Permalink
Refactor collection listing and add get wallets for user
Browse files Browse the repository at this point in the history
  • Loading branch information
mederic committed Sep 17, 2018
1 parent 2c5d966 commit 917dbec
Show file tree
Hide file tree
Showing 26 changed files with 638 additions and 189 deletions.
32 changes: 28 additions & 4 deletions OmiseGO.xcodeproj/project.pbxproj
Expand Up @@ -103,6 +103,11 @@
034F1518214F6713002BB513 /* WalletGetParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 034F1517214F6713002BB513 /* WalletGetParams.swift */; };
034F151A214F6AC0002BB513 /* WalletAdminFixtureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 034F1519214F6AC0002BB513 /* WalletAdminFixtureTests.swift */; };
034F151C214F6E8E002BB513 /* RequestAdminFixtureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 034F151B214F6E8E002BB513 /* RequestAdminFixtureTests.swift */; };
034F1520214F8708002BB513 /* WalletGetForUserParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 034F151F214F8708002BB513 /* WalletGetForUserParams.swift */; };
034F1523214FA205002BB513 /* SortParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 034F1522214FA205002BB513 /* SortParams.swift */; };
034F1525214FA21B002BB513 /* SearchParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 034F1524214FA21B002BB513 /* SearchParams.swift */; };
034F1527214FA228002BB513 /* PaginationParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 034F1526214FA228002BB513 /* PaginationParams.swift */; };
034F1529214FA6FB002BB513 /* PaginatedListParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 034F1528214FA6FB002BB513 /* PaginatedListParams.swift */; };
0379E7E921184BC900E65D4F /* SocketEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0379E7AB21184BC800E65D4F /* SocketEvent.swift */; };
0379E7EA21184BC900E65D4F /* SocketPayload.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0379E7AC21184BC800E65D4F /* SocketPayload.swift */; };
0379E7EB21184BC900E65D4F /* SocketDispatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0379E7AD21184BC800E65D4F /* SocketDispatcher.swift */; };
Expand All @@ -111,7 +116,6 @@
0379E7EE21184BC900E65D4F /* SocketChannel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0379E7B021184BC800E65D4F /* SocketChannel.swift */; };
0379E7EF21184BC900E65D4F /* SocketObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0379E7B121184BC800E65D4F /* SocketObject.swift */; };
0379E7F021184BC900E65D4F /* SocketClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0379E7B221184BC800E65D4F /* SocketClient.swift */; };
0379E7F121184BC900E65D4F /* PaginationParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0379E7B421184BC800E65D4F /* PaginationParams.swift */; };
0379E7F221184BC900E65D4F /* TransactionRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0379E7B521184BC800E65D4F /* TransactionRequest.swift */; };
0379E7F321184BC900E65D4F /* Setting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0379E7B621184BC800E65D4F /* Setting.swift */; };
0379E7F421184BC900E65D4F /* ExchangePair.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0379E7B721184BC800E65D4F /* ExchangePair.swift */; };
Expand Down Expand Up @@ -286,6 +290,11 @@
034F1517214F6713002BB513 /* WalletGetParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletGetParams.swift; sourceTree = "<group>"; };
034F1519214F6AC0002BB513 /* WalletAdminFixtureTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletAdminFixtureTests.swift; sourceTree = "<group>"; };
034F151B214F6E8E002BB513 /* RequestAdminFixtureTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestAdminFixtureTests.swift; sourceTree = "<group>"; };
034F151F214F8708002BB513 /* WalletGetForUserParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletGetForUserParams.swift; sourceTree = "<group>"; };
034F1522214FA205002BB513 /* SortParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SortParams.swift; sourceTree = "<group>"; };
034F1524214FA21B002BB513 /* SearchParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchParams.swift; sourceTree = "<group>"; };
034F1526214FA228002BB513 /* PaginationParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginationParams.swift; sourceTree = "<group>"; };
034F1528214FA6FB002BB513 /* PaginatedListParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaginatedListParams.swift; sourceTree = "<group>"; };
0379E7AB21184BC800E65D4F /* SocketEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEvent.swift; sourceTree = "<group>"; };
0379E7AC21184BC800E65D4F /* SocketPayload.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketPayload.swift; sourceTree = "<group>"; };
0379E7AD21184BC800E65D4F /* SocketDispatcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketDispatcher.swift; sourceTree = "<group>"; };
Expand All @@ -294,7 +303,6 @@
0379E7B021184BC800E65D4F /* SocketChannel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketChannel.swift; sourceTree = "<group>"; };
0379E7B121184BC800E65D4F /* SocketObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketObject.swift; sourceTree = "<group>"; };
0379E7B221184BC800E65D4F /* SocketClient.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketClient.swift; sourceTree = "<group>"; };
0379E7B421184BC800E65D4F /* PaginationParams.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaginationParams.swift; sourceTree = "<group>"; };
0379E7B521184BC800E65D4F /* TransactionRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionRequest.swift; sourceTree = "<group>"; };
0379E7B621184BC800E65D4F /* Setting.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Setting.swift; sourceTree = "<group>"; };
0379E7B721184BC800E65D4F /* ExchangePair.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExchangePair.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -629,10 +637,22 @@
children = (
034F1515214F6617002BB513 /* Wallet+Admin.swift */,
034F1517214F6713002BB513 /* WalletGetParams.swift */,
034F151F214F8708002BB513 /* WalletGetForUserParams.swift */,
);
path = Models;
sourceTree = "<group>";
};
034F1521214FA1F9002BB513 /* CollectionParams */ = {
isa = PBXGroup;
children = (
034F1528214FA6FB002BB513 /* PaginatedListParams.swift */,
034F1522214FA205002BB513 /* SortParams.swift */,
034F1524214FA21B002BB513 /* SearchParams.swift */,
034F1526214FA228002BB513 /* PaginationParams.swift */,
);
path = CollectionParams;
sourceTree = "<group>";
};
0379E7A721184BC800E65D4F /* Source */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -676,6 +696,7 @@
0379E7B321184BC800E65D4F /* Models */ = {
isa = PBXGroup;
children = (
034F1521214FA1F9002BB513 /* CollectionParams */,
0379E7C921184BC800E65D4F /* Account.swift */,
03E72EDC212424B00060E1D7 /* AuthenticationToken.swift */,
0379E7C121184BC800E65D4F /* Avatar.swift */,
Expand All @@ -686,7 +707,6 @@
0379E7C321184BC800E65D4F /* JSONResponse.swift */,
0379E7C021184BC800E65D4F /* OMGError.swift */,
0379E7BF21184BC800E65D4F /* Pagination.swift */,
0379E7B421184BC800E65D4F /* PaginationParams.swift */,
0379E7C221184BC800E65D4F /* Reponse.swift */,
0379E7B621184BC800E65D4F /* Setting.swift */,
0379E7CA21184BC800E65D4F /* Token.swift */,
Expand Down Expand Up @@ -1085,6 +1105,7 @@
03E565B3211AC87500BC9124 /* SocketClient+Client.swift in Sources */,
0379E7F721184BC900E65D4F /* TransactionParams.swift in Sources */,
0379E80621184BC900E65D4F /* Account.swift in Sources */,
034F1523214FA205002BB513 /* SortParams.swift in Sources */,
0379E80B21184BC900E65D4F /* Listable.swift in Sources */,
0379E81321184BC900E65D4F /* QRScannerViewController.swift in Sources */,
0379E80D21184BC900E65D4F /* QRReader.swift in Sources */,
Expand All @@ -1111,6 +1132,7 @@
0320EABF2119499B0006685C /* TransactionRequest+Client.swift in Sources */,
0320EAC72119865A0006685C /* ClientConfiguration.swift in Sources */,
0379E7F921184BC900E65D4F /* User.swift in Sources */,
034F1525214FA21B002BB513 /* SearchParams.swift in Sources */,
0320EAC1211962060006685C /* HTTPClientAPI.swift in Sources */,
03E565B9211AFFCA00BC9124 /* AdminConfiguration.swift in Sources */,
0379E7EC21184BC900E65D4F /* SocketDelegate.swift in Sources */,
Expand All @@ -1120,6 +1142,7 @@
0379E81021184BC900E65D4F /* QRScannerViewModel.swift in Sources */,
0379E81721184BC900E65D4F /* RequestBuilder.swift in Sources */,
0320EAAF211852EB0006685C /* APIAdminEndpoint.swift in Sources */,
034F1520214F8708002BB513 /* WalletGetForUserParams.swift in Sources */,
0379E81921184BC900E65D4F /* Request.swift in Sources */,
0379E81C21184BC900E65D4F /* OMGNumberFormatter.swift in Sources */,
0320EABE2119499B0006685C /* Wallet+Client.swift in Sources */,
Expand All @@ -1142,8 +1165,10 @@
0379E7F821184BC900E65D4F /* TransactionRequestParams.swift in Sources */,
0379E81121184BC900E65D4F /* QRScannerOverlayView.swift in Sources */,
03E72EDB2124177E0060E1D7 /* LoginParams.swift in Sources */,
034F1529214FA6FB002BB513 /* PaginatedListParams.swift in Sources */,
03E565B5211AFBB100BC9124 /* QRScannerViewController+Client.swift in Sources */,
0379E80521184BC900E65D4F /* TransactionListParams.swift in Sources */,
034F1527214FA228002BB513 /* PaginationParams.swift in Sources */,
0379E81221184BC900E65D4F /* QRScannerLoadingView.swift in Sources */,
0379E82221184CE900E65D4F /* CredentialEncoder.swift in Sources */,
0320EABC2119499B0006685C /* Transaction+Client.swift in Sources */,
Expand All @@ -1154,7 +1179,6 @@
0379E7FC21184BC900E65D4F /* Pagination.swift in Sources */,
0379E80F21184BC900E65D4F /* QRGenerator.swift in Sources */,
0379E7ED21184BC900E65D4F /* SocketMessage.swift in Sources */,
0379E7F121184BC900E65D4F /* PaginationParams.swift in Sources */,
0320EACC2119AF420006685C /* QRClientVerifier.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
5 changes: 5 additions & 0 deletions Source/Admin/API/APIAdminEndpoint.swift
Expand Up @@ -11,6 +11,7 @@ enum APIAdminEndpoint: APIEndpoint {
case login(params: LoginParams)
case logout
case getWallet(params: WalletGetParams)
case getWalletsForUser(params: WalletListForUserParams)

var path: String {
switch self {
Expand All @@ -20,6 +21,8 @@ enum APIAdminEndpoint: APIEndpoint {
return "/me.logout"
case .getWallet:
return "/wallet.get"
case .getWalletsForUser:
return "/user.get_wallets"
}
}

Expand All @@ -29,6 +32,8 @@ enum APIAdminEndpoint: APIEndpoint {
return .requestParameters(parameters: parameters)
case let .getWallet(parameters):
return .requestParameters(parameters: parameters)
case let .getWalletsForUser(parameters):
return .requestParameters(parameters: parameters)
default:
return .requestPlain
}
Expand Down
34 changes: 33 additions & 1 deletion Source/Admin/Models/Wallet+Admin.swift
Expand Up @@ -6,13 +6,29 @@
// Copyright © 2017-2018 Omise Go Pte. Ltd. All rights reserved.
//

extension Wallet: Searchable {
public enum SearchableFields: String, KeyEncodable {
case address
case name
case identifier
}
}

extension Wallet: Sortable {
public enum SortableFields: String, KeyEncodable {
case address
case name
case identifier
}
}

extension Wallet {
@discardableResult
/// Get a wallet from its address
///
/// - Parameters:
/// - client: An API client.
/// This client need to be initialized with a ClientConfiguration struct before being used.
/// This client need to be initialized with a AdminConfiguration struct before being used.
/// - params: The WalletGetParams object containing the address of the wallet to retrieve
/// - callback: The closure called when the request is completed
/// - Returns: An optional cancellable request.
Expand All @@ -22,3 +38,19 @@ extension Wallet {
return self.retrieve(using: client, endpoint: APIAdminEndpoint.getWallet(params: params), callback: callback)
}
}

extension Wallet: PaginatedListable {
@discardableResult
/// Get all wallets for a user
///
/// - Parameters:
/// - client: An API client.
/// This client need to be initialized with a AdminConfiguration struct before being used.
/// - callback: The closure called when the request is completed
/// - Returns: An optional cancellable request.
public static func getForUser(using client: HTTPAdminAPI,
params: WalletListForUserParams,
callback: @escaping Wallet.PaginatedListRequestCallback) -> Wallet.PaginatedListRequest? {
return self.list(using: client, endpoint: APIAdminEndpoint.getWalletsForUser(params: params), callback: callback)
}
}
53 changes: 53 additions & 0 deletions Source/Admin/Models/WalletGetForUserParams.swift
@@ -0,0 +1,53 @@
//
// WalletGetForUserParams.swift
// OmiseGO
//
// Created by Mederic Petit on 17/9/18.
// Copyright © 2017-2018 Omise Go Pte. Ltd. All rights reserved.
//

/// Represents a structure used to retrieve wallets for a specific user
public struct WalletListForUserParams {
public let paginatedListParams: PaginatedListParams<Wallet>
public let userId: String?
public let providerUserId: String?

/// Initialize the params used to query a paginated list of wallets belonging to a user from its userId
///
/// - Parameters:
/// - paginatedListParams: The params to use for the pagination
/// - userId: The id of the user
public init(paginatedListParams: PaginatedListParams<Wallet>,
userId: String) {
self.paginatedListParams = paginatedListParams
self.userId = userId
self.providerUserId = nil
}

/// Initialize the params used to query a paginated list of wallets belonging to a user from its providerUserId
///
/// - Parameters:
/// - paginatedListParams: The params to use for the pagination
/// - providerUserId: The id of the user
public init(paginatedListParams: PaginatedListParams<Wallet>,
providerUserId: String) {
self.paginatedListParams = paginatedListParams
self.providerUserId = providerUserId
self.userId = nil
}
}

extension WalletListForUserParams: APIParameters {
private enum CodingKeys: String, CodingKey {
case paginatedListParams
case userId = "id"
case providerUserId = "provider_user_id"
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try paginatedListParams.encode(to: encoder)
try container.encodeIfPresent(userId, forKey: .userId)
try container.encodeIfPresent(providerUserId, forKey: .providerUserId)
}
}
2 changes: 1 addition & 1 deletion Source/Admin/Models/WalletGetParams.swift
Expand Up @@ -9,7 +9,7 @@
/// Represents a structure used to retrieve a wallet from its address
public struct WalletGetParams: APIParameters {
/// The address of the wallet to retrieve
var address: String
public let address: String

/// Initialize the params used to retrive a wallet from its address
///
Expand Down
4 changes: 2 additions & 2 deletions Source/Client/Models/Transaction+Client.swift
Expand Up @@ -23,7 +23,7 @@ extension Transaction: Retrievable {
}
}

extension Transaction: PaginatedListable {
extension Transaction {
@discardableResult
/// Get a paginated list of transaction for the current user
///
Expand All @@ -35,7 +35,7 @@ extension Transaction: PaginatedListable {
/// - Returns: An optional cancellable request.
public static func list(using client: HTTPClientAPI,
params: TransactionListParams,
callback: @escaping Transaction.ListRequestCallback) -> Transaction.ListRequest? {
callback: @escaping Transaction.PaginatedListRequestCallback) -> Transaction.PaginatedListRequest? {
return self.list(using: client, endpoint: APIClientEndpoint.getTransactions(params: params), callback: callback)
}
}
2 changes: 1 addition & 1 deletion Source/Client/Models/Wallet+Client.swift
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2017-2018 Omise Go Pte. Ltd. All rights reserved.
//

extension Wallet: Listable {
extension Wallet {
@discardableResult
/// Get all wallets of the current user
///
Expand Down
75 changes: 75 additions & 0 deletions Source/Core/Models/CollectionParams/PaginatedListParams.swift
@@ -0,0 +1,75 @@
//
// PaginatedListParams.swift
// OmiseGO
//
// Created by Mederic Petit on 17/9/18.
// Copyright © 2017-2018 Omise Go Pte. Ltd. All rights reserved.
//

import UIKit

public struct PaginatedListParams<T: PaginatedListable> {
let sortParams: SortParams<T>
let searchParams: SearchParams<T>?
let paginationParams: PaginationParams

/// Initialize the params used to query a paginated collection
///
/// - Parameters:
/// - page: The page requested (0 and 1 are the same)
/// - perPage: The number of result expected per page
/// - sortBy: The field to sort by
/// - sortDirection: The sort direction (ascending or descending)
public init(page: Int,
perPage: Int,
sortBy: T.SortableFields,
sortDirection: SortDirection) {
self.sortParams = SortParams<T>(sortBy: sortBy, sortDirection: sortDirection)
self.paginationParams = PaginationParams(page: page, perPage: perPage)
self.searchParams = nil
}

/// Initialize the params used to query a paginated collection
///
/// - Parameters:
/// - page: The page requested (0 and 1 are the same)
/// - perPage: The number of result expected per page
/// - searchTerm: The global search term used to search in any of the SearchableFields
/// - sortBy: The field to sort by
/// - sortDirection: The sort direction (ascending or descending)
public init(page: Int,
perPage: Int,
searchTerm: String,
sortBy: T.SortableFields,
sortDirection: SortDirection) {
self.sortParams = SortParams<T>(sortBy: sortBy, sortDirection: sortDirection)
self.searchParams = SearchParams<T>(searchTerm: searchTerm)
self.paginationParams = PaginationParams(page: page, perPage: perPage)
}

/// Initialize the params used to query a paginated collection
///
/// - Parameters:
/// - page: The page requested (0 and 1 are the same)
/// - perPage: The number of result expected per page
/// - searchTerms: A dictionary where each key is a Searchable field that and each value is a search term
/// - sortBy: The field to sort by
/// - sortDirection: The sort direction (ascending or descending)
public init(page: Int,
perPage: Int,
searchTerms: [T.SearchableFields: Any],
sortBy: T.SortableFields,
sortDirection: SortDirection) {
self.sortParams = SortParams<T>(sortBy: sortBy, sortDirection: sortDirection)
self.searchParams = SearchParams<T>(searchTerms: searchTerms)
self.paginationParams = PaginationParams(page: page, perPage: perPage)
}
}

extension PaginatedListParams: APIParameters {
public func encode(to encoder: Encoder) throws {
try self.sortParams.encode(to: encoder)
try self.searchParams?.encode(to: encoder)
try self.paginationParams.encode(to: encoder)
}
}

0 comments on commit 917dbec

Please sign in to comment.