I post here my my little C# solution to get access to the new API.I had some problems running the official example and it is in my opinion to complex to give you an insight about the work flow. My code is more protoypical so that i could understand much better the basic operations. That`s why you will not find any error handling because i run the code in the debugger.
To reproduce the project you should create a new project with Visual Studio. Then you should add Newtonsoft Json (VS 2012 has a nice NuGet installer - menu: Projects / look for online submittals). We add now a new class "ResponseClass". On the "form 1" i have dropped a simple button and added the following code:
Our aim is to make a simple request that should reutrn all EventTypes. The filter should be empty. To create the correct Json i use "Linq to JSON".
We are going now to the ResponseClass form. We add the following using directives:
At the end we insert the EventTypes Class. The class is empty because we have no filter criteria and my first aim was to get a json response so that i know that the request code works.
Here is the code of the ResponseClass. It is based on the official "code snippet" (https://api.developer.betfair.com/se...pageId=3834049)and for the JSON part i used the Json.Net Documentation (http://james.newtonking.com/json/help/index.html) / Samples / LINQ to JSON / Create Json form anonymous Type)
As result of the operation you will get string with all datas but interpreting these datas will be much easier. I struggled a long time to get a response. At moment i think that writing the code in VB is more difficult because the syntax in VB is not suited for JSON. There will be of course a solution, but this will be done by better programmers. I do not claim that i present the best solution but i have not now a solution which i understand and from here i can build on.
To reproduce the project you should create a new project with Visual Studio. Then you should add Newtonsoft Json (VS 2012 has a nice NuGet installer - menu: Projects / look for online submittals). We add now a new class "ResponseClass". On the "form 1" i have dropped a simple button and added the following code:
Code:
private void button1_Click(object sender, EventArgs e) { ResponseClass resp = new ResponseClass(); resp.ResponseTest(); }
We are going now to the ResponseClass form. We add the following using directives:
Code:
using System.Net; using System.IO; using Newtonsoft.Json.Linq;
Code:
public class EventTypes { }
Code:
public class ResponseClass : WebClient { public void ResponseTest() { // 1. Request Object string token = "your_token"; string key = "your_app_key"; // delayed AppKey string address = "https://api.betfair.com/exchange/betting/rest/v1.0/listEventTypes/"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address); request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = 0; request.Headers.Add(HttpRequestHeader.AcceptCharset, "UTF-8"); request.Accept = "application/json"; request.Headers.Add("X-Application", key); request.Headers.Add("X-Authentication", token); // 2. Market Filter (as JSON) JObject postData = CreateFilter(); // 3. Transfer MarketFilter in Request var bytes = Encoding.GetEncoding("UTF-8").GetBytes(postData.ToString()); request.ContentLength = bytes.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } // 4. Get WebResponse WebResponse response = GetWebResponse(request); // 5. Process Response using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { var jsonResponse = reader.ReadToEnd(); string result = jsonResponse; } } public JObject CreateFilter() { List<EventTypes> evt = new List<EventTypes> { new EventTypes { } }; JObject json_obj = JObject.FromObject(new { filter = new { } }); return json_obj; } }
Comment