<jupyter_start><jupyter_text>Introduction to requests and API's  APIs : What and why    An API (Application Programming interface) is a way for two different applications to communicate. Whilst the term applies to any two programs we are using it to refer to the API of a web service that provides data.To retrieve data from an API, a request to a remote web server is made.For example, if you want to build an application which plots stock prices, you would use the API of something like google finance to request the current stock prices.APIs are useful where:* Data is changing quickly, e.g. stock prices* The whole dataset is not required, e.g. the tweets of one user* Repeated computation is involved, e.g. Spotify API that tells you the genre of a piece of music RESTMost API's you come across will be RESTful, i.e. they provide a REST (REpresentational State Transfer) interface.REST uses standard HTTP commands which means that getting data from an API is similar to accessing a webpage. For example, When you type `www.duckduckgo.com` in your browser, your browser is asking the `www.duckduckgo.com` server for a webpage by making a `GET` HTTP (Hypertext Transfer Protocol) request. Making a `GET` request to a RESTful API instead retrieves data (rather than a webpage).Similarly, while your browser uses `POST` to submit the contents of a form, REST APIs use `POST` to update data.REST APIs also uses other HTTP commmands such as `PUT` - for creating data - and `DELETE` - for removing data.HTTP is a text-based protocol (the response is always text) and could return a response in any format - this is typically found in the API documentation - though data is more often than not returned in JSON format.As they are used to retrieve data `GET` requests are the most commonly used type of request, therefore we will restrict ourselves to `GET` in this tutorial. JSONJSON (JavaScript Object Notation) is a format for sending data, that is meant to be human readable and easy to parse (It was derived from JavaScript but is language-independent).It uses attribute-value pairs (e.g. python dictionaries `{"class": "Energy Services", "year": 2022}`) and array data-types (e.g. python lists `[1, 2, 3]`)Example JSON representation :```{  "Class": "Energy Services",  "year": 2022,  "city": "Lisbon"  "addresses":[      {        "streetAddress": "Av. Rovisco Pais, 1",        "city": "Lisboa",        "postalCode": "1200",        "country": "PT"]        }}```<jupyter_code>import json
# a Python object (dict):
x = {
  "Class": "Energy Services",
  "year": 2022,
  "city": "Lisbon"
}

# convert into JSON:
y = json.dumps(x)

# the result is a JSON string:
print(y)<jupyter_output>{"Class": "Energy Services", "year": 2022, "city": "Lisbon"}<jupyter_text>Status codesSo we've sent off some mystery `GET` request but how do we know the request was successful?Servers issue numeric [status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) in response to HTTP requests that indicate whether a request has been successfully completed.Some common ones relating to `GET` requests are:* `200` - Success* `300` - The API is redirecting to a different endpoint* `400` - Bad request* `401` - Not authenticated* `403` - Forbidden* `404` - Not found* `429` - Too many requests  List of API'sMassive list [**here**](https://github.com/public-apis/public-apis) API wrapper librariesMassive list [**here**](https://github.com/realpython/list-of-python-api-wrappers).For example, geo-code location data with [`geopy`](https://github.com/geopy/geopy) :<jupyter_code>#! pip install numpy==1.21.2
#! pip install pandas==1.1.0
import numpy as np
print(np.__version__)
# libraries
import urllib.request
import json
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
api_link="https://api.eot.pt/api/pulse/last/power?key=<KEY>OQZ"

req=urllib.request.urlopen(api_link)
req_body=req.read()
value=json.loads(req_body.decode("utf-8"))
print(value/1000, 'kW')
#API for North Tower
api_link="https://energist.tecnico.ulisboa.pt/api/v1/m2m/4d7768b47bfbf9e32c975f15df498370"

req=urllib.request.urlopen(api_link)
req_body=req.read()
j=json.loads(req_body.decode("utf-8"))
o=j["outputs"]
i=o[0]
value=pd.DataFrame(i)

value2=value.iloc[0]["results"]
print(value2/1000 ,"[kWh]")
#API for IST Total
api_link="https://api.eot.pt/api/meter/feed.json?key=FHVAL4DWEXBQ3XNP&results=1000"
req=urllib.request.urlopen(api_link)
req_body=req.read()
#
j=json.loads(req_body.decode("utf-8"))
j.keys()
o=j["channels"]
value=pd.DataFrame(o)
value
i=o[5]["feeds"]  #5 temperaturee
temperature=pd.DataFrame(i)
temperature
temperature['created_at'] = pd.to_datetime(temperature['created_at'])
temperature = temperature.set_index('created_at')
temperature
temperature['value'] = temperature['value'].apply(lambda x: float(x))
temperature['value']
#temperature['value'].plot(linewidth=0.5);
temp=float(temperature.value[0])

print("Room V1.10 - Temperature:", temp,"ºC")<jupyter_output>Room V1.10 - Temperature: 21.51 ºC<jupyter_text>NREL APIDocumentation: https://developer.nrel.gov/<jupyter_code>import pandas as pd
api_link2="https://developer.nrel.gov/api/solar/solar_resource/v1.json?api_key=DEMO_KEY&lat=40&lon=-105"
req=urllib.request.urlopen(api_link2)
req_body=req.read()

j=json.loads(req_body.decode("utf-8"))
o=j["outputs"]
i=o["avg_dni"]
df=pd.DataFrame(i)
fig=px.bar(df,x=df.index,y=df.monthly )
fig.show()
#fig1=plt.bar(df.index,df.monthly)<jupyter_output><empty_output><jupyter_text>ENTSOE APIReading from an API ENTSOE Transparency Platform: https://transparency.entsoe.eu/dashboard/showUsing ENTSOE API ("To request access to the Restful API, please register on the Transparency Platform and send an email to <EMAIL> with “Restful API access” in the subject line. Indicate the email address you entered during registration in the email body. The ENTSO-E Helpdesk will make their best efforts to respond to your request within 3 working days."user guide: https://transparency.entsoe.eu/content/static_content/Static%20content/web%20api/Guide.html<jupyter_code>! pip install entsoe-py==0.4.1 #entsoe API
#or Force pandas>=1.4.0 in requirements #163
from entsoe import EntsoePandasClient
from entsoe import EntsoeRawClient
import pandas as pd
client = EntsoePandasClient(api_key='*******') #have to have a key
start = pd.Timestamp('20211201', tz ='Europe/Lisbon')
end = pd.Timestamp('20220301', tz ='Europe/Lisbon')
country_code = 'PT'  # Portugal
#day-ahead market prices (€/MWh)
client.query_day_ahead_prices(country_code, start=start,end=end)
# methods that return Pandas Series load
client.query_load(country_code, start=start,end=end)
# unavailability of generation units
client.query_unavailability_of_generation_units(country_code, start=start,end=end, docstatus=None)
#generation
client.query_generation(country_code, start=start,end=end, psr_type=None)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11761297798377753, 'fraction_numerical': 0.02187137891077636, 'mean_word_length': 5.633045148895293, 'pattern_counts': {'":': 16, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 11, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '19858', 'license': 'MIT', 'n_tokens_mistral': 2333, 'n_tokens_neox': 2182, 'n_words': 725, 'path': 'Energy_Services_Intro_APIs.ipynb', 'repo_name': 'INmais/Energy_Services_2022', 'size': '6927'}