Maxbox Starter104 restcountriesAPI
Maxbox Starter104 restcountriesAPI
Restcountries API
____________________________________________________________________________
maXbox Starter 104 – Countries API microservices.
“A map is worth a thousand words.
An interface is worth a thousand maps.”.
Pic: 1180_osm_map_tutor104.jpg
As you can see we use OpenStreetMap which is a map of the world, created
by people like you and free to use under an open licence.
http://www.softwareschule.ch/examples/jsonlib.htm
1/7
Simply put, an endpoint of one end of a communication channel. When an
API interacts with another system, the touchpoints of this communication
are considered endpoints. For APIs, an endpoint can include a URL of a
server or service. Each endpoint is the location from which APIs can
access the resources they need to carry out their function based on
versions.
Const
RESTCountries = 'https://restcountries.com/v3.1/name/%s';
RESTCountriesC = 'https://restcountries.com/v2/capital/%s';
writeln('capital: '+(jo.getstring('capital')));
writeln('nativename: '+
jo.getjsonobject('name').getjsonobject('nativename').getjsonobject('fra').getstr
ing('common'));
writeln('len translations: '+itoa(jo.getjsonobject('translations').length))
jo2:= jo.getjsonobject('translations');
ajar:= jo.names;
writeln('opt: '+ajar.optstring(6))
writeln('len node names: '+itoa(ajar.length))
for it:= 0 to jo2.length-1 do
writeln(jo2.getstring(jo2.keys[it]));
2/7
ajar:= jo.getjsonarray('borders');
writeln('len names borders: '+itoa(ajar.length))
for it:= 0 to ajar.length-1 do
writeln(ajar.getstring(it));
jo2:= jo.getjsonobject('languages');
writeln('len names languages: '+itoa(jo2.length))
for it:= 0 to jo2.length-1 do begin
//writeln(jo2.getstring(it));
writeln(jo2.getstring(jo2.keys[it]));
end;
writeln(jo.getjsonobject('flags').getstring('png'))
openweb(jo.getjsonobject('maps').getstring('openStreetMaps'));
except
//writeln('Error: '+mapstrm.datastring);
writeln('E: '+ExceptiontoString(exceptiontype, exceptionparam));
finally
mapStrm.Free;
encodURL:= '';
jo.Free;
//ajar.Free; //jo2.Free;
end;
end;
You can modify a lot of member parameters in JSON as you like and
interact with the API from many languages. The API export format is JSON,
e.g. our name/capital call see below:
Pic: 1180_jsonstruct_tutor104.png
3/7
To validate JSON we use jsonlint:
https://jsonlint.com/
JSONLint is by the way an online editor, validator, and reformat tool for
JSON, which allows you to directly type your code, copy and paste it, or
input a URL containing your code. It will validate your JSON content
according to JS standards, informing you of every human-made error, which
happens for a multitude of reasons – one of them being the lack of focus,
for example a not valid and a valid JSON:
const HIDDENT4INVALID=
'{'+
' "tms_guid": "9LaHmoHpmTd811R", '+
' "recharge_status": "100", '+
' "message": "Transaction Successful", '+
' "response_time": { '+
' "verifyClient": 0.0281, '+
' "verifyGuid": 0.8695, '+
' "verifyOperator": 0.8698, '+
' "verifyMsid": 0.8698, '+
' "tms_guid": 1.6971, '+
' "queryErr": 7.4243, '+
' "StoringRecharge": 7.4358, '+
' "UpdatingBalance": 7.448 '+
' } '+
'} ';
const HIDDENT4VALID=
'{'+
'"tms_guid": "9LaHmoHpmTd811R", '+
'"recharge_status": "100", '+
'"message": "Transaction Successful", '+
'"response_time": { '+
' "verifyClient": 0.0281, '+
' "verifyGuid": 0.8695, '+
' "verifyOperator": 0.86 '+
' "verifyMsid": 0.8698, '+
' "tms_guid": 1.6971, '+
' "queryErr": 7.4243, '+
' "StoringRecharge": 7.4358, '+
' "UpdatingBalance": 7.448 '+
' } '+
'}';
At a first sight you don't see a difference but it depends on the non
printable characters like tab or space on the structure. The grey shaded
areas are the problem for a non valid result.
If you use a Win computer for example you may end up with different
results. This is possibly due to the way Win handles newlines or tabs.
4/7
Essentially, if you have just newline characters (\n) in your JSON and
paste it into JSON Lint from a Win computer, it may validate it as valid
erroneously since Win may need a carriage return (\r) as well to detect
newlines properly. As a solution, either use direct URL input as we use,
or make sure your content's newlines match or the code-page of UTF-8
match the architecture your system expects!
pic: 1180_jsonstruct_validator104.png
Using JSONLint, you can quickly find any errors that might've occurred,
allowing you to focus more on the rest of your script-code than on a tiny
error itself. But the hack I said before (replace [{ to { took me many
hours cause few people said to replace from [{ to {[{, which also means
in a context of arrays use [] not {} as the outer brackets but my library
expects opening and closing curly brackets.
{"official":"Swiss\u0020Confederation","common":"Switzerland"}
5/7
len names languages: 4
French
Swiss German
Italian
Romansh
https://flagcdn.com/w320/ch.png
6/7
Furthermore the API access is provided in a REST-like interface
(Representational State Transfer) exposing database resources or pre-
trained models in a JSON format with content-type in the Response Header.
Note: If a programming language is not listed in the Code Example from
the live demo, you can still make API calls by using a HTTP request
library written in our programming language, as we did with GET or POST.
Pic: 1176_apilayer_demo_mX4.png
Conclusion:
Restful Countries API allows users to explore the entire database for
information on countries and their states, presidents, flag, population,
covid19 stats and others.
Restful Countries API is organized around REST. Our API has predictable
resource-oriented URLs, returns JSON-encoded responses and uses standard
HTTP response codes, and verbs.
Reference:
https://github.com/maxkleiner/restcountries
https://apilayer.com/docs
7/7