This package is unofficial and is NOT in any way supported by Daybreak Games.
All use of game data and other content must comply with Daybreak Games Intellectual Property policy and Daybreak Game Company's Terms of Service.
DaybreakGames.Census is a library that makes it much easier to use the Census API provided by Daybreak Games Company.
For a full demonstration check out the DemoApp!
- Installing
- Creating a new query
- Returning data from the query
- Setting global parameters
- Using multiple namespaces or service ids
- Cancelling a query
- Configuring the HTTP client and timeout
- Logging
- Defining a condition
- Setting a language
- Show certain fields
- Hide certain fields
- Set a limit for number of rows to return
- Set the starting row
- Add a resolve
- Join to another service
- Tree results on a field
- Getting the url of the query
- Streaming data
DaybreakGames.Census is available via NuGet.
To inject DaybreakGames.Census services into your project add services.AddCensusServices();
to your ConfigureServices block.
To start creating queries, use ICensusQueryFactory via DI and call
ICensusQueryFactory.Create(<serviceName>) to get a CensusQuery object.
public MyClass(ICensusQueryFactory censusFactory)
{
var query = censusFactory.Create("character");
query.SetServiceId("my-service-id")
.SetServiceNamespace("ps2");
}SetServiceId and SetServiceNamespace are optional. If not set than the values
provided in AddCensusServices() are used. Otherwise it defaults to ServiceId="example"
and ServiceNamespace="ps2:v2".
There are three methods of query resolution: GetAsync<T>(), GetListAsync<T>(), and
GetBatchAsync<T>(). A model can be passed in that deserializes to the expected census model
that matches Pascal Case versions of the census properties (i.e character_id becomes
CharacterId). For other cases, the [JsonProperty] decorators should be respected.
If a model is not given than the default return type is as a JToken.
var resultJToken = await query.GetAsync();If your application is always using the same service id and namespace you might as well set it globally so you don't have to configure it with every query.
public void ConfigureServices(IServiceCollection services)
{
services.AddCensusServices(options =>
{
options.CensusServiceId = "my-service-id";
options.CensusServiceNamespace = "ps2";
});
}To talk to several namespaces (or use a different service id per namespace) in the same
application, call AddCensusServices once per configuration and give each one a name. Each named
registration gets its own CensusOptions and its own HttpClient, and its ICensusClient,
ICensusQueryFactory and ICensusStreamClient are registered as keyed services under that name.
services.AddCensusServices("pc", options =>
{
options.CensusServiceId = "my-pc-service-id";
options.CensusServiceNamespace = "ps2:v2";
})
.AddCensusServices("ps4us", options =>
{
options.CensusServiceId = "my-ps4-service-id";
options.CensusServiceNamespace = "ps2ps4us:v2";
options.CensusTimeout = TimeSpan.FromSeconds(30);
});Resolve a named configuration with [FromKeyedServices], or with GetRequiredKeyedService when
the name is only known at runtime:
public class PcCharacterService([FromKeyedServices("pc")] ICensusClient censusClient)
{
}
var censusClient = serviceProvider.GetRequiredKeyedService<ICensusClient>("ps4us");Named registrations are independent of the unnamed AddCensusServices(). Both can be used
together, and the unnamed services are only registered if AddCensusServices() is also called.
Named options can also be bound from configuration with
services.Configure<CensusOptions>("pc", configuration.GetSection("Census:Pc")).
To customize a named registration's HttpClient, use CensusServiceExtensions.GetCensusHttpClientName:
services.AddHttpClient(CensusServiceExtensions.GetCensusHttpClientName("pc"))
.AddStandardResilienceHandler();GetAsync, GetListAsync and GetBatchAsync (and their generic overloads) accept an optional
CancellationToken. Cancelling it aborts the in-flight request and throws an
OperationCanceledException. For GetBatchAsync the token is checked between each page request.
var characters = await query.GetListAsync<CensusCharacterModel>(cancellationToken);A request that exceeds CensusTimeout (see below) is cancelled the same way.
Census requests are sent through a typed HttpClient registered with AddHttpClient, so the
client's lifetime and handlers are managed by IHttpClientFactory. CensusTimeout (default
10 seconds) and UserAgent are applied to that client.
services.AddCensusServices(options =>
{
options.CensusTimeout = TimeSpan.FromSeconds(30);
options.UserAgent = "my-app/1.0";
});Because the client is registered through AddHttpClient, ICensusClient and ICensusQueryFactory
are transient and no longer implement IDisposable. To customize the underlying client further
(retry policies, handlers, etc.), call AddHttpClient for it again after AddCensusServices():
services.AddCensusServices();
services.AddHttpClient<ICensusClient, CensusClient>()
.AddStandardResilienceHandler();The stream client reconnects automatically if no message is received within 35 seconds.
The library logs through Microsoft.Extensions.Logging. All messages use structured templates, so
properties such as RequestUri, StatusCode and ElapsedMilliseconds are available as fields in
structured logging sinks. Logging is controlled entirely through the standard log level filters on
these categories:
| Category | Level | Event Id | Message |
|---|---|---|---|
DaybreakGames.Census.CensusClient |
Trace | 85400 | Request sent (RequestUri) |
DaybreakGames.Census.CensusClient |
Trace | 85401 | Request completed (StatusCode, ElapsedMilliseconds, RequestUri) |
DaybreakGames.Census.CensusClient |
Trace | 85402 | Request task canceled, either by a timeout or the caller's CancellationToken (ElapsedMilliseconds, RequestUri). The exception is still thrown to the caller |
DaybreakGames.Census.CensusClient |
Error | 84530 | Unknown exception while processing a query |
DaybreakGames.Census.CensusClient |
Error | 84531 | Census service unavailable |
DaybreakGames.Census.CensusClient |
Error | 84532 | Census returned a server error |
DaybreakGames.Census.CensusClient |
Error | 84533 | Connection failed or a non-success status code was returned |
DaybreakGames.Census.CensusClient |
Error | 84534 | Request timed out |
DaybreakGames.Census.Stream.CensusStreamClient |
Information | Stream connected, reconnected and subscription sent | |
DaybreakGames.Census.Stream.CensusStreamClient |
Warning | 75421 | Stream disconnected |
System.Net.Http.HttpClient.ICensusClient.LogicalHandler |
Information | Request start and end, including duration (from IHttpClientFactory) |
|
System.Net.Http.HttpClient.ICensusClient.ClientHandler |
Information | Each attempt sent over the wire, including retries (from IHttpClientFactory) |
|
System.Net.Http.HttpClient.ICensusClient.* |
Trace | Request and response headers (from IHttpClientFactory) |
Errors are always logged and the exception is still thrown to the caller. A query cancelled through
its CancellationToken is not logged as an error.
To see a trace of every Census request with timings, lower the level of the DaybreakGames.Census
namespace (or just the CensusClient category) in appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"DaybreakGames.Census": "Trace",
"System.Net.Http.HttpClient.ICensusClient": "Warning"
}
}
}To silence the library entirely, set "DaybreakGames.Census": "None".
Note: the logged RequestUri includes your service id (/s:<service-id>/).
query.Where("name.lower").Equals("lampjaw");The following operations and their equivalent syntax is below:
Equals: =NotEquals: =!IsLessThan: =<IsLessThanOrEquals: =[IsGreaterThan: =>IsGreaterThanOrEquals: =]StartsWith: =^Contains: =*
query.SetLanguage("en");
OR
query.SetLanguage(CensusLanguage.English);No language is set by default so you will receive all localized strings if available.
query.ShowFields("character_id"[, "field2", "field3", ...]);query.HideFields("currency"[, "field2", "field3", ...]);query.SetLimit(10);query.SetStart(100);query.AddResolve("world"[, "resolve2", "resolve3", ...]);var worldJoin = query.JoinService("characters_world");Join objects have the following methods:
IsList(bool)IsOuterJoin(bool)ShowFields(array): See the 'Show certain fields' section aboveHideFields(array): See the 'Hide certain fields' section aboveOnField(string)ToField(string)InjectAt(string)Where(string): See the 'Defining a condition' section aboveJoinService(string): Returns another join object for sub joining
var query = queryFactory.Create("vehicle");
var vehicleTree = query.TreeField("type_id");Tree objects have the following methods:
IsList(bool)GroupPrefix(string)StartField(string)TreeField(string): Returns another tree object for sub grouping
Uri uri = query.GetUri();This package also allows you to leverage the Census' websocket service to get data
in real time. Inject ICensusStreamClient and subcribe to events.
See the WebsocketMonitor class in the DemoApp
for a full example.