mardi 29 décembre 2020

How do applications know how return the data an API requested?

Let's say I created Twilio. Below is their alerts API

https://www.twilio.com/docs/usage/monitor-alert:

Download the helper library from https://www.twilio.com/docs/python/install

import os
from twilio.rest import Client

Your Account Sid and Auth Token from twilio.com/console and set the environment variables. See http://twil.io/secure

account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)

alert = client.monitor.alerts('NOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX').fetch()

print(alert.alert_text)

Example JSON API response:

{
  "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "alert_text": "alert_text",
  "api_version": "2010-04-01",
  "date_created": "2015-07-30T20:00:00Z",
  "date_generated": "2015-07-30T20:00:00Z",
  "date_updated": "2015-07-30T20:00:00Z",
  "error_code": "error_code",
  "log_level": "log_level",
  "more_info": "more_info",
  "request_method": "GET",
  "request_url": "http://www.example.com",
  "request_variables": "request_variables",
  "resource_sid": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "response_body": "response_body",
  "response_headers": "response_headers",
  "request_headers": "request_headers",
  "sid": "NOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "url": "https://monitor.twilio.com/v1/Alerts/NOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "service_sid": "PNe2cd757cd5257b0217a447933a0290d2"
}

How does the application return the data above? How does it know to return the above data?

DO you have to create an object with the data above to respond to said API call> Does the programmer need to write a special function, they would not normally create if they didn't want to provide API access to the application, to respond to the api call that will return the data?

So for example if I had a website that enabled people to enter in their first and last names into a database. I would then need to write a program that utilized some function that inserted the names. If I then wanted to create an API to give others to retrieve the names from the database I would need to create another function, that I wouldn't create if I didn't have the API, to retrieve the names for the API call. Or I would create an API call that would make a request to some function that would return an object with all the data I said my API call would return; or the function would go searching around for the data create an object and then return the information to the person who made the call.

Aucun commentaire:

Enregistrer un commentaire