Skip to content

Commit

Permalink
🎉 data/obs routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyfritz committed Mar 31, 2019
1 parent daec19b commit c759ef1
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 8 deletions.
19 changes: 19 additions & 0 deletions src/api/IHistoricObservationsOnADate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Category } from "../constants/Category";
import { Sort } from "../constants/Sort";
import { IObservation } from "../interfaces/IObservation";

export interface IHistoricObservationsOnADateRequest {
regionCode: string;
y: number;
m: number;
d: number;
rank?: Range;
cat?: Category[];
maxResults?: number
detail?: Detail;
includeProvisional?: boolean;
hotspot?: boolean;
r?: string;
}

export type IHistoricObservationsOnADateResponse = IObservation[];
16 changes: 16 additions & 0 deletions src/api/INearestObservationsOfASpecies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Category } from "../constants/Category";
import { Sort } from "../constants/Sort";
import { IObservation } from "../interfaces/IObservation";

export interface INearestObservationsOfASpeciesRequest {
speciesCode: string;
lat: number;
lng: number;
dist?: number;
back?: number;
maxResults?: number;
includeProvisional?: boolean;
hotspot?: boolean;
}

export type INearestOBservationOfASpeciesResponse = IObservation[];
18 changes: 18 additions & 0 deletions src/api/IRecentNearbyObservationsOfASpecies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Category } from "../constants/Category";
import { Sort } from "../constants/Sort";
import { IObservation } from "../interfaces/IObservation";

export interface IRecentNearbyObservationsOfASpeciesRequest {
speciesCode: string;
lat: number;
lng: number;
dist?: number;
back?: number;
cat?: Category[];
maxResults?: number;
includeProvisional?: boolean;
hotspot?: boolean;
sppLocale?: string;
}

export type IRecentNearbyObservationsOfASpeciesResponse = IObservation[];
13 changes: 13 additions & 0 deletions src/api/IRecentNotableObservationsInARegion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Detail } from "../constants/Detail";
import { IObservation } from "../interfaces/IObservation";

export interface IRecentNotableObservationsInARegionRequest {
regionCode: string;
back?: number;
maxResults?: number;
detail?: Detail;
hotspot?: boolean;
r?: string;
}

export type IRecentNotableObservationsInARegionResponse = IObservation[];
14 changes: 14 additions & 0 deletions src/api/IRecentObservationsInARegion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Category } from "../constants/Category";
import { IObservation } from "../interfaces/IObservation";

export interface IRecentObservationsInARegionRequest {
regionCode: string;
back?: number;
cat?: Category[];
maxResults?: number;
includeProvisional?: boolean;
hotspot?: boolean;
r?: string;
}

export type IRecentObservationsInARegionResponse = IObservation[];
14 changes: 14 additions & 0 deletions src/api/IRecentObservationsOfASpeciesInARegion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Category } from "../constants/Category";
import { IObservation } from "../interfaces/IObservation";

export interface IRecentObservationsOfASpeciesInARegionRequest {
regionCode: string;
speciesCode: string;
back?: number;
maxResults?: number;
includeProvisional?: boolean;
hotspot?: boolean;
r?: string;
}

export type IRecentObservationsOfASpeciesInARegionResponse = IObservation[];
4 changes: 4 additions & 0 deletions src/constants/Detail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Detail {
SIMPLE = "simple",
FULL = "full",
}
46 changes: 38 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import axios, { AxiosInstance } from "axios";
import * as _ from "lodash";
import _ from "lodash";
import { IHistoricObservationsOnADateRequest, IHistoricObservationsOnADateResponse } from "./api/IHistoricObservationsOnADate";
import { INearestOBservationOfASpeciesResponse, INearestObservationsOfASpeciesRequest } from "./api/INearestObservationsOfASpecies";
import { IRecentNearbyNotableObservationsRequest, IRecentNearbyNotableObservationsResponse } from "./api/IRecentNearbyNotableObservations";
import { IRecentNearbyObservationsRequest, IRecentNearbyObservationsResponse } from "./api/IRecentNearbyObservations";
import { IRecentNearbyObservationsOfASpeciesRequest, IRecentNearbyObservationsOfASpeciesResponse } from "./api/IRecentNearbyObservationsOfASpecies";
import { IRecentNotableObservationsInARegionRequest, IRecentNotableObservationsInARegionResponse } from "./api/IRecentNotableObservationsInARegion";
import { IRecentObservationsInARegionRequest, IRecentObservationsInARegionResponse } from "./api/IRecentObservationsInARegion";
import { IRecentObservationsOfASpeciesInARegionRequest, IRecentObservationsOfASpeciesInARegionResponse } from "./api/IRecentObservationsOfASpeciesInARegion";
import { ITaxonomyRequest, ITaxonomyResponse } from "./api/ITaxonomy";

