__copyright__ = "Reiner Lemoine Institut, Zentrum für nachhaltige Energiesysteme Flensburg"
__license__   = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__       = "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__    = "wolfbunke, Ludee"

"""
Explanation: <img src="http://193.175.187.164/static/OEP_logo_2_no_text.svg" alt="OpenEnergy Platform" height="100" width="100"  align="left"/>
OpenEnergy Platform
<br>
End of explanation
"""


import requests
import pandas as pd
from IPython.core.display import HTML

# oedb
oep_url= 'http://oep.iks.cs.ovgu.de/'

# token
your_token = ''

"""
Explanation: Tutorial - How to work with the OpenEnergy Platform (OEP)
<br>
<div class="alert alert-block alert-danger">
This is an important information!
</div>
<div class="alert alert-block alert-info">
This is an information!
</div>
<div class="alert alert-block alert-success">
This is your task!
</div>

This tutorial gives you an overview of the OpenEnergy Platform and how you can work with the REST-full-HTTP API in Python. <br>
The full API documentaion can be found on ReadtheDocs.io.
Part II
0 Setup token <br>
1 Select data from a table <br>
2 Include filters <br>
3 More Options
Part II
0. Setup token
<br>
<div class="alert alert-block alert-danger">
Do not push your token to GitHub!
</div>
End of explanation
"""


# select data
schema = 'model_draft'
table = 'example_api_table_test'

get_data = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/')
get_data.status_code

"""
Explanation: 1. Select data from a table
End of explanation
"""


# Convert to dataframe
plant_df = pd.DataFrame(get_data.json())
plant_df

"""
Explanation: <div class="alert alert-block alert-info">
**200** succesfully selected data! <br>
</div>
End of explanation
"""


schema = 'model_draft'
table = 'example_api_table_test'
where = 'type=wind_onshore'

result = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/?where='+where, )
result.status_code

# Convert to dataframe
df = pd.DataFrame(result.json())
df

"""
Explanation: 2. Include filters
2.1 where
End of explanation
"""


schema = 'model_draft'
table = 'example_api_table_test'
column = 'column=name&column=id&column=type'

result = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/?'+column, )
result.status_code

# Convert to dataframe
df = pd.DataFrame(result.json())
df

"""
Explanation: 2.2 column
End of explanation
"""


schema = 'model_draft'
table = 'example_api_table_test'
limit = '2'

result = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/?limit='+limit, )
result.status_code

# Convert to dataframe
df = pd.DataFrame(result.json())
df

"""
Explanation: 2.3 limit
End of explanation
"""


schema = 'model_draft'
table = 'example_api_table_test'
order_by = 'id'

result = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/?orderby='+order_by, )
result.status_code
print(result)
# Convert to dataframe
df = pd.DataFrame(result.json())
df


schema = 'model_draft'
table = 'example_api_table_test'
order_by = 'id'
limit = '2'

result = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/?limit='+limit+'&orderby='+order_by, )
result.status_code
print(result)
# Convert to dataframe
df = pd.DataFrame(result.json())
df

"""
Explanation: 2.4 orderby
End of explanation
"""


schema = 'model_draft'
table = 'example_api_table_test'
where = 'type=photovoltaics'
column = 'column=name&column=id&column=type'

#result = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/?where='+where+'&'+column, )
#result = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/?where=type=photovoltaics&column=name&column=id&column=type', )
result.status_code

# Convert to dataframe
df = pd.DataFrame(result.json())
df

"""
Explanation: 2.5 Combine filters
End of explanation
"""


