Skip to content

Commit

Permalink
Major changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
eelkjaer committed Feb 18, 2021
1 parent ffbc97d commit 16a0a35
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 52 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
### OWN ###
credentials.py
.credentials.py
./credentials.py
*/credentials.py
#barry_energy_api/credentials.py
#.credentials.py
#barry_energy_api/credentials.py
#*/credentials.py


### Linux ###
Expand Down
66 changes: 65 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,65 @@
On its way
<h1 align="center">Welcome to Barry Energy API Client 👋</h1>
<p>
<img alt="Version" src="https://img.shields.io/badge/version-1.0.0-blue.svg?cacheSeconds=2592000" />
<a href="LICENSE" target="_blank">
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" />
</a>
</p>

> Simple python package to work with the API of Barry Energy.
> The API documentation can be found [here](https://developer.barry.energy).
> Please be aware that this project is <u>NOT</u> offical.
## Install

```sh
pip3 install barry-energy-api
```

## Usage

#### Get your API Token
```sh
Get from mobile app under "Add-ons" -> "Barry API"
```
<br>

1. Import the package
```python
from barry_energy_api import PriceZone, BarryEnergyAPI
```
2. Initialize the API
```python
api = BarryEnergyAPI('api-token', 5, PriceZone.DK_EAST)
```
3. Get aggregated consumption for the last 5 days as defined when API was initialized.
```python
api.consumption()
```
4. Get prices from the last two days.
##### Notice that methode has overloading
```python
api.spotprices(2)
```


## Author

👤 **Emil Elkjær Nielsen**

* Website: https://evsn.dk
* Github: [@eelkjaer](https://github.com/eelkjaer)
* LinkedIn: [@emil-elkjær](https://linkedin.com/in/emil-elkjær)

## Show your support

Give a ⭐️ if this project helped you!

## 📝 License

Copyright © 2021 [Emil Elkjær Nielsen](https://github.com/eelkjaer) <br />
This project is [MIT](LICENSE) licensed.

***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_
88 changes: 88 additions & 0 deletions barry_energy_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import enum

from barry_energy_api.entities import meteringpoints, spotprice, consumption
from barry_energy_api.entities.consumption import getConsumption
from barry_energy_api.entities.meteringpoints import getMeteringPoints
from barry_energy_api.entities.spotprice import getSpotprice
from barry_energy_api.utils import dateFormat, createHeader
from datetime import datetime, timedelta


class BarryException(Exception):
pass

class PriceZone(enum.Enum):
"""
DK_WEST = West Denmark (zip-code below 4999)
DK_EAST = East Denmark (zip-code above 5000)
FRANCE = Self explanatory
"""
DK_WEST = "DK_NORDPOOL_SPOT_DK1"
DK_EAST = "DK_NORDPOOL_SPOT_DK2"
FRANCE = "FR_EPEX_SPOT_FR"


class BarryEnergyAPI:
def __init__(self, api_token: str, pastdays: int, pricezone: PriceZone):
# Sets the API key
self.api_token = api_token
self.pricezone = pricezone.value

# Set the date range
self.todate = datetime.utcnow()
self.fromdate = dateFormat(self.todate - timedelta(days=pastdays))
self.todate = dateFormat(self.todate)

# Creates HTTP header
self.header = createHeader(self.api_token, self.fromdate, self.todate, self.pricezone)

def set_startdate(self, pastdays: int):
"""
Changes the date which measuring is happening from.
:param pastdays:
Days before today
"""
self.fromdate = dateFormat(datetime.utcnow() - timedelta(days=pastdays))

def spotprices(self, pastdays=None):
"""
Gives the spot prices in the specified from/to range.
Spot prices are delivered ahead of our current time, up to 24 hours ahead.
The price is excluding VAT and tarrifs.
"""
try:
headers = self.header
if pastdays is not None:
headers[1] = dateFormat(datetime.utcnow() - timedelta(days=pastdays))
return getSpotprice(headers)
except Exception as ex:
raise BarryException(str(ex))

def consumption(self, pastdays=None):
"""
Gives the consumption on an hourly resolution.
It accepts a past from and to date and MPID/PDL as input.
Note that it will not return any data unless one is a Barry customer.
The data usually has two to three days of delay, which is a limitation of the regulatory authorities.
"""
try:
headers = self.header
if pastdays is not None:
headers[1] = dateFormat(datetime.utcnow() - timedelta(days=pastdays))
return getConsumption(headers)
except Exception as ex:
raise BarryException(str(ex))

def meteringpoints(self, pastdays=None):
"""
Gives information on all of your metering points subscribed to Barry.
This method contains metadata on your metering points, including the price area and the MPID/PDL (Metering Point Identification).
"""
try:
headers = self.header
if pastdays is not None:
headers[1] = dateFormat(datetime.utcnow() - timedelta(days=pastdays))
return getMeteringPoints(headers)
except Exception as ex:
raise BarryException(str(ex))
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion spotprice.py → barry_energy_api/entities/spotprice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def getSpotprice(headers):
"id": 0,
"jsonrpc": "2.0",
"params": [
"DK_NORDPOOL_SPOT_DK1",
headers[3],
headers[1],
headers[2]
]
Expand Down
11 changes: 11 additions & 0 deletions barry_energy_api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def createHeader(apitoken, fromdate, todate, pricezone):
apiHeader = {
"Content-Type": "application/json",
"Authorization": "Bearer " + apitoken
}

return [apiHeader, fromdate, todate, pricezone]


def dateFormat(date):
return date.strftime("%Y-%m-%dT%H:%M:%SZ")
31 changes: 0 additions & 31 deletions main.py

This file was deleted.

27 changes: 27 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from setuptools import setup

# Thanks Frankkkkk for inspiring.
def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
name = "barry-energy-api",
version = "1.0.0",
author = "Emil Elkjær Nielsen",
author_email = "emil@evsn.dk",
description = ("Connecting to and getting data from Barry Energy's API (http://barry.energy)"),
license = "LICENS",
keywords = "python barry energy api electricity prices barry.energy",
url = "https://github.com/eelkjaer/BarryAPIClient",
packages=['barry_energy_api'],
long_description=open("README.md", "r").read(),
long_description_content_type="text/markdown",
install_requires=[],
classifiers=[
"Development Status :: 4 - Beta",
"Programming Language :: Python :: 3.9",
"Topic :: Utilities",
"License :: OSI Approved :: MIT License",
],
)
15 changes: 0 additions & 15 deletions utils.py

This file was deleted.

0 comments on commit 16a0a35

Please sign in to comment.