- 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.
推薦閱讀
- 機(jī)器學(xué)習(xí)實(shí)戰(zhàn):基于Sophon平臺(tái)的機(jī)器學(xué)習(xí)理論與實(shí)踐
- 輕松學(xué)C語言
- 人工智能超越人類
- 極簡AI入門:一本書讀懂人工智能思維與應(yīng)用
- Dreamweaver CS3網(wǎng)頁設(shè)計(jì)與網(wǎng)站建設(shè)詳解
- 影視后期制作(Avid Media Composer 5.0)
- PyTorch深度學(xué)習(xí)實(shí)戰(zhàn)
- 模型制作
- 嵌入式Linux上的C語言編程實(shí)踐
- 中國戰(zhàn)略性新興產(chǎn)業(yè)研究與發(fā)展·智能制造
- 工業(yè)機(jī)器人應(yīng)用案例集錦
- 單片機(jī)C語言程序設(shè)計(jì)完全自學(xué)手冊
- 新編計(jì)算機(jī)圖形學(xué)
- 基于企業(yè)網(wǎng)站的顧客感知服務(wù)質(zhì)量評(píng)價(jià)理論模型與實(shí)證研究
- Unity Multiplayer Games