In this lab you will learn about API’s (Application Programming Interfaces), specifically Web API’s. Web API’s are API’s that are accessible over the HTTP protocol. They represent the server-side behavior of the device-driven web of today: smart phones, intelligent speakers, smart TV’s, etc.
Specifically, we will create a Web API to keep track of craft Beers that you’ve consumed and your opinions of them. Typically, an API like this would be the data and business logic back-end for a Mobile application. A Smart Phone would be how the users interact with the application and any data would be sent over the cloud to the back-end service.
We will build out the back-tiers in an open-source, content-as-a-service platform known as Squidex https://squidex.io/. If this were a real working system, the architecture would look something like this:
+----------------+ +----------+ +---------+
| Mobile App | |Beer API | | MongoDb |
| iOS / Android |---Internet---| Squidex |-----| Data |
| client Tier | |Bus. Logic| | Storage |
+----------------+ +----------+ +---------+
Building a mobile application is beyond the scope of this lab, so instead we will simply use the curl
command to connect to the web API we create.
curl
utility.ist346-labs
PS > cd ist346-labs
PS ist346-labs> git pull origin master
lab-L
folder:PS ist346-labs> cd lab-L
As we said before, we use an open source project Squidex to create our Web API. Squidex is a headless content management system. It allows you to create custom content definitions (schema) and it will create an HTTP API that allows you to create, retrieve, update and delete (CRUD) your custom content.
PS C:\ist346-labs\lab-L> docker-compose up -d
PS C:\ist346-labs\lab-L> docker-compose ps
curl-client
, squidex_mongo
database and the squidex_squidex
application. All should be Up.admin@example.com
and mySecret123!
. You will then come to the tutorial. Walk through the tutorial to understand the different features of Squidex. Once the tutorial is complete you should end up the screen shown below:http://localhost/api/content/my-beer-app/
60 Minute IPA
Dogfish Head
38.74
lng: -75.34
IPA
150
test-client
for the client name and click Add Client you should end up with something similar to the image below.To access the API you will need to generate an OAuth 2 bearer access token. This is an opaque string, generated from the client secret which provides the bearer of the token access to the API for a limited amount of time. Any client with the access token can access the API with the set permissions for as long as the token is valid.
Typically the access token is generated as requested by an authenticated client. For our case we are just going to generate it within the Squidex user interface. To generate a new token, click the Create Token button. It will show a modal dialog containing your access token.
IMPORTANT copy and paste this token into a blank Notepad or TextEdit document for later use.
Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IjkxRkRENEVCRDYwNjMxNURFREI4MENEMDkzMERFRkZBMjFEREE2NkIiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJrZjNVNjlZR01WM3R1QXpRa3czdi1pSGRwbXMifQ.eyJuYmYiOjE1NDA2NTI0MzQsImV4cCI6MTU0MzI0NDQzNCwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdC9pZGVudGl0eS1zZXJ2ZXIiLCJhdWQiOlsiaHR0cDovL2xvY2FsaG9zdC9pZGVudGl0eS1zZXJ2ZXIvcmVzb3VyY2VzIiwic3F1aWRleC1hcGkiXSwiY2xpZW50X2lkIjoibXktYmVlci1hcHA6dGVzdC1jbGllbnQiLCJzY29wZSI6WyJzcXVpZGV4LWFwaSJdfQ.wkPN_hl4ftxUPdHdXDh0Yqe7Vl5UIiDYwCuoogG6pzg5WYesWB7DoRxjG22xk3RJl5Wk7ihOyO2GA3QG9dClrk_8Ko_dDVaYOB0AZtikNw5AVXg7vAm0FmApMm9O02gSZqMQ8VQVUIETl64CB_X-5pIlU0_eGnRlWatkWcdLfW9P_Ex9-msMGCfOw0npHGUoLRbZicBVPb37l_n_Zmh2hJBeoJJUKAFLGzkzAQOR1y0xRMwz4W1pGFxPU9-dGSB7nSMNALg55AAoqvJOtrQq1il6qu1Qu681Dvg75BkyRFCkCIn3oSeolksl1SxZwa0OXQlD6mZDTNWTjJXZfAWV_g
In this part, we will use the curl
utility to consume our Beers REST API. Make sure you enter the access token you retrieved where indicated.
curl
command: PS C:\ist346-labs\lab-L> docker-compose exec curl-client bash
root@client:/#
prompt. Let’s find the beers available. According to the documentation at http://localhost/api/content/my-beer-app/docs we initiate a GET request to the beers
endpoint using our token:root@client:/# curl -X GET http://squidex_squidex/api/content/my-beer-app/beers -H "Authorization: YOUR_ACCESS_TOKEN"
YOUR_ACCESS_TOKEN
with the access token you saved from the previous step. For Example, the request with my Token:root@client:/# curl -X GET http://squidex_squidex/api/content/my-beer-app/beers -H "Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IjkxRkRENEVCRDYwNjMxNURFREI4MENEMDkzMERFRkZBMjFEREE2NkIiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJrZjNVNjlZR01WM3R1QXpRa3czdi1pSGRwbXMifQ.eyJuYmYiOjE1NDE1Mzc4NjIsImV4cCI6MTU0NDEyOTg2MiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdC9pZGVudGl0eS1zZXJ2ZXIiLCJhdWQiOlsiaHR0cDovL2xvY2FsaG9zdC9pZGVudGl0eS1zZXJ2ZXIvcmVzb3VyY2VzIiwic3F1aWRleC1hcGkiXSwiY2xpZW50X2lkIjoibXktYmVlci1hcHA6dGVzdC1jbGllbnQiLCJzY29wZSI6WyJzcXVpZGV4LWFwaSJdfQ.nzH1tpARXqIHsACJ0gNMYnXytsw21JblGoIQZJqZlUJV7wY6Fc6IHqVAfHai7mvPv0qgVdXLxXDBCwup4Ze25VM5kkf0df6ObH6ucl-DQjowdJB8aLIgLTezq1d899oowz_BtS4e7-Rk4BFVtGnHE-TXAM_CHiNl9IMZbdNpgyoR4GoSdRsqBkERzyYHcvEkqQRJL-BpkTzAXsJJB1TOP-37fIsQXEEQ2rmmZ49WEvTs3P_a_sy1KQ94X4x0gJE9kAkLRnAtHjWjrTo0PAqpeVccdhOcsosqaRjDSt6e4lilVtuNXiQVnQWg83Ge40My67zNG2Er1bQQIruXp0LfSw"
{"total":1,"items":[{"id":"7e6218c6-0734-4bbb-a615-afdedddde415","createdBy":"subject:5be1d4ab62b8c60001189ebf","lastModifiedBy":"subject:5be1d4ab62b8c60001189ebf","data":{"name":{"iv":"60 Minute IPA"},"brewery":{"iv":"Dogfish Head"},"location":{"iv":{"latitude":38.74,"longitude":-75.34}},"type":{"iv":"IPA"},"calories":{"iv":150}},"isPending":false,"created":"2018-11-06T18:13:01Z","lastModified":"2018-11-06T18:13:01Z","status":"Published","version":1}]}
"total":1
let’s us know the API returned 1 item (the only beer we have right now).beers
endpoint. Do not forget to replace YOUR_ACCESS_TOKEN
with your bearer token. Type: root@client:/# curl -X POST http://squidex_squidex/api/content/my-beer-app/beers?publish=true -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: YOUR_ACCESS_TOKEN" --data '{"name": {"iv": "Summer Ale"},"brewery": {"iv": "Samuel Adams"},"location": {"iv": {"latitude": 42.36,"longitude": -71.06}},"type": {"iv": "American Pale Wheat Ale"},"calories": {"iv": 150}}'
--data
to add the Sam Adams Summer Ale to our collection of beers."createdBy":"my-beer-app:test-client"
indicating the client we used to initiate the POST added the record.root@client:/#
command line let’s query the beers
endpoint with an HTTP GET so we can see there are now 2 beers when we query the API. Again replace YOUR_ACCESS_TOKEN
with your bearer token.root@dclient:/# curl -X GET http://squidex_squidex/api/content/my-beer-app/beers -H "Authorization: YOUR_ACCESS_TOKEN"
"total":2
.id
key for this beer to the endpoint. You should be able to find it in your JSON output from the previous command, or by clicking on the beer in the Squidex UI and then reading it off the URL. My screenshot demonstrates this but your ID will be different from mine!
YOUR_BEER_ID
with the id
of the 60 minute IPA and replace YOUR_ACCESS_TOKEN
with your bearer token.:root@dclient:/# curl -X PATCH http://squidex_squidex/api/content/my-beer-app/beers/YOUR_BEER_ID -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: YOUR_ACCESS_TOKEN" --data '{"calories":{"iv":209}}'
{"calories":{"iv":209}}
. You can check the change in the Squidex UI or by executing the HTTP GET command again.id
so let’s remove the 60 Minute IPA. Try this:root@dclient:/# curl -X DELETE http://squidex_squidex/api/content/my-beer-app/beers/YOUR_BEER_ID -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: YOUR_ACCESS_TOKEN"
Even though you are using curl this is exactly how other application and clients interact with web API’s. If we built a mobile app or Website, the code on the app or site would perform these same commands to communicate with the API!
root@client:/# exit
PS ist346-labs\lab-L> docker-compose down