BetSwift

master

Swift library for Betfair exchange
foulkesjohn/BetSwift

BetSwift

Swift wrapper for Betfair API-NG. Currently only market streaming is supported.

It is built on top of Swift NIO

This is untested in production. Using this as-is would be a bad idea. Memory consumption especially needs to be tested.

Installation

Only Swift 4 is supported.

Due to a change needed upstream in Kitura-Net you can only reference master in a Swift package manifest:

.package(url: "https://github.com/foulkesjohn/BetSwift.git", .branch("master"))

Usage

You will need your Betfair cert in PEM format to run on linux. For mac you will need both pem and p12.

First you will need to get a session token:

let sessionParams = SessionFetchParams(username: "username",
                                       password: "password",
                                       appKey: "appkey",
                                       certPath: "path/to/cert_file.pem",
                                       certPassword: "cert_password")

SessionToken.fetch(params: sessionParams) {
  sessionToken in
  if let sessionToken = sessionToken {
    connect(sessionToken: sessionToken)
  } else {
    print("no session token")
  }
}

Once you have a token you can connect the stream. You will need to create a handler which sends the authentication Op. Once connected you can send a subscription.

Once the stream has connected you can send the subscription:

let marketFilter = MarketFilter(marketIds: nil,
                                bspMarket: nil,
                                bettingTypes: nil,
                                eventTypeIds: ["7", "1"],
                                turnInPlayEnabled: nil,
                                marketTypes: ["WIN"],
                                venues: nil,
                                countryCodes: ["GB"])
let marketDataFilter = MarketDataFilter(fields: ["EX_BEST_OFFERS",
                                                 "EX_MARKET_DEF",
                                                 "EX_LTP"],
                                        ladderLevels: 3)

let marketSubscription = MarketSubscription(id: 1,
                                            marketFilter: marketFilter,
                                            marketDataFilter: marketDataFilter)

let op = Op.marketSubscription(marketSubscription)
ctx.writeAndFlush(wrapOutboundOut(op), promise: nil)

Create your own ChannelInboundHandler with InboundIn type as Op to receive Op's from stream

public class OpHandler: ChannelInboundHandler {
  public typealias InboundIn = Op
  public typealias OutboundOut = Op

  public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
    let op = unwrapInboundIn(data)

    let outOp = ....
    ctx.writeAndFlush(wrapOutboundOut(op), promise: nil)
  }
}

Todo

  • Price cache
  • Order stream
  • Betting operations
  • Performance test

Description

  • Swift Tools
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Oct 22 2024 21:37:55 GMT-0900 (Hawaii-Aleutian Daylight Time)