AlgoTrader / betfair-sports-api

Library is discontinued
MIT License
55 stars 15 forks source link

Use of inv.execute() #11

Open JustMee opened 11 years ago

JustMee commented 11 years ago

Great library, but I am having trouble debugging some code that includes use of inv.execute, so trying to understand this better. Where is the execute() method defined and why are you using this approach, rather than say a direct function call?

AlgoTrader commented 11 years ago

All the Betfair invocations are instances generated by BetfairInvocation constructor in betfair_invocation.js file. The sematics of using invocation is following:

1) invocation is created by apropriate session.something() call. All the calls return instances generated by BetfairInvocation

2) execute() method prepares HTTP request, get HTTP response and parse it.

3) after the call is executed, invocation can be inspected for result, raw XML request and response, duration, etc.

You are right, everything could be done in a single function with a callback. The reason that in some cases I can create dozens of invocations then call all them in a batch. Personally, I do not like constructors do lots of work. For example:

var invocation = session.keepAlive();

is not supposed to do all the work. It just create the invocation object. Then, the invocation is executed by calling execute method, the P in RPC is procedure, and procedures are naturally executed. After executing the invocation you can do cosole.log(invocation) and you will see lots of debug stuff there