Giter Club home page Giter Club logo

betswift's Introduction

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

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.