From e0b51c7f949eb82957b3ec94a0fadda6ea2c7f73 Mon Sep 17 00:00:00 2001
From: Emmanuel Raviart <emmanuel@raviart.com>
Date: Wed, 24 Mar 2021 09:35:09 +0100
Subject: [PATCH] Add sample calls of OpenFisca web API.

---
 src/routes/calculations/index.svelte | 71 ++++++++++++++++++++++++++++
 src/routes/entities/index.svelte     | 25 ++++++++++
 src/routes/index.svelte              | 19 +++++---
 src/routes/parameters/index.svelte   | 25 ++++++++++
 src/routes/spec.svelte               | 25 ++++++++++
 src/routes/variables/index.svelte    | 25 ++++++++++
 6 files changed, 184 insertions(+), 6 deletions(-)
 create mode 100644 src/routes/calculations/index.svelte
 create mode 100644 src/routes/entities/index.svelte
 create mode 100644 src/routes/parameters/index.svelte
 create mode 100644 src/routes/spec.svelte
 create mode 100644 src/routes/variables/index.svelte

diff --git a/src/routes/calculations/index.svelte b/src/routes/calculations/index.svelte
new file mode 100644
index 000000000..e83992d8c
--- /dev/null
+++ b/src/routes/calculations/index.svelte
@@ -0,0 +1,71 @@
+<script context="module" lang="ts">
+  export async function load({ page, fetch, session, context }) {
+    const url = "https://fr.openfisca.org/api/latest/calculate";
+    const situation = {
+      individus: {
+        Claude: {
+          salaire_de_base: {
+            "2017": 20000,
+          },
+        },
+        Dominique: {
+          salaire_de_base: {
+            "2017": 30000,
+          },
+        },
+        Camille: {},
+      },
+      menages: {
+        menage_1: {
+          personne_de_reference: ["Claude"],
+          conjoint: ["Dominique"],
+          enfants: ["Camille"],
+          revenu_disponible: {
+            "2017": null,
+          },
+          impots_directs: {
+            "2017": null,
+          },
+        },
+      },
+      familles: {
+        famille_1: {
+          parents: ["Claude", "Dominique"],
+          enfants: ["Camille"],
+        },
+      },
+      foyers_fiscaux: {
+        foyer_fiscal_1: {
+          declarants: ["Claude", "Dominique"],
+          personnes_a_charge: ["Camille"],
+        },
+      },
+    };
+    const res = await fetch(url, {
+      body: JSON.stringify(situation, null, 2),
+      headers: {
+        "Content-Type": "application/json",
+      },
+      method: "POST",
+    });
+
+    if (res.ok) {
+      return {
+        props: {
+          simulation: await res.json(),
+        },
+      };
+    }
+
+    return {
+      status: res.status,
+      error: new Error(`Could not load ${url}`),
+    };
+  }
+</script>
+
+<script lang="ts">
+  export let simulation: any;
+</script>
+
+<pre>{JSON.stringify(simulation, null, 2)}</pre>
diff --git a/src/routes/entities/index.svelte b/src/routes/entities/index.svelte
new file mode 100644
index 000000000..f0e710b81
--- /dev/null
+++ b/src/routes/entities/index.svelte
@@ -0,0 +1,25 @@
+<script context="module" lang="ts">
+	export async function load({ page, fetch, session, context }) {
+		const url = "https://fr.openfisca.org/api/latest/entities";
+		const res = await fetch(url);
+
+		if (res.ok) {
+			return {
+				props: {
+					entityById: await res.json()
+				}
+			};
+		}
+
+		return {
+			status: res.status,
+			error: new Error(`Could not load ${url}`)
+		};
+	}
+</script>
+
+<script lang="ts">
+  export let entityById: any
+</script>
+
+<pre>{JSON.stringify(entityById, null, 2)}</pre>
\ No newline at end of file
diff --git a/src/routes/index.svelte b/src/routes/index.svelte
index d23ba33ce..88429daaa 100644
--- a/src/routes/index.svelte
+++ b/src/routes/index.svelte
@@ -42,13 +42,20 @@
 </style>
 
 <main>
-  <h1>Hello world!</h1>
+  <h1>Simulateur socio-fiscal Leximpact</h1>
 
   <Counter />
 
-  <p>
-    Visit
-    <a class="text-blue-600 underline" href="https://svelte.dev">svelte.dev</a>
-    to learn how to build Svelte apps.
-  </p>
+  <h2>Usage</h2>
+  <ul>
+    <li><a href="calculations">Calculations</a></li>
+    <li><a href="entities">Entities</a></li>
+    <li><a href="parameters">Parameters</a></li>
+    <li><a href="variables">Variables</a></li>
+  </ul>
+
+  <h2>Documentation</h2>
+  <ul>
+    <li><a href="spec">API documentation</a></li>
+  </ul>
 </main>
diff --git a/src/routes/parameters/index.svelte b/src/routes/parameters/index.svelte
new file mode 100644
index 000000000..9d7000d01
--- /dev/null
+++ b/src/routes/parameters/index.svelte
@@ -0,0 +1,25 @@
+<script context="module" lang="ts">
+	export async function load({ page, fetch, session, context }) {
+		const url = "https://fr.openfisca.org/api/latest/parameters";
+		const res = await fetch(url);
+
+		if (res.ok) {
+			return {
+				props: {
+					parameterById: await res.json()
+				}
+			};
+		}
+
+		return {
+			status: res.status,
+			error: new Error(`Could not load ${url}`)
+		};
+	}
+</script>
+
+<script lang="ts">
+  export let parameterById: any
+</script>
+
+<pre>{JSON.stringify(parameterById, null, 2)}</pre>
\ No newline at end of file
diff --git a/src/routes/spec.svelte b/src/routes/spec.svelte
new file mode 100644
index 000000000..3b6adba0c
--- /dev/null
+++ b/src/routes/spec.svelte
@@ -0,0 +1,25 @@
+<script context="module" lang="ts">
+	export async function load({ page, fetch, session, context }) {
+		const url = "https://fr.openfisca.org/api/latest/spec";
+		const res = await fetch(url);
+
+		if (res.ok) {
+			return {
+				props: {
+					spec: await res.json()
+				}
+			};
+		}
+
+		return {
+			status: res.status,
+			error: new Error(`Could not load ${url}`)
+		};
+	}
+</script>
+
+<script lang="ts">
+  export let spec: any
+</script>
+
+<pre>{JSON.stringify(spec, null, 2)}</pre>
\ No newline at end of file
diff --git a/src/routes/variables/index.svelte b/src/routes/variables/index.svelte
new file mode 100644
index 000000000..a73655606
--- /dev/null
+++ b/src/routes/variables/index.svelte
@@ -0,0 +1,25 @@
+<script context="module" lang="ts">
+	export async function load({ page, fetch, session, context }) {
+		const url = "https://fr.openfisca.org/api/latest/variables";
+		const res = await fetch(url);
+
+		if (res.ok) {
+			return {
+				props: {
+					variableById: await res.json()
+				}
+			};
+		}
+
+		return {
+			status: res.status,
+			error: new Error(`Could not load ${url}`)
+		};
+	}
+</script>
+
+<script lang="ts">
+  export let variableById: any
+</script>
+
+<pre>{JSON.stringify(variableById, null, 2)}</pre>
\ No newline at end of file
-- 
GitLab