-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7e1746
commit 9bcb53e
Showing
6 changed files
with
327 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { ConfigInterfaceResolver, NotFoundException, provider } from "@/ilos/common/index.ts"; | ||
import { readFile } from "@/lib/file/index.ts"; | ||
import { logger } from "@/lib/logger/index.ts"; | ||
import { basename } from "@/lib/path/index.ts"; | ||
import { Dataset, Metadata, Resource } from "@/pdc/providers/datagouv/DataGouvAPITypes.ts"; | ||
import { DataGouvAPIConfig } from "@/pdc/services/export/config/datagouv.ts"; | ||
|
||
@provider() | ||
export class DataGouvAPIProvider { | ||
protected _dataset: Dataset | null = null; | ||
protected _resource: Resource | null = null; | ||
protected config: DataGouvAPIConfig; | ||
|
||
constructor(protected configStore: ConfigInterfaceResolver) { | ||
this.config = configStore.get("datagouv.api"); | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------------------------- | ||
// PUBLIC API | ||
// ------------------------------------------------------------------------------------------------------------------- | ||
|
||
/** | ||
* Get all dataset metadata | ||
* | ||
* Includes organisations and the list of resources | ||
*/ | ||
public async dataset(): Promise<Dataset> { | ||
if (this._dataset) { | ||
return this._dataset; | ||
} | ||
|
||
const dataset = await this.get<Dataset>(`datasets/${this.config.dataset}`); | ||
if (!dataset) { | ||
throw new NotFoundException(`Dataset not found: ${this.config.dataset}`); | ||
} | ||
|
||
this._dataset = dataset; | ||
|
||
return this._dataset; | ||
} | ||
|
||
/** | ||
* Get a specific resource from the dataset by title. | ||
* | ||
* Defaults to the latest resource if no title is provided. | ||
* | ||
* @param title | ||
* @returns | ||
*/ | ||
public async resource(title: string | null = null): Promise<Resource> { | ||
const dataset = await this.dataset(); | ||
const resource = title | ||
? dataset.resources.find((r) => r.title === title) | ||
: dataset.resources.find((r) => r.latest.includes(r.id)); | ||
|
||
if (!resource) { | ||
throw new NotFoundException(`Resource not found for dataset ${dataset.id}`); | ||
} | ||
|
||
this._resource = resource; | ||
return this._resource; | ||
} | ||
|
||
public async exists(title: string): Promise<boolean> { | ||
try { | ||
await this.resource(title); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
public async upload(filepath: string): Promise<Resource> { | ||
const title = basename(filepath); | ||
let url = `datasets/${this.config.dataset}/upload/`; | ||
|
||
if (await this.exists(title)) { | ||
logger.info(`Resource ${title} already exists, replacing...`); | ||
url = `datasets/${this.config.dataset}/resources/${this._resource!.id}/upload/`; | ||
} | ||
|
||
const form = new FormData(); | ||
const file = new File([await readFile(filepath)], title); | ||
form.append("file", file); | ||
|
||
const resource = await this.post<Resource>(url, form); | ||
if (!resource) { | ||
throw new Error(`Failed to upload resource for dataset ${this.config.dataset}`); | ||
} | ||
|
||
this._resource = resource; | ||
|
||
return resource; | ||
} | ||
|
||
public async setMetadata(resource: Resource, metadata: Metadata): Promise<Resource> { | ||
const r = await this.put<Resource>( | ||
`datasets/${this.config.dataset}/resources/${resource.id}`, | ||
JSON.stringify({ | ||
title: resource.title, | ||
...metadata, | ||
}), | ||
); | ||
|
||
if (!r) { | ||
throw new Error(`Failed to update resource ${resource.id} metadata`); | ||
} | ||
|
||
this._resource = r; | ||
|
||
return this._resource; | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------------------------- | ||
// PRIVATE REQUEST HELPERS | ||
// ------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected async get<T>(url: string): Promise<T> { | ||
return this._query<T>(url); | ||
} | ||
|
||
protected async post<T>(url: string, body: BodyInit): Promise<T> { | ||
return this._query<T>(url, "POST", body); | ||
} | ||
|
||
protected async put<T>(url: string, body: BodyInit): Promise<T> { | ||
return this._query<T>(url, "PUT", body); | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------------------------- | ||
// INTERNALS | ||
// ------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected async _query<T>( | ||
url: string, | ||
method: "GET" | "POST" | "PUT" = "GET", | ||
body: BodyInit | null = null, | ||
): Promise<T> { | ||
const baseURL = this.config.url; | ||
const headers: HeadersInit = { | ||
"Accept": "application/json", | ||
"Content-Type": "application/json", | ||
"X-API-KEY": this.config.key, | ||
}; | ||
|
||
const init: RequestInit = { method, headers }; | ||
if (body) { | ||
init.body = body; | ||
if (body instanceof FormData) delete headers["Content-Type"]; | ||
} | ||
|
||
const response = await fetch(`${baseURL}/${url}`, init); | ||
if (!response.ok) { | ||
throw Error(response.statusText); | ||
} | ||
|
||
return response.json() as T; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
export type Organisation = { | ||
acronym: string; | ||
badges: { kind: string }[]; | ||
class: string; | ||
id: string; | ||
logo: string; | ||
logo_thumbnail: string; | ||
name: string; | ||
page: string; | ||
slug: string; | ||
uri: string; | ||
}; | ||
|
||
export type Dataset = { | ||
acronym: string; | ||
archived: null | string; | ||
badges: { kind: string }[]; | ||
contact_point: null | string; | ||
created_at: string; | ||
deleted: null | string; | ||
description: string; | ||
extras: { | ||
"recommendations-externals": { | ||
id: string; | ||
messages: { en: object; fr: object }; | ||
score: number; | ||
source: string; | ||
}[]; | ||
"recommendations:sources": string[]; | ||
}; | ||
frequency: string; | ||
frequency_date: string; | ||
harvest: null | string; | ||
id: string; | ||
internal: { | ||
created_at_internal: string; | ||
last_modified_internal: string; | ||
}; | ||
last_modified: string; | ||
last_update: string; | ||
license: string; | ||
metrics: { | ||
discussions: number; | ||
followers: number; | ||
resources_downloads: number; | ||
reuses: number; | ||
views: number; | ||
}; | ||
organization: Organisation; | ||
owner: null | string; | ||
page: string; | ||
private: boolean; | ||
quality: { | ||
all_resources_available: boolean; | ||
dataset_description_quality: boolean; | ||
has_open_format: boolean; | ||
has_resources: boolean; | ||
license: boolean; | ||
resources_documentation: boolean; | ||
score: number; | ||
spatial: boolean; | ||
temporal_coverage: boolean; | ||
update_frequency: boolean; | ||
update_fulfilled_in_time: boolean; | ||
}; | ||
resources: Resource[]; | ||
schema: null | string; | ||
slug: string; | ||
spatial: { | ||
geom: null | string; | ||
granularity: string; | ||
zones: string[]; | ||
}; | ||
tags: string[]; | ||
temporal_coverage: { end: string; start: string }; | ||
title: string; | ||
uri: string; | ||
}; | ||
|
||
export type Resource = { | ||
checksum: { | ||
type: "sha1"; | ||
value: string; | ||
}; | ||
created_at: string; | ||
description: null | string; | ||
extras: { | ||
"check:available": boolean; | ||
"check:date": string; | ||
"check:headers:content-type": string; | ||
"check:status": number; | ||
"check:timeout": boolean; | ||
}; | ||
filesize: number; | ||
filetype: string; | ||
format: string; | ||
harvest: null | string; | ||
id: string; | ||
internal: { | ||
created_at_internal: string; | ||
last_modified_internal: string; | ||
}; | ||
last_modified: string; | ||
latest: string; | ||
metrics: Record<string, unknown>; | ||
mime: string; | ||
preview_url: null | string; | ||
schema: { name: null | string; url: null | string; version: null | string }; | ||
title: string; | ||
type: string; | ||
url: string; | ||
}; | ||
|
||
export type Metadata = { | ||
description: string; | ||
}; |
16 changes: 16 additions & 0 deletions
16
api/src/pdc/providers/datagouv/DataGouvMetadataProvider.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { provider } from "@/ilos/common/Decorators.ts"; | ||
import { ConfigInterfaceResolver } from "@/ilos/common/index.ts"; | ||
import { DataGouvAPIConfig } from "@/pdc/services/export/config/datagouv.ts"; | ||
|
||
@provider() | ||
export class DataGouvMetadataProvider { | ||
protected config: DataGouvAPIConfig; | ||
|
||
constructor(configStore: ConfigInterfaceResolver) { | ||
this.config = configStore.get("datagouv.api"); | ||
} | ||
|
||
description(): string { | ||
return new Date().toISOString(); | ||
} | ||
} |
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
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
Oops, something went wrong.