Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit 41ddb63

Browse files
committed
black
1 parent 9626e26 commit 41ddb63

File tree

8 files changed

+131
-98
lines changed

8 files changed

+131
-98
lines changed

docs/pge-sdk/OAuth2/Api.py

+39-27
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,43 @@
1010
import json
1111
from base64 import b64encode
1212

13+
1314
class Api:
14-
def __init__(self, cert_params_hash):
15-
self.cert = (cert_params_hash["crt"], cert_params_hash["key"])
16-
17-
#API sync request using Oauth2 access token
18-
def sync_request(self, url,subscription_id,usage_point, published_min, published_max, access_token):
19-
url = url + "/Subscription/" + subscription_id + "/UsagePoint/"+usage_point
20-
url = url + "?published-max=" +published_max+ "&published-min="+published_min
21-
header_params = {'Authorization' : 'Bearer ' + access_token}
22-
request = requests.get(url, data = {}, headers = header_params, cert = self.cert)
23-
if str(request.status_code) == "200":
24-
response = {"status": request.status_code, "data": request.text}
25-
return response
26-
response = {"status": request.status_code, "error": request.text}
27-
return response
28-
29-
30-
#API async request using Oauth2 access token
31-
def async_request(self, url, subscription_id,published_min,published_max, access_token):
32-
url = url +"/Subscription/" + subscription_id
33-
url += "?published-max=" +published_max+ "&published-min="+published_min
34-
header_params = {'Authorization' : 'Bearer ' + access_token}
35-
request = requests.get(url, data = {}, headers = header_params, cert = self.cert)
36-
if str(request.status_code) == "202":
37-
response = {"status": request.status_code, "data": request.text}
38-
return response
39-
response = {"status": request.status_code, "error": request.text}
40-
return response
15+
def __init__(self, cert_params_hash):
16+
self.cert = (cert_params_hash["crt"], cert_params_hash["key"])
17+
18+
# API sync request using Oauth2 access token
19+
def sync_request(
20+
self,
21+
url,
22+
subscription_id,
23+
usage_point,
24+
published_min,
25+
published_max,
26+
access_token,
27+
):
28+
url = url + "/Subscription/" + subscription_id + "/UsagePoint/" + usage_point
29+
url = (
30+
url + "?published-max=" + published_max + "&published-min=" + published_min
31+
)
32+
header_params = {"Authorization": "Bearer " + access_token}
33+
request = requests.get(url, data={}, headers=header_params, cert=self.cert)
34+
if str(request.status_code) == "200":
35+
response = {"status": request.status_code, "data": request.text}
36+
return response
37+
response = {"status": request.status_code, "error": request.text}
38+
return response
39+
40+
# API async request using Oauth2 access token
41+
def async_request(
42+
self, url, subscription_id, published_min, published_max, access_token
43+
):
44+
url = url + "/Subscription/" + subscription_id
45+
url += "?published-max=" + published_max + "&published-min=" + published_min
46+
header_params = {"Authorization": "Bearer " + access_token}
47+
request = requests.get(url, data={}, headers=header_params, cert=self.cert)
48+
if str(request.status_code) == "202":
49+
response = {"status": request.status_code, "data": request.text}
50+
return response
51+
response = {"status": request.status_code, "error": request.text}
52+
return response

docs/pge-sdk/OAuth2/ClientCredentials.py

+22-17
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,27 @@
1212
import json
1313
from base64 import b64encode
1414

15+
1516
class ClientCredentials:
16-
def __init__(self, client_credentials_hash, cert_params_hash):
17-
client_key = client_credentials_hash["client_key"]
18-
client_secret_key = client_credentials_hash["client_secret_key"]
19-
self.cert = (cert_params_hash["crt"], cert_params_hash["key"])
20-
self.base64code = 'Basic ' + bytes.decode(b64encode(('%s:%s' %(client_key,client_secret_key)).encode('latin1')).strip())
17+
def __init__(self, client_credentials_hash, cert_params_hash):
18+
client_key = client_credentials_hash["client_key"]
19+
client_secret_key = client_credentials_hash["client_secret_key"]
20+
self.cert = (cert_params_hash["crt"], cert_params_hash["key"])
21+
self.base64code = "Basic " + bytes.decode(
22+
b64encode(
23+
("%s:%s" % (client_key, client_secret_key)).encode("latin1")
24+
).strip()
25+
)
2126

