Skip to content

Commit

Permalink
feat!: add authentication
Browse files Browse the repository at this point in the history
- Add authentication support
- Rename types
- Rename methods
  • Loading branch information
Foohx committed Jun 15, 2023
1 parent 6007888 commit 44c5500
Show file tree
Hide file tree
Showing 17 changed files with 269 additions and 242 deletions.
44 changes: 25 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,29 @@ En utilisant yarn:
Node.js :

```javascript
const { getErps } = require("acceslibre-client");
const { getEstablishments } = require("acceslibre-client");

const API_KEY = "YOUR_KEY"

(async () => {
const erps = await getErps({ page: 1 });
const erps = await getEstablishments(API_KEY, { page: 1 });
)
```
React (Js) :
```js
import { getErps } from "acceslibre-client";
import { getEstablishments } from "acceslibre-client";
import { useEffect, useState } from "react";

const API_KEY = "YOUR_KEY";

function App() {
const [erps, setErps] = useState(undefined);

useEffect(() => {
(async () => {
const erps = await getErps();
const erps = await getEstablishments(API_KEY);
setErps(erps);
})();
}, []);
Expand Down Expand Up @@ -78,25 +82,25 @@ export default App;
Obtenir une liste d'activités d'ERP :
```ts
const activities = await getActivites();
const activities = await getActivities(API_KEY);
```
Obtenir une activité particulière par le biais de son slug :
```ts
const activity = await readActivite("accessoires");
const activity = await readActivity(API_KEY, "accessoires");
```
Demander une page spécifique d'une liste d'activités :
```ts
const activities = await getActivites({ page: 2 });
const activities = await getActivities(API_KEY, { page: 2 });
```
Récupérer la activités de l'ensemble des ERP de la ville de Bordeaux :
```ts
const activities = await getActivities({ commune: "Bordeaux" });
const activities = await getActivities(API_KEY, { commune: "Bordeaux" });
```
### ERPs :
Expand All @@ -106,31 +110,31 @@ const activities = await getActivities({ commune: "Bordeaux" });
Obtenir une liste d'ERP :
```ts
const erps = await getErps();
const erps = await getEstablishments(API_KEY);
```
Obtenir un ERP en particulier par le biais de son slug :
```ts
const erp = await readErp("acdp-formation-82");
const erp = await readEstablishment(API_KEY, "acdp-formation-82");
```
Demander une page spécifique d'une liste d'ERP :
```ts
const erps = await getErps({ page: 2 });
const erps = await getEstablishments(API_KEY, { page: 2 });
```
Rechercher les ERPs contenant le terme "impôts" dans leur nom ou activité :
```ts
const erps = await getErps({ q: "impôts" });
const erps = await getEstablishments(API_KEY, { q: "impôts" });
```
Récupérer les ERPs de la ville de Bordeaux :
```ts
const erps = await getErps({ commune: "Bordeaux" });
const erps = await getEstablishments(API_KEY, { commune: "Bordeaux" });
```
La liste complète des paramètres de `getErps()` est disponible dans la [Documentation de l'API](https://acceslibre.beta.gouv.fr/api/docs/).
Expand All @@ -142,19 +146,19 @@ La liste complète des paramètres de `getErps()` est disponible dans la [Docume
Obtenir une liste des critères d'accessibilité de plusieurs ERPs :
```ts
const erpsRequirements = await getAccessibilites();
const erpsRequirements = await getAccessibilites(API_KEY);
```
Obtenir les critères d'accessibilité d'un ERP spécifique en utilisant le slug d'un critère :
```ts
const requirements = await readAccessibilite("1234");
const requirements = await readAccessibilite(API_KEY, "1234");
```
Demander une page spécifique d'une liste des critères d'accessibilité d'ERPs :
```ts
const erpsRequirements = await getAccessibilites({ page: 1 });
const erpsRequirements = await getAccessibilites(API_KEY, { page: 1 });
```
### Utilitaires :
Expand All @@ -164,10 +168,12 @@ Les utilitaires sont disponibles dans `acceslibre-client/dist/utils`.
La méthode `readAccessibilite()` a besoin d'un `id` que vous pouvez récupérer dans l'url disponible via la propriété `accessibilite` du type `IErp`.
```ts
import { IErp, readErp, readAccessibilite } from "acceslibre-client";
import { extractAccessibiliteSlugFromApiUrl } from "acceslibre-client/dist/utils";
import { IEstablishment readEstablishment, readAccessibility } from "acceslibre-client";
import { extractAccessibilitiesSlugFromApiUrl } from "acceslibre-client/dist/utils";

const API_KEY = "YOUR_KEY"

