- Practical Network Automation
- Abhishek Ratan
- 200字
- 2021-07-02 14:53:09
API access
Here, we call the weather API to get coordinates for a particular location (say London, England):
#use the city of london as a reference
$city="london"
$urlx="https://samples.openweathermap.org/data/2.5/weather?q="+$city+"&appid=b6907d289e10d714a6e88b30761fae22"
# used to Invoke API using GET method
$stuff = Invoke-RestMethod -Uri $urlx -Method Get
#write raw json
$stuff
#write the output of latitude and longitude
write-host ("Longitude: "+$stuff.coord.lon+" , "+"Latitude: "+$stuff.coord.lat)
The output is as follows:
PS C:\Users\abhishek.ratan> C:\gdrive\book2\github\edition2\chapter1\api_invoke_ps.ps1
coord : @{lon=-0.13; lat=51.51}
weather : {@{id=300; main=Drizzle; description=light intensity drizzle; icon=09d}}
base : stations
main : @{temp=280.32; pressure=1012; humidity=81; temp_min=279.15; temp_max=281.15}
visibility : 10000
wind : @{speed=4.1; deg=80}
clouds : @{all=90}
dt : 1485789600
sys : @{type=1; id=5091; message=0.0103; country=GB; sunrise=1485762037; sunset=1485794875}
id : 2643743
name : London
cod : 200
Longitude: -0.13 , Latitude: 51.51
As we can see in the code, a major difference between writing code in Python and PowerShell is that in PowerShell we do not need to focus on indentation. PowerShell does not care about indentation, whereas a Python compilation would fail if strict indentation was not adhered to.
Also, we do not need to import any specific library in PowerShell, as it has very extensive built-in functions that are directly callable from the script.
推薦閱讀
- 樂高機器人:WeDo編程與搭建指南
- Introduction to DevOps with Kubernetes
- Learning Microsoft Azure Storage
- Cinema 4D R13 Cookbook
- 商戰(zhàn)數(shù)據(jù)挖掘:你需要了解的數(shù)據(jù)科學與分析思維
- 腦動力:PHP函數(shù)速查效率手冊
- Google App Inventor
- Hands-On Machine Learning with TensorFlow.js
- 自動化控制工程設(shè)計
- 大數(shù)據(jù)平臺異常檢測分析系統(tǒng)的若干關(guān)鍵技術(shù)研究
- OpenStack Cloud Computing Cookbook(Second Edition)
- 強化學習
- 傳感器原理與工程應(yīng)用
- Learning Couchbase
- PVCBOT零基礎(chǔ)機器人制作(第2版)