22-
# To get client_credentials from PG&E
23-
def get_client_access_token(self, url):
24-
request_params = {'grant_type': 'client_credentials'}
25-
header_params = {'Authorization' : self.base64code}
26-
response = requests.post(url, data = request_params, headers = header_params, cert = self.cert)
27-
if str(response.status_code) == "200":
28-
res = response.json()
29-
res.update({"status": response.status_code})
30-
return res
31-
return {"status": response.status_code, "error": response.text}
32-
33-
27+
# To get client_credentials from PG&E
28+
def get_client_access_token(self, url):
29+
request_params = {"grant_type": "client_credentials"}
30+
header_params = {"Authorization": self.base64code}
31+
response = requests.post(
32+
url, data=request_params, headers=header_params, cert=self.cert
33+
)
34+
if str(response.status_code) == "200":
35+
res = response.json()
36+
res.update({"status": response.status_code})
37+
return res
38+
return {"status": response.status_code, "error": response.text}

docs/pge-sdk/OAuth2/OAuth2.py

+41-34
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,46 @@
1515
import json
1616
from base64 import b64encode
1717

18+
1819
class OAuth2:
19-
def __init__(self, client_credentials_hash, cert_params_hash):
20-
client_key = client_credentials_hash["client_key"]
21-
client_secret_key = client_credentials_hash["client_secret_key"]
22-
self.cert = (cert_params_hash["crt"], cert_params_hash["key"])
23-
self.base64code = 'Basic ' + bytes.decode(b64encode(('%s:%s' %(client_key,client_secret_key)).encode('latin1')).strip())
24-
25-
26-
#Get Acces Token
27-
def get_access_token(self, url, code, redirect_uri):
28-
request_params = {"grant_type":"authorization_code", "code": code , "redirect_uri":redirect_uri}
29-
header_params = {'Authorization' : self.base64code}
30-
request = requests.post(url, data = request_params, headers = header_params, cert = self.cert)
31-
if str(request.status_code) == "200":
32-
res = request.json()
33-
res.update({"status": request.status_code})
34-
return res
35-
response = {"status": request.status_code, "error": request.text}
36-
return response
37-
38-
39-
# Refresh token will collect back the new access token
40-
def get_refresh_token(self, url, refresh_token):
41-
request_params = {"grant_type":"refresh_token", "refresh_token": refresh_token}
42-
header_params = {"Authorization":self.base64code}
43-
request = requests.post(url, data = request_params, headers = header_params, cert = self.cert)
44-
if str(request.status_code) == "200":
45-
res = request.json()
46-
res.update({"status": request.status_code})
47-
return res
48-
response = {"status": request.status_code, "error": request.text}
49-
return response
50-
20+
def __init__(self, client_credentials_hash, cert_params_hash):
21+
client_key = client_credentials_hash["client_key"]
22+
client_secret_key = client_credentials_hash["client_secret_key"]
23+
self.cert = (cert_params_hash["crt"], cert_params_hash["key"])
24+
self.base64code = "Basic " + bytes.decode(
25+
b64encode(
26+
("%s:%s" % (client_key, client_secret_key)).encode("latin1")
27+
).strip()
28+
)
5129

52-
53-
30+
# Get Acces Token
31+
def get_access_token(self, url, code, redirect_uri):
32+
request_params = {
33+
"grant_type": "authorization_code",
34+
"code": code,
35+
"redirect_uri": redirect_uri,
36+
}
37+
header_params = {"Authorization": self.base64code}
38+
request = requests.post(
39+
url, data=request_params, headers=header_params, cert=self.cert
40+
)
41+
if str(request.status_code) == "200":
42+
res = request.json()
43+
res.update({"status": request.status_code})
44+
return res
45+
response = {"status": request.status_code, "error": request.text}
46+
return response
47+
48+
# Refresh token will collect back the new access token
49+
def get_refresh_token(self, url, refresh_token):
50+
request_params = {"grant_type": "refresh_token", "refresh_token": refresh_token}
51+
header_params = {"Authorization": self.base64code}
52+
request = requests.post(
53+
url, data=request_params, headers=header_params, cert=self.cert
54+
)
55+
if str(request.status_code) == "200":
56+
res = request.json()
57+
res.update({"status": request.status_code})
58+
return res
59+
response = {"status": request.status_code, "error": request.text}
60+
return response