const erp: IErp | null = await readErp("erp-slug");
const erp: IEstablishment | null = await readEstablishment("erp-slug");
if (erp !== null) {
const requirementsId = extractAccessibiliteSlugFromApiUrl(erp.accessibilite);
if (requirementsId !== null) {
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./types";
export * from "./methods";
export * from "./utils";
29 changes: 15 additions & 14 deletions methods/accessibilite.ts → methods/accessibility.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
import axios from "axios";
import { IAccessibilite, IAccessibilitePaginate } from "../types";
import { API_URL } from "../utils";
import { IAccessibility, IPaginateAccessibilities } from "../types";

interface IAccessibiliteOptions {
interface IAccessibilityOptions {
/** A page number within the paginated result set. */
page?: number;
/** Écarter les valeurs nulles ou non-renseignées */
// clean?: boolean
/** Formater les données d'accessibilité pour une lecture humaine */
// readable?: boolean;
/** Number of results to return per page. */
page_size?: number;
}

/** Permer de récupérer une liste d'ERP. */
export async function getAccessibilites(query: IAccessibiliteOptions = {}) {
/** Lists the accessibility criteria for establishments. */
export async function getAccessibilites(
key: string,
query: IAccessibilityOptions = {}
) {
try {
const r = await axios.get<IAccessibilitePaginate>(
const r = await axios.get<IPaginateAccessibilities>(
`${API_URL}/accessibilite/`,
{
headers: {
"Content-Type": "application/json",
Authorization: `Api-Key ${key}`,
},
params: query,
}
);
return r.data;
} catch (error) {
return null;
}
} catch (error) {}
}

/* Permet de récupérer les données d'un ERP spécifique, par le biais de son id */
export async function readAccessibilite(id: string) {
export async function readAccessibility(key: string, id: string) {
try {
const r = await axios.get<IAccessibilite>(
const r = await axios.get<IAccessibility>(
`${API_URL}/accessibilite/${id}/`,
{
headers: {
"Content-Type": "application/json",
Authorization: `Api-Key ${key}`,
},
}
);
Expand Down
39 changes: 0 additions & 39 deletions methods/activite.ts

This file was deleted.

41 changes: 41 additions & 0 deletions methods/activity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import axios from "axios";
import { API_URL } from "../utils";
import { IActivity, IEstablishment, IPaginateActivities } from "../types";

interface IActiviteOptions {
/** A page number within the paginated result set. */
page?: number;
/** Name of the town (ex. Bordeaux) */
commune?: string;
}

/** Lists the activities of establishments. */
export async function getActivities(key: string, query: IActiviteOptions = {}) {
try {
const r = await axios.get<IPaginateActivities>(`${API_URL}/activites/`, {
headers: {
"Content-Type": "application/json",
Authorization: `Api-Key ${key}`,
},
params: query,
});
return r.data;
} catch (error) {
return null;
}
}

/** List the activities of an establishments. */
export async function readActivity(key: string, slug: IActivity["slug"]) {
try {
const r = await axios.get<IActivity>(`${API_URL}/activites/${slug}/`, {
headers: {
"Content-Type": "application/json",
Authorization: `Api-Key ${key}`,
},
});
return r.data;
} catch (error) {
return null;
}
}
19 changes: 12 additions & 7 deletions methods/erp.ts → methods/establishment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import axios from "axios";
import { IErp, IErpPaginate } from "../types";
import { API_URL } from "../utils";
import { IEstablishment, IPaginateEstablishments } from "../types";

interface IErpsOptions {
/** A page number within the paginated result set. */
page?: number;
/** Number of results to return per page. */
page_size?: number;
/** Termes de recherche */
q?: string;
/** Nom de la commune (ex. Clichy) */
Expand All @@ -21,18 +23,20 @@ interface IErpsOptions {
source?: string;
/** ID unique fourni par un fournisseur tier */
source_id?: string;
asp_id?: string;
/** Identifiant unique OpenData */
uuid?: string;
/** Biais de localisation géographique, au format `latitude,longitude` (par ex. `43.22,3.83`) */
around?: string;
}

/** Permer de récupérer une liste d'ERP. */
export async function getErps(query: IErpsOptions = {}) {
/** List of establishments. */
export async function getEstablishments(key: string, query: IErpsOptions = {}) {
try {
const r = await axios.get<IErpPaginate>(`${API_URL}/erps/`, {
const r = await axios.get<IPaginateEstablishments>(`${API_URL}/erps/`, {
headers: {
"Content-Type": "application/json",
Authorization: `Api-Key ${key}`,
},
params: query,
});
Expand All @@ -42,12 +46,13 @@ export async function getErps(query: IErpsOptions = {}) {
}
}

/* Permet de récupérer les données d'un ERP spécifique, par le biais de son slug */
export async function readErp(slug: string) {
/* Retrieve data from a specific establishment. */
export async function readEstablishment(key: string, slug: string) {
try {
const r = await axios.get<IErp>(`${API_URL}/erps/${slug}/`, {
const r = await axios.get<IEstablishment>(`${API_URL}/erps/${slug}/`, {
headers: {
"Content-Type": "application/json",
Authorization: `Api-Key ${key}`,
},
});
return r.data;
Expand Down
6 changes: 3 additions & 3 deletions methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./accessibilite";
export * from "./activite";
export * from "./erp";
export * from "./accessibility";
export * from "./activity";
export * from "./establishment";
Loading

0 comments on commit 44c5500

Please sign in to comment.