Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues 56, 57, 58, 59 #60

Merged
merged 4 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/census.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class CensusClient extends EventEmitter<ClientEvents> {

this.serviceId = serviceId;

this.rest = new RestClient({serviceId, ...rest});
this.rest = new RestClient(environment, {serviceId, ...rest});

this.characterManager = new CharacterManager(this, characterManager,);

Expand Down
8 changes: 4 additions & 4 deletions src/client/concerns/cache.contract.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export interface CacheContract {
put(key: string, data: any): Promise<void>;
put(key: string, data: any): Promise<void> | void;

fetch(key: string): Promise<any | null>;
fetch(key: string): Promise<any | null> | any | null;

forget(key: string): Promise<void>;
forget(key: string): Promise<void> | void;

forgetAll(): Promise<void>;
forgetAll(): Promise<void> | void;
}
12 changes: 10 additions & 2 deletions src/client/managers/character.manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CacheContract} from '../concerns/cache.contract';
import {CacheContract} from '../concerns/cache.contract';
import {GetQuery} from '../../rest';
import {CensusClient} from '../census.client';
import {MaxRetryException} from '../exceptions/max-retry.exception';
Expand Down Expand Up @@ -57,6 +57,14 @@ export class CharacterManager {
return data;
}

async forget(id: string): Promise<void> {
await this.cache.forget(id);
}

async forgetAll(): Promise<void> {
await this.cache.forgetAll();
}

private async request(id: string): Promise<any> {
const conditions = {character_id: id};
let retries = this.maxRetries;
Expand All @@ -66,7 +74,7 @@ export class CharacterManager {

do {
try {
const data = await this.query.get(this.client.environment, conditions);
const data = await this.query.get(conditions);

if (data.length <= 0)
throw new Error(`Census did not return a character for "${id}"`);
Expand Down
7 changes: 2 additions & 5 deletions src/rest/concern/rest.contract.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import {CollectionNames, Conditions, Params} from '../types/collection';
import {PS2Environment} from '../../types/ps2.options';
import {CensusGetResponse} from '../types/census';

export interface RestContract {
/**
* Run a count query
*
* @param environment
* @param collection
* @param query
*/
count<C extends CollectionNames>(environment: PS2Environment, collection: C, query: Conditions<C>): Promise<number>;
count<C extends CollectionNames>(collection: C, query: Conditions<C>): Promise<number>;

/**
* Execute a get query directly
*
* @param environment
* @param collection
* @param params
*/
get<C extends CollectionNames, R>(environment: PS2Environment, collection: C, params: Params<C>): Promise<CensusGetResponse<C, R>>;
get<C extends CollectionNames, R>(collection: C, params: Params<C>): Promise<CensusGetResponse<C, R>>;
}
6 changes: 2 additions & 4 deletions src/rest/get.query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {CollectionNames, Conditions, Format, PartialPaths, Paths} from './types/collection';
import {CensusResponse} from './utils/census-response';
import {PS2Environment} from '../types/ps2.options';
import {CommandParamKeys, CommandParams, Join, Langs, Resolve, Sort, Tree} from './types/commands';
import {CensusBoolean, CensusGetResponse} from './types/census';
import {RestContract} from './concern/rest.contract';
Expand Down Expand Up @@ -153,13 +152,12 @@ export class GetQuery<C extends CollectionNames, R = Format<C>> {
/**
* Execute the query
*
* @param environment the environment to query
* @param query conditions to filter results
*/
async get(environment: PS2Environment, query: Conditions<C>): Promise<CensusResponse<R>> {
async get(query: Conditions<C> = {}): Promise<CensusResponse<R>> {
return new CensusResponse(
this.collection,
await this.census.get<C, CensusGetResponse<C, R>>(environment, this.collection, {...this.params, ...query}),
await this.census.get<C, CensusGetResponse<C, R>>(this.collection, {...this.params, ...query}),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export * from './utils/census-response';
export * from './collections';

/** Utility */
export * as commandTypes from './types/commands';
export * from './types';

/** Exceptions */
export * from './exceptions/census-rest.exception';
Expand Down
13 changes: 5 additions & 8 deletions src/rest/rest.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ export class RestClient implements RestContract {
static readonly CENSUS_HTTP = 'http://census.daybreakgames.com';
static readonly CENSUS_HTTPS = 'https://census.daybreakgames.com';


private api: AxiosInstance;

constructor({serviceId, https = true}: RestClientOptions = {}) {
constructor(readonly environment: PS2Environment,{serviceId, https = true}: RestClientOptions = {}) {
let baseUrl = https ? RestClient.CENSUS_HTTPS : RestClient.CENSUS_HTTP;

if (serviceId) baseUrl += `/s:${serviceId}`;
Expand Down Expand Up @@ -57,13 +56,12 @@ export class RestClient implements RestContract {
/**
* Run a count query
*
* @param environment
* @param collection
* @param query
*/
async count<C extends CollectionNames>(environment: PS2Environment, collection: C, query: Conditions<C>): Promise<number> {
async count<C extends CollectionNames>(collection: C, query: Conditions<C>): Promise<number> {
const {data} = await this.api.get(
`/count/${environment}:v2/${collection}`,
`/count/${this.environment}:v2/${collection}`,
{params: query},
);

Expand All @@ -73,13 +71,12 @@ export class RestClient implements RestContract {
/**
* Execute a get query directly
*
* @param environment
* @param collection
* @param params
*/
async get<C extends CollectionNames, R>(environment: PS2Environment, collection: C, params: Params<C>): Promise<CensusGetResponse<C, R>> {
async get<C extends CollectionNames, R>(collection: C, params: Params<C>): Promise<CensusGetResponse<C, R>> {
const {data} = await this.api.get(
`/get/${environment}:v2/${collection}`,
`/get/${this.environment}:v2/${collection}`,
{params},
);

Expand Down
1 change: 1 addition & 0 deletions src/rest/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as collection from './collection';