Corvid: Accessing Third-Party Services with the Fetch API
Client-Side Service Call
For this example, we've created a simple form with the following elements:
Type | ID | Usage |
---|---|---|
Input | textInputSymbol | For entering the currency to look up |
Button | buttonFetchRate | For submitting the request |
Text | textRate | A label for displaying the result |
When you have the form set up, you can set the button's properties so that its onClick
handler will call the following function:
1 2 3 4 5 6 7 8 9
export function buttonFetchRate_click(event) { let url = "https://api.exchangeratesapi.io/latest?base=USD"; let symbol = $w("#textInputSymbol").value; let fullUrl = url + "&symbols=" + symbol; fetch(fullUrl, {method: 'get'}) .then(response => response.json()) .then(json => $w("#textRate").text = json.rates[symbol].toString()); }
In this example, you start by constructing the full URL for the fetch request. The URL is made up of the service's address and some query parameters. For simplicity, we'll always convert U.S. dollars, so the base
parameter's value is hardcoded to USD
. The value for the symbols
parameter is pulled from the form. Then the function makes the actual request. When it receives a response, it pulls out the conversion rate for the symbol
we passed and sets it as the label's text.
Backend Service Call
First, you create a function in a backend web module to call the weather service and retrieve the data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//serviceModule.jsw import {fetch} from 'wix-fetch'; export function getCurrentTemp(city) { const url = 'https://api.openweathermap.org/data/2.5/weather?q='; const key = '<api key placeholder>'; let fullUrl = url + city + '&appid=' + key + '&units=imperial'; return fetch(fullUrl, {method: 'get'}) .then(response => response.json()) .then(json => json.main.temp); }
In this example, you start by importing fetch
from wix-fetch
, which is used to make https
requests. Then you create a new function that takes in a city
whose weather you want to look up. The function begins by constructing the full URL for the fetch request. The URL is made up of the service's address and an API key. To make this example work, you'd have to replace <api key placeholder>
with your own API key. Then the function makes the actual request. When it receives a response, it pulls out the temperature data. That temperature data will be received by the client-side call.
For this example, we've created a simple form that has the following elements:
Type | ID | Usage |
---|---|---|
Text Input | textInputCity | For entering the name of the city |
Button | buttonFetchTemp | For submitting the request |
Text | textTemp | A label for displaying the result |
When you have the form set up, you can set the button's properties so that its onClick
handler calls the function you wrote in the backend module.
1 2 3 4 5 6 7 8 9 10
//Form's page code import {getCurrentTemp} from 'backend/serviceModule'; //... export function buttonFetchTemp_click(event) { getCurrentTemp($w("#textInputCity").value) .then(temp => $w("#textTemp").text = temp + "°"); }
You start here by importing the function that you wrote in the backend. Then, in the button's event handler, call the getCurrentTemp
function. The Input element's value gets passed as the city
argument. When the function has finished running, set the text label to display the temperature that was returned.
Oscar perez
0 comentarios:
Publicar un comentario