Created
February 24, 2019 04:01
-
-
Save choudeshell/2df0f57d5ed9b116170f16c070c5949f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EnergyCap.Sdk; | |
using EnergyCap.Sdk.Models; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace SdkDemo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Test().Wait(); | |
} | |
public class PagingHandler : DelegatingHandler | |
{ | |
protected async override Task<HttpResponseMessage> SendAsync( | |
HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
if(request.Headers.Contains("i-pageSize")) | |
{ | |
var uri = new UriBuilder(request.RequestUri); | |
uri.Query += $"&pageSize={request.Headers.GetValues("i-pageSize").First()}"; | |
request.RequestUri = uri.Uri; | |
} | |
var response = await base.SendAsync(request, cancellationToken); | |
return response; | |
} | |
} | |
static async Task Test() | |
{ | |
var httpHandler = new DelegatingHandler[] { new PagingHandler() }; | |
var client = new EnergyCapApi(httpHandler); | |
client.BaseUri = new Uri("https://implement.energycap.com"); | |
var loginResult = await client.LoginAsync(new LoginRequest() | |
{ | |
DataSource = "ds", | |
Username = "un", | |
Password = "ps" | |
}); | |
client.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", loginResult.Token); | |
var headers = new Dictionary<string, List<string>>() { | |
{ "i-pageSize", new List<string>(){ "1" } } | |
}; | |
var result = await client.GetPlacesWithHttpMessagesAsync(filter: "", customHeaders: headers); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment