Git Product home page Git Product logo

fugle-realtime-swift's Introduction

fugle-realtime-swift

license

The Swift implementation for Fugle Realtime API client.

  • HTTP API
  • Websocket API
  • Async/Await support

Installation

Add fugle-realtime-swift as a dependency for your Package.swift file:

dependencies: [
    .package(url: "https://github.com/KeanuPang/fugle-realtime-swift.git", .upToNextMajor(from: "0.1.1"))
]

Add dependency for you target in Package.swift:

dependencies: [
 .product(name: "FugleRealtime", package: "fugle-realtime-swift")
]

Quick demo

Query Intraday meta data by symbol 2884 via HTTP:

import FugleRealtime

do {
    if let result = try await client.getIntraday(MetaData.self, symbol: "2884") {
        print("\(result.toJSONString(prettyPrint: true) ?? "")")

        if let metaData = result.meta {
            print("name: \(metaData.nameZhTw ?? "")")
            print("priceReference: \(metaData.priceReference?.stringValue ?? "")")
            
            /// will print:
            /// name: 玉山金
            /// priceReference: 29.6
        }
    }

    client.shutdownWS()
} catch {}

Subscribe Intraday quote data by symbol 2884 realtime via Websocket:

import FugleRealtime

var promise: EventLoopPromise<Void>?

// Prepare your callback function for quote data
let quoteDataCallback: ((Result<QuoteData, ClientError>) -> Void) = {
    switch $0 {
    case .success(let result):
        print("\(result.quote?.priceAvg?.price?.stringValue ?? ""), \(result.quote?.priceAvg?.at ?? "")")
    case .failure(let failures):
        promise?.fail(failures)
    }
}

// connect and subscribe intraday websocket endpoint
do {
    promise = try await client.streamIntraday(QuoteData.self, symbol: "2884", callback: quoteDataCallback)
    try promise?.futureResult.wait()
} catch {
    client.shutdownWS()
}

Usage

Pass your API token to FugleClient:

let token = "demo"
let client = FugleClient = FugleClient.initWithApiToken(demo)

Or you can put .env file into working folder that contains the following enviroment variables declaration:

FUGLE_API_TOKEN=demo

Now your could use FugleClient directly without passing token parameter:

let client = FugleClient.shared

Calling intraday resource via HTTP endpoint, just pass the Mapped data class to getIntraday() function:

let response: MetaData? = try await client.getIntraday(MetaData.self, symbol: "2884")

If you would like to get dealts data with paging via HTTP endpoint, you can call getIntradayDealts() function:

let response: DealtsData? = try await client.getIntradayDealts(symbol: "2884", pagingLimit: 10)

Intraday Resource:

Type Mapped Data Class
Meta MataData
Quote QuoteData
Chart ChartData
Dealts DealtsData
Volumes VolumesData

For historical stock data, just call getMarketData() function:

let response: CandleData? = try await client.getMarketData(symbol: "2884", from: "2022-04-25", to: "2022-04-29")

Marketdata Resource:

Type Mapped Data Class
Candles CandleData

Bug Report

If you hit any issues while using this SDK, please bug reports on GitHub issue.

fugle-realtime-swift's People

Contributors

keanupang avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.