Call external API
The code in every language
Each example is the inside of main, written in every language API Maker runs : pick yours in the tabs, and every page shows it from then on. The types a call takes (GetAll, DeepItem…) are those of am : from am import GetAll in Python, use am::GetAll; in Rust, import am.GetAll; in Java and Kotlin ; PHP and C# have them without an import, Go names them am.GetAll.
g.sys.system.callExternalApi calls the call external API system API from your code, whatever the settings of that API say about HTTP callers.
Call external API using global object 'g'.
Simple call external apis¶
Parallel type¶
- If the type is 'parallel' the external APIs are executed in parallel.
Sequential type¶
- If the type is 'sequential' the API calls execute one by one.
Path and query parameters, an API key, a form¶
pathParamsfills:customerIdof the URL,queryArrayFormat: 'repeat'writes the list astags=web&tags=priority, andbodyType: 'form'sends the body asproduct=Pen&quantity=3.throwOnErrorthrows when the API answers with a status of 400 or above.
Upload files¶
bodyType: 'multipart'sends a form of several parts : every key of the body is a field, and{ file }a file of the uploads folder.- A file a form sent to your custom API is sent on as it is :
document: g.req.files.files[0].
XML and SOAP¶
bodyType: 'xml'sends the text of the body as it is,responseType: 'text'gives the answer as its text, whatever it holds.retrysends the request again when it got no answer, or one of the statuses 408, 429, 500, 502, 503 and 504.
OAuth 2, redirects and retries¶
- The token is asked of
tokenUrlwith the id and the secret of the client, kept until it expires, and asked for again when the API refuses it. okistruewhen the status of the answer is 200 to 299.
Download a file¶
downloadsaves the body of the answer as a file of the uploads folder, and the answer hasfile: itspathin the uploads folder, itsname, itssizeand itscontentType.- Your code reads the file at
uploads/+file.path. A custom API sends it to its caller by returning{ __am__downloadFilePath: saved.file.path }. - Redirects are followed. When the status of the answer is not 200 to 299 there is no
file: the answer is given as any other.
Bytes¶
responseType: 'base64'gives the bytes of the answer as Base64 text, in every language.'buffer'gives aBufferin TypeScript.bodyType: 'binary'sends bytes : their Base64 text, aBuffer, or{ file }for a file of the uploads folder.
A call which may fail¶
allowFailure: when the call fails, its answer is{ statusCode: 0, ok: false, error: { message } }, and the calls after it run.throwOnError: a status of 400 or above ends the list with an error.retry: 2sends the request two more times first.