Skip to content

Special Case Integration

guillaumedebavelaere edited this page Oct 4, 2017 · 15 revisions

If you don't know what this page is then you are probably looking for Custom Integration

Configuration

First, configure the TradeItSDK (preferably as soon as possible, e.g. in AppDelegate)

class AppDelegate: UIResponder, UIApplicationDelegate {
    override init() {
        super.init() 
        TradeItSDK.configure(
            apiKey: "tradeit-test-api-key",
            oAuthCallbackUrl: URL(string: "tradeItExampleScheme://completeOAuth")!,
            environment: TradeItEmsTestEnv,
            marketDataService: YourCustomMarketDataService(), // See below for details
            cookieService: YourCustomCookieService() // See below for details
        )

        // Set API base URL/host for each environment like this:
        TradeItSDK.set(host: "https://example.com/myAPI/", forEnvironment: TradeItEmsProductionEnv)
        TradeItSDK.set(host: "https://qa.example.com:1234/myAPI/", forEnvironment: TradeItEmsTestEnv)
        ...
    }
    ...

Custom host configuration

Configure the SDK to point to the Yahoo backend host like this:

TradeItSDK.set(host: "https://yahoobackend.com/yahooAPI/", forEnvironment: TradeItEmsProductionEnv)
TradeItSDK.set(host: "https://qa.yahoobackend.com:1234/yahooAPI/", forEnvironment: TradeItEmsTestEnv)

Cookies

Inject Yahoo user cookies by implementing the CookieService protocol and passing into TradeItSDK.configure().

class TestCookieService: NSObject, CookieService {
    public func getCookies() -> [HTTPCookie] {
        // Get Yahoo user cookie and populate a list of HTTPCookie objects
        let yahooUserCookie: HTTPCookie = ...
        return [yahooUserCookie]
    }
}

Market Data

Inject Yahoo market data by implementing the MarketDataService protocol and passing into TradeItSDK.configure().

class YourCustomMarketDataService: MarketDataService {
    func getQuote(
        symbol: String,
        onSuccess: @escaping (TradeItQuote) -> Void,
        onFailure: @escaping (TradeItErrorResult) -> Void
    ) {
        // Get Yahoo market data for symbol and populate a TradeItQuote
        let yahooMarketData = ...
        let quote = TradeItQuote()
        quote.companyName = yahooMarketData.companyName // "Yahoo! Inc."
        quote.lastPrice = yahooMarketData.lastPrice // 1337.42
        quote.change = yahooMarketData.change // 42.1337
        quote.pctChange = yahooMarketData.percentChange // -123.456
        quote.dateTime = yahooMarketDate.timestamp // "12:34:56"
        onSuccess(quote)
        
        // OR if failed to get market data, create an error
        let error = TradeItErrorResult.error(withSystemMessage: "Some technical reason for failure")
        onFailure(error)
    }
}

Linking

The following code will launch the OAuth flow (use your deep link URL):

TradeItSDK.yahooLauncher.launchOAuth( 
    fromViewController: self, 
    withCallbackUrl: "tradeItExampleScheme://completeYahooOAuth"
)

When you receive the deep link, complete the OAuth flow by passing the callback URL and the top most view controller to the launcher. Like this:

// In AppDelegate
func application(
    _ application: UIApplication,
    open url: URL,
    sourceApplication: String?,
    annotation: Any
) -> Bool {
    // Get the top view controller to pass to the launcher
    TradeItSDK.yahooLauncher.handleOAuthCallback(
        onTopmostViewController: topViewController,
        oAuthCallbackUrl: url,
        onOAuthCompletionSuccessHandler: { presentedViewController, oAuthCallbackUrl, linkedBroker in
            // Dismiss the presented OAuth completion view controller
            presentedViewController.dismiss(
                animated: true,
                completion: {
                    // do whatever you want here like sending the user to the portfolio
                }
            )
        }
    )
}

Trading

And once a broker has been linked, the trading flow can be launched like this:

let order = TradeItOrder()
// Since you will be launching from a QSP screen with buy/sell buttons you can pre-populate symbol/action
order.symbol = "YHOO"
order.action = .buy
TradeItSDK.yahooLauncher.launchTrading(
    fromViewController: self,
    withOrder: order,
    onViewPortfolioTappedHandler: { presentedViewController, linkedBrokerAccount in
        // Go to Yahoo portfolio for linkedBrokerAccount by dismissing presentedViewController
        if let presentingViewController = presentedViewController.presentingViewController {
            presentingViewController.dismiss(animated: true, completion: nil)
            presentingViewController.present(yahooPortfolioViewController, animated: true, completion: nil)
        }

        // OR replace navigationController view stack with portfolio
        if let navController = self.navigationController {
           navController.setViewControllers([yahooPortfolioViewController], animated: true)
        }
    }
)