You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Connecting to an external API is probably the number 1 question we have.
This commit improves (and rename) the "async function" documentation, using a more
step-by-step approach.
It's also added in the landing page to improve its discoverability, since it's
a common question.
Hopefully, this version is better than the previous one
Some of it was rephrased by ChatGPT.
closes#2560
Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
-[Connecting to an external API](#connecting-to-an-external-api)
16
16
17
17
## Adding a new custom function
18
18
@@ -420,33 +420,34 @@ addFunction("USER.NAME", {
420
420
});
421
421
```
422
422
423
-
## Asynchronous function
423
+
## Connecting to an external API
424
424
425
-
Function are synchronous. However, you can use a `getter` to fetch data from an [external source](#external-dependency).
426
-
Here is what a function fetching currency rate from a server might look like.
425
+
This section provides a step-by-step guide on implementing a function that connects to an external API in the o-spreadsheet library.
426
+
427
+
To illustrate the process, let's create a simple function called `CURRENCY.RATE` that returns the exchange rate between two currencies, such as `USD` and `EUR`.
428
+
429
+
Here is the basic structure of our `CURRENCY.RATE` function:
427
430
428
431
```ts
429
432
addFunction("CURRENCY.RATE", {
430
433
description:
431
-
"This function takes in two currency codes as arguments, and returns the exchange rate from the first currency to the second as float.",
arg("currency_from (string)", "The code of the first currency."),
437
+
arg("currency_to (string)", "The code of the second currency."),
444
438
],
439
+
compute:function (currencyFrom, currencyTo) {
440
+
// TODO: Implement the function logic here
441
+
},
445
442
returns: ["NUMBER"],
446
443
});
447
444
```
448
445
449
-
And add a [plugin](./extending/architecture.md#plugins) to handle data loading.
446
+
The `compute` function inside the function definition can use external dependencies available in its evaluation context. Refer to the [Custom external dependency](#custom-external-dependency) section for more details on how to implement data fetching and caching in your preferred manner.
447
+
448
+
To adhere to the o-spreadsheet's architecture, we'll use a dedicated [plugin](./extending/architecture.md#plugins) for this purpose. The `compute` function can access relevant data using its getters.
449
+
450
+
First, let's create the `CurrencyPlugin` class that extends `UIPlugin` and registers the necessary getters:
Next, we need to update the `compute` function to use the `getCurrencyRate` getter:
472
+
473
+
```ts
474
+
addFunction("CURRENCY.RATE", {
475
+
// ...
476
+
compute:function (currencyFrom, currencyTo) {
477
+
constfrom=toString(currencyFrom);
478
+
constto=toString(currencyTo);
479
+
returnthis.getters.getCurrencyRate(from, to);
480
+
},
481
+
// ...
482
+
});
483
+
```
466
484
467
-
// a cache to store fetched rates
468
-
this.currencyRates= {};
485
+
Now, let's address an issue: **spreadsheet functions are synchronous**. This means that our getter `getCurrencyRate` also needs to return synchronously.
486
+
487
+
To handle this requirement and enable caching of API results, we'll introduce a simple `cache` data structure within our plugin. Caching is important to avoid making repeated API calls when the function is evaluated multiple times during spreadsheet editing.
488
+
489
+
The `getCurrencyRate` function reads from the cache and returns the status. If the status is `"missing"`, the `fetch` method handles data fetching and updates the cache. The `getFromCache` and `fetch` methods are described below:
0 commit comments