export default class EbirdClient {
Expand All @@ -14,28 +20,52 @@ export default class EbirdClient {
});
}

public async historicObservationsOnADate(request: IHistoricObservationsOnADateRequest): Promise<void | IHistoricObservationsOnADateResponse> {
return this.get(`/data/obs/${request.regionCode}/historic/${request.y}/${request.m}/${request.d}`, ["back", "cat", "maxResults", "includeProvisional", "hotspot", "r"], request);
}

public async nearestObservationsOfASpecies(request: INearestObservationsOfASpeciesRequest): Promise<void | INearestOBservationOfASpeciesResponse> {
return this.get(`/data/nearest/geo/recent/${request.speciesCode}`, ["back", "cat", "maxResults", "includeProvisional", "hotspot", "r"], request);
}

public async recentObservationsInARegion(request: IRecentObservationsInARegionRequest): Promise<void | IRecentObservationsInARegionResponse> {
return this.get(`/data/obs/${request.regionCode}/recent`, ["back", "cat", "maxResults", "includeProvisional", "hotspot", "r"], request);
}

public async recentObservationsOfASpeciesInARegion(request: IRecentObservationsOfASpeciesInARegionRequest): Promise<void | IRecentObservationsOfASpeciesInARegionResponse> {
return this.get(`/data/obs/${request.regionCode}/recent/${request.speciesCode}`, ["back", "cat", "maxResults", "includeProvisional", "hotspot", "r"], request);
}

public async recentNearbyNotableObservations(request: IRecentNearbyNotableObservationsRequest): Promise<void | IRecentNearbyNotableObservationsResponse> {
return this.get(`/ref/obs/geo/recent/notable`, request);
return this.get(`/ref/obs/geo/recent/notable`, Object.keys(request), request);
}

public async recentNearbyObservations(request: IRecentNearbyObservationsRequest): Promise<void | IRecentNearbyObservationsResponse> {
return this.get(`/ref/obs/geo/recent`, request);
return this.get(`/ref/obs/geo/recent`, Object.keys(request), request);
}

public async recentNearbyObservationsOfASpecies(request: IRecentNearbyObservationsOfASpeciesRequest): Promise<void | IRecentNearbyObservationsOfASpeciesResponse> {
return this.get(`/ref/obs/geo/recent/${request.speciesCode}`, Object.keys(request), request);
}

public async recentNotableObservationsInARegion(request: IRecentNotableObservationsInARegionRequest): Promise<void | IRecentNotableObservationsInARegionResponse> {
return this.get(`/data/obs/${request.regionCode}/recent/notable`, ["back", "cat", "maxResults", "includeProvisional", "hotspot", "r"], request);
}

public async taxonomy(request: ITaxonomyRequest): Promise<void | ITaxonomyResponse> {
return this.get(`/ref/taxonmy/ebird`, request);
return this.get(`/ref/taxonomy/ebird`, Object.keys(request), request);
}

private async get<Req, Res>(url: string, request: Req): Promise<void | Res> {
const queryString = this.generateQueryString(request);
private async get<Req, Res>(url: string, queryStringKeys: string[], request: Req): Promise<void | Res> {
const queryString = this.generateQueryString(queryStringKeys, request);
const response = await this.ebirdHttp
.get<Res>(`${url}?${queryString}`);
return response.data;
}

private generateQueryString(value: any): string {
private generateQueryString(keys: string[], value: any): string {
const components = [];
for (const key in Object.keys(value)) {
for (const key in keys) {
if (value.hasOwnProperty(key)) {
const field = value[key];
if (field === undefined) {
Expand Down

0 comments on commit c759ef1

Please sign in to comment.