Skip to content

Commit

Permalink
fix GCS init
Browse files Browse the repository at this point in the history
  • Loading branch information
AdenForshaw committed Nov 1, 2024
1 parent 1d71142 commit d9baf07
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "overture-maps-api",
"version": "0.0.5",
"version": "0.0.6",
"description": "",
"author": "",
"private": true,
Expand Down
3 changes: 2 additions & 1 deletion src/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe('AppController', () => {

describe('root', () => {
it('should return hello', () => {
expect(appController.getHello()).toBe('API by ThatAPICompany.com, Data by OvertureMaps.org');
const result = appController.getHello();
expect(result.message).toBe('API by ThatAPICompany.com, Data by OvertureMaps.org');
});
});
});
4 changes: 2 additions & 2 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello();
getHello(): {message:string, version:string, service:string} {
return {"service":this.appService.getAppName(),"version":this.appService.getVersion(), message:"API by ThatAPICompany.com, Data by OvertureMaps.org"};
}
}
8 changes: 6 additions & 2 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
getHello(): string {
return 'API by ThatAPICompany.com, Data by OvertureMaps.org';

getVersion(): string {
return process.env.npm_package_version;
}
getAppName(): string {
return process.env.npm_package_name;
}
}
2 changes: 1 addition & 1 deletion src/gcs/gcs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class GcsService {

constructor() {

if(!process.env.BIGQUERY_PROJECT_ID || process.env.GOOGLE_APPLICATION_CREDENTIALS || process.env.GCS_BUCKET_NAME){
if(!process.env.BIGQUERY_PROJECT_ID || !process.env.GOOGLE_APPLICATION_CREDENTIALS || !process.env.GCS_BUCKET_NAME){
this.logger.error('GCS environment variables not set');
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/middleware/auth-api.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export class AuthAPIMiddleware implements NestMiddleware {
} catch (error) {
Logger.error('APIKeyMiddleware Error:', error, ` key: ${apiKeyString}`);
}

//if we got this far and they passed a key we should tell the user their key doesn't work and to check for a spelling mistake
res.status(401).send('Unauthorized - check the spelling of your API Key');

next()
return;
}
Expand Down
64 changes: 37 additions & 27 deletions src/places/places.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,32 @@ export class PlacesController {
const cacheKey = `get-places-${JSON.stringify(query)}`;

// Check if cached results exist in GCS
const cachedResult = await this.gcsService.getJSON(cacheKey);
if (cachedResult) {
this.logger.log(`Cache hit for ${cacheKey} - cachedResult length: ${cachedResult.length}`);

if(query.format === 'geojson') {
return toPlacesGeoJSONResponseDto(cachedResult);
}
return cachedResult.map((place: any) => new PlaceResponseDto(place));
let results = await this.getFromCache(cacheKey);
if (!results) {
// if only country is provided, then potentially just use the lat / lng of it's capital city

// If no cache, query BigQuery with wikidata and country support
results = await this.bigQueryService.getPlacesNearby(lat, lng, radius, brand_wikidata,brand_name, country, categories, min_confidence,limit);

// Cache the results in GCS
await this.gcsService.storeJSON (results,cacheKey);
}

// if only country is provided, then potentially just use the lat / lng of it's capital city

// If no cache, query BigQuery with wikidata and country support
const places = await this.bigQueryService.getPlacesNearby(lat, lng, radius, brand_wikidata,brand_name, country, categories, min_confidence,limit);

// Cache the results in GCS
await this.gcsService.storeJSON (places,cacheKey);

if(query.format === 'geojson') {
return toPlacesGeoJSONResponseDto(places);
return toPlacesGeoJSONResponseDto(results);
}
return places.map((place: any) => new PlaceResponseDto(place));
return results.map((place: any) => new PlaceResponseDto(place));
}

@Get('brands')
async getBrands(@Query() query: GetBrandsDto) {
const { country, lat, lng, radius, categories } = query;

const cacheKey = `get-places-brands-${JSON.stringify(query)}`;

// Check if cached results exist in GCS
const cachedResult = await this.gcsService.getJSON(cacheKey);
const cacheKey = `get-places-brands-${JSON.stringify(query)}`;
const cachedResult = await this.getFromCache(cacheKey);
if (cachedResult) {
this.logger.log(`Cache hit for ${cacheKey} - cachedResult length: ${cachedResult.length}`);
return cachedResult;
}

Expand All @@ -71,9 +63,8 @@ export class PlacesController {
@Get('countries')
async getCountries() {
const cacheKey = `get-places-countries`;
const cachedResult = await this.gcsService.getJSON(cacheKey);
const cachedResult = await this.getFromCache(cacheKey);
if (cachedResult) {
this.logger.log(`Cache hit for ${cacheKey} - cachedResult length: ${cachedResult.length}`);
return cachedResult;
}

Expand All @@ -85,16 +76,35 @@ export class PlacesController {
@Get('categories')
async getCategories(@Query() query: GetCategoriesDto) {

const cacheKey = `get-places-categories-${JSON.stringify(query)}`;
const cachedResult = await this.gcsService.getJSON(cacheKey);
const cacheKey = `get-places-categories-${JSON.stringify(query)}`;
const cachedResult = await this.getFromCache(cacheKey );
if (cachedResult) {
this.logger.log(`Cache hit for ${cacheKey} - cachedResult length: ${cachedResult.length}`);
return cachedResult;
}

const results = await this.bigQueryService.getCategories(query.country);

await this.gcsService.storeJSON (results,cacheKey);
await this.storeToCache (results, cacheKey);
return results;
}

async getFromCache(cacheKey:string): Promise<any[]|null> {
try{;
const cachedResult = await this.gcsService.getJSON(cacheKey);
this.logger.log(`Cache hit for get-places-categories-${JSON.stringify(cacheKey)} - cachedResult length: ${cachedResult.length}`);
return cachedResult;
}catch(error){
this.logger.error('Error fetching cached places:', error);
return null;
}
}

async storeToCache( data,cacheKey:string): Promise<void> {
try{

await this.gcsService.storeJSON (data,cacheKey);
}catch(error){
this.logger.error('Error saving cached places:', error);
}
}
}
11 changes: 8 additions & 3 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import e from 'express';

describe('AppController (e2e)', () => {
let app: INestApplication;
Expand All @@ -15,10 +16,14 @@ describe('AppController (e2e)', () => {
await app.init();
});

it('/ (GET)', () => {
return request(app.getHttpServer())
it('/ (GET)', async() => {
const response = await request(app.getHttpServer())
.get('/')
.expect(200)
.expect('API by ThatAPICompany.com, Data by OvertureMaps.org');
expect(response.body).toBeDefined();
expect(response.body).toHaveProperty('service');
expect(response.body).toHaveProperty('version');
expect(response.body).toHaveProperty('message');
expect(response.body.message).toBe('API by ThatAPICompany.com, Data by OvertureMaps.org');
});
});

0 comments on commit d9baf07

Please sign in to comment.