Introduction to Cloud Storage models
• Communication APIs
• Webserver-Web server for IoT
• Cloud for IoT
• Python web application framework
• Designing a RESTful web API.
Size: 3.6 MB
Language: en
Added: Aug 11, 2023
Slides: 111 pages
Slide Content
Unit -V
IoT Physical Servers &
CloudOfferings
6/7/2022 1
Outline
•Introduction to Cloud Storage models
•Communication APIs
•Webserver-Web server for IoT
•Cloud for IoT
•Python web application framework
•Designing a RESTful web API.
6/7/2022 2
Cloud Storage API
•TheAPIdefinesthecorrectwayforadevelopertowriteaprogramthat
requestsservicesfromanoperatingsystem(OS)orotherapplication.
•APIsareimplementedbyfunctioncallscomposedofverbsandnouns.
•Therequiredsyntaxisdescribedinthedocumentationoftheapplication
beingcalled.
6/7/2022 13
How APIs Works
•APIsaremadeupoftworelatedelements.
•Thefirstisaspecificationthatdescribeshowinformationisexchanged
betweenprograms,doneintheformofarequestforprocessinganda
returnofthenecessarydata.
•Thesecondisasoftwareinterfacewrittentothatspecificationand
publishedinsomewayforuse.Thesoftwarethatwantstoaccessthe
featuresandcapabilitiesoftheAPIissaidtocallit,andthesoftwarethat
createstheAPIissaidtopublishit.
6/7/2022 14
Why APIs are important
•Theweb,softwaredesignedexchangeinformationviatheinternetand
cloudcomputinghaveallcombinedtoincreasetheinterestinAPIsin
generalandservicesinparticular.
•Softwarethatwasoncecustom-developedforaspecificpurposeisnow
oftenwrittenreferencingAPIsthatprovidebroadlyusefulfeatures,
reducingdevelopmenttimeandcostandmitigatingtheriskoferrors.
6/7/2022 15
Why APIs are important
•APIshavesteadilyimprovedsoftwarequalityoverthelastdecade,andthe
growingnumberofwebservicesexposedthroughAPIsbycloudproviders
isalsoencouragingthecreationofcloud-specificapplications,internetof
things(IoT)effortsandappstosupportmobiledevicesandusers.
6/7/2022 16
Starting Development with Django
CreatingaDjangoProjectandApp
•WhenanewDjangoprojectiscreated,anumberoffilesarecreatedas
describedbelow
6/7/2022 54
Starting Development with Django
•ADjangocanhavemultipleApps,wheretheseAppsareusedtowritethe
codethatmakeswebsitefunction.
•Whenanewapplicationiscreatedanewdirectoryfortheapplicationis
alsocreatedwhichhasanumberoffilesincluding–
6/7/2022 55
Starting Development with Django
6/7/2022 56
Starting Development with Django
•Djangocomeswithabuiltin,lightweightwebserverthatcanbeusedfor
developmentpurposes.
•WhentheDjangodevelopmentserverisstartedthedefaultprojectcanbe
viewedattheURL:http://localhost:8000.
6/7/2022 57
Starting Development with Django
6/7/2022 58
Starting Development with Django
ConfiguringaDatabase
•Developershaveawidechoiceofdatabasesthatcanbeusedforweb
applicationsincludingbothrelationalandnonrelationaldatabases.
•DjangoprovidesaunifiedAPIfordatabasebackendthusgivingthe
freedomtochoosethedatabase.
•DjangosupportsvariousrelationaldatabaseenginesincludingMySQL,
PostgreSQL,OracleandSQLite3.
6/7/2022 59
Starting Development with Django
•SupportfornonrelationaldatabasessuchasMongoDBcanbeaddedby
installingadditionalengines(Eg.Django-MongoDBenginefor
MkongoDB).
•Thefirststepinsettingupadatabaseistoinstallandconfigurea
databaseserver.
•Afterinstallingthedatabase,thenextstepistospecifythedatabase
settingsinthesetting.pyfileintheDjangoproject.
6/7/2022 60
Commands to setup My SQL
6/7/2022 61
Database setting to use MySQL with a
DjangoProject
6/7/2022 62
6/7/2022 63
Starting Development with Django
6/7/2022 64
Defining a Model
•AModelactsasadefinitionofthedatainthedatabase.
•Box8.10showsanexampleofaDjangomodelforTemperatureData
collectedbyanIoTDevice.
•TheTemperatureDatatableinthedatabaseisdefinedasaclassinthe
DjangoModel.
•Eachclassthatrepresentsadatabasetableisasubclassof
django.db.models.Modelwhichcontainsallthefunctionalitythatallows
themodelstointeractwiththedatabase.
6/7/2022 65
Defining a Model
•TheTemperatureDataclasshasfieldstimestamp,temperature,latandlon
allofwhichareCharField.
•Tosyncthemodelswiththedatabase,runthefollowingcommand:
•>pythonmanage.pysyncdb
•Whenthesyncdbcommandisrunforthefisttime,itcreatesallthetables
definedintheDjangomodelintheconfigureddatabase.
6/7/2022 66
Defining a View
•In the view shown in Box 8.12, the
TemperatureData.objects.order_by(‘_id’)queryreturnsthelatestentryin
thetable.
•Toretrieveallentries,canusetable.objects.all().
•Toretrievespecificentries,usetable.objects.filter(**Kwargs)tofilterout
queriesthatmatchthespecifiedcondition.
6/7/2022 75
Defining the URL Patterns
•Box8.15showsanexampleoftheURLpatternsfortheWeatherStation
ProjectandtheURLpatternsareconstructedusingregularexpressions.
•Thesimplestregularexpression(r’^$’)correspondstotherootofthe
websiteorthehomepage.
6/7/2022 87
Defining the URL Patterns
6/7/2022 88
Defining the URL Patterns
•WiththeDjangomodels,views,templatesandURLpatternsdefinedfor
theDjangoproject,theapplicationfinallyrunswiththecommandsshown
inBox8.16.
•
6/7/2022 89
Designing a RESTfulWeb API
•DjangoRESTframeworkcanbeinstalledasfollows:
6/7/2022 90
Designing a RESTfulWeb API
•AfterinstallingtheDjangoRESTframework,createanewDjangoproject
namedrestfulapi,andthenstartanewappcalledmyapp,asfollows:
6/7/2022 91
Designing a RESTfulWeb API
•TheRESTAPIallowstocreate,view,updateanddeleteacollectionof
resourceswhereeachresourcerepresentsasensordatareadingfroma
weathermonitoringstation.
•Box8.17showstheDjangomodelforsuchastation.
•Thestationmodelcontainsfourfields–stationname,timestamp,
temperature,latitudeandlongitude
6/7/2022 92
Designing a RESTfulWeb API
6/7/2022 93
Designing a RESTfulWeb API
•Box8.18showstheDjangoviewsfortheRESTAPI.
•ViewSetsareusedfortheviewsthatallowstocombinethelogicforaset
ofrelatedviewsinasingleclass.
•
6/7/2022 94
Designing a RESTfulWeb API
•
6/7/2022 95
Designing a RESTfulWeb API
6/7/2022 96
Designing a RESTfulWeb API
•Box8.19showstheserializersfortheRESTAPI.
•Serializersallowcomplexdata(suchasquerysetsandmodelinstances)
tobeconvertedtonativepythondatatypesthatcanbeeasilyrendered
intoJSON,XMLorothercontenttypes.
•Serializersalsoprovidede-serialization,allowingparseddatatobe
convertedbackintocomplextypes,afterfirstvalidatingtheincoming
data.
6/7/2022 97
Designing a RESTfulWeb API
6/7/2022 98
Designing a RESTfulWeb API
•Box8.20showstheURLpatternsfortheRESTAPI.
•Since,ViewSetsareusedinsteadofviews,itcanautomaticallygenerate
theURLconfforAPI,byregisteringtheViewSetswitharouterclass.
•RoutersautomaticallydetermininghowtheURLsforanapplication
shouldbemappedtothelogicthatdealswithhandlingincoming
requests.
6/7/2022 99
Designing a RESTfulWeb API
6/7/2022 100
Designing a RESTfulWeb API
•Box8.21showsthesettingsfortheRESTAPIDjangoproject.
6/7/2022 101
Designing a RESTfulWeb API
•Box8.21showsthesettingsfortheRESTAPIDjangoproject.
6/7/2022 102
Designing a RESTfulWeb API
•AftercreatingthestationRESTAPIsourcefiles,thenextstepistosetup
thedatabaseandthenruntheDjangodevelopmentwebserveras
follows:
6/7/2022 103
Designing a RESTfulWeb API
•Box8.22showsexamplesofinteractingwiththeStationRESTAPIusing
CURL.
•TheHTTPPOSTmethodisusedtocreateanewresource,GETmethod
isusedtoobtaininformationaboutaresource,PUTmethodisusedto
updatearesourceandDELETEmethodisusedtodeletearesource.
6/7/2022 104