docs/pge-sdk/setup.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
from setuptools import setup, find_packages
33
import os, re
44

5-
setup(name="OAuth2",
6-
version= "1.0",
7-
description="library for OAuth2",
8-
author="Bharati",
9-
packages = find_packages(),
10-
author_email="[email protected]",
11-
license = "MIT License"
12-
)
5+
setup(
6+
name="OAuth2",
7+
version="1.0",
8+
description="library for OAuth2",
9+
author="Bharati",
10+
packages=find_packages(),
11+
author_email="[email protected]",
12+
license="MIT License",
13+
)

open_energy_view/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from gevent import monkey
2+
23
monkey.patch_all()
34

45

@@ -23,7 +24,7 @@ def create_app(config_name) -> Flask:
2324
instance_relative_config=False,
2425
template_folder="./frontend/dist/",
2526
static_folder="./frontend/dist/",
26-
static_url_path=""
27+
static_url_path="",
2728
)
2829
app.config.from_object(config_name)
2930
from . import resources
@@ -65,7 +66,7 @@ def create_app(config_name) -> Flask:
6566

6667
# Task status
6768
rest.add_resource(resources.TestCelery, "/api/celery")
68-
rest.add_resource(resources.CheckTaskStatus, '/api/web/task')
69+
rest.add_resource(resources.CheckTaskStatus, "/api/web/task")
6970

7071
# Initialize extensions with the Flask app
7172
db.init_app(app)
@@ -89,11 +90,11 @@ def create_app(config_name) -> Flask:
8990
demo_user.save_to_db()
9091
except Exception as e:
9192
print(e)
92-
93+
9394
# Really need to be setting Dev/Prod flags for stuff like this
9495
# Technically this will get intercepted by Nginx in production
9596
@app.route("/")
9697
def index():
97-
return render_template('index.html')
98+
return render_template("index.html")
9899

99100
return app

open_energy_view/celery.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"imports": (
1212
"open_energy_view.celery_tasks",
1313
"open_energy_view.espi_helpers",
14-
"open_energy_view.utility_apis"
14+
"open_energy_view.utility_apis",
1515
),
1616
"task_routes": {
1717
"get_jp": {"queue": "io"},

open_energy_view/celery_tasks.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from gevent import monkey
2+
23
monkey.patch_all()
34

45
import time
@@ -42,9 +43,13 @@ def insert_espi_xml_into_db(self, xml, given_source_id=None, save=False):
4243
if given_source_id:
4344
source_id_memo[usage_point] = [given_source_id]
4445
else:
45-
sources = db.session.query(models.Source).filter_by(usage_point=usage_point)
46+
sources = db.session.query(models.Source).filter_by(
47+
usage_point=usage_point
48+
)
4649
if sources.count() == 0:
47-
print(f"could not find usage point {usage_point} in db, probably gas")
50+
print(
51+
f"could not find usage point {usage_point} in db, probably gas"
52+
)
4853
source_id_memo[usage_point] = []
4954
elif sources.count() > 1:
5055
print(f"WARNING: {usage_point} is associated with multiple sources")
@@ -83,6 +88,7 @@ def process_data(self, inc):
8388
time.sleep(15)
8489
return inc
8590

91+
8692
@celery.task
8793
def add(x, y):
8894
time.sleep(10)
@@ -125,6 +131,7 @@ def fetch_task(self, published_period_start, interval_block_url, headers, cert):
125131
sleep(1)
126132
return "done"
127133

134+
128135
@celery.task(bind=True, name="fake_fetch")
129136
def fake_fetch(self):
130137
test_xml = [

setup.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
setup(
66
name="open_energy_view",
7-
version='0.1dev',
8-
author='J.P. Hutchins',
9-
author_email='[email protected]',
7+
version="0.1dev",
8+
author="J.P. Hutchins",
9+
author_email="[email protected]",
1010
packages=setuptools.find_packages(),
11-
license='MIT',
12-
long_description=open('README.md').read()
11+
license="MIT",
12+
long_description=open("README.md").read(),
1313
)

0 commit comments

Comments
 (0)