public static string GetMethodName(this BetfairMethod betfairMethod) { if (_methodMappings.TryGetValue(betfairMethod, out string methodName)) { return(methodName); } return(null); }
public async Task <T> Invoke <T>(BetfairMethod method, BetfairRequestParameters requestParameters) { if (method == BetfairMethod.UnknownMethod) { throw new ArgumentOutOfRangeException("Method must be defined", "method"); } string methodName = BetfairMethodExtensions.GetMethodName(method); IDictionary <string, object> args = new Dictionary <string, object>() { { "filter", requestParameters?.Filter ?? new MarketFilter() } }; if (requestParameters != null && requestParameters.MarketProjections != null && requestParameters.MarketProjections.Count > 0) { args["marketProjection"] = requestParameters.MarketProjections; } if (requestParameters != null && requestParameters.MaxResults != null && requestParameters.MaxResults > 0 && requestParameters.MaxResults <= 1000) { args["maxResults"] = requestParameters.MaxResults; } LoginResponse loginResponse = await _authenticationClient.Login(); if (loginResponse == null) { throw new AuthenticationException("LoginResponse is null"); } else if (loginResponse.SessionToken == null) { throw new AuthenticationException("LoginResponse does not contain SessionToken"); } else { _customHeaders[_sessionTokenHeader] = loginResponse.SessionToken; } var json = JsonConvert.SerializeObject(new JsonRequest { Method = _methodPrefix + methodName, Id = 1, Params = args }, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); var content = new StringContent(json, Encoding.UTF8, _requestContentType); foreach (var header in _customHeaders) { content.Headers.Add(header.Key, header.Value); } var timeoutPolicy = _registry.Get <IAsyncPolicy>("thirtySecondTimeoutPolicy"); var asyncGetPolicy = _registry.Get <IAsyncPolicy <HttpResponseMessage> >("thriceTriplingRetryPolicy"); IAsyncPolicy <JsonResponse <T> > readStreamPolicy = Policy.HandleResult <JsonResponse <T> >(jr => jr.HasError) .Or <TimeoutRejectedException>() .WaitAndRetryAsync(_retryDelays); JsonResponse <T> response = await readStreamPolicy.ExecuteAsync(async() => { HttpResponseMessage responseMessage = await asyncGetPolicy.ExecuteAsync(async() => await timeoutPolicy.ExecuteAsync(async token => await _httpClient.PostAsync("", content, token), CancellationToken.None)); if (!responseMessage.IsSuccessStatusCode) { throw new HttpResponseException(responseMessage); } string responseContent = await responseMessage.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <JsonResponse <T> >(responseContent)); }); if (response.HasError) { throw ReconstituteException(response.Error); } else { return(response.Result); } }