Contents

foulkesjohn/betswift

Swift wrapper for [Betfair API-NG](http://docs.developer.betfair.com/docs/display/1smk3cen4v3lu3yomq5qye0ni). Currently only market streaming is supported.

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

Package Metadata

Repository: foulkesjohn/betswift

Default branch: master

README: README.md