This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshops.py
57 lines (49 loc) · 1.87 KB
/
shops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
import psycopg2
import requests
class Shop(object):
def __init__(self, id, hostname):
self.id = id
self.hostname = hostname
class Shops(object):
def __init__(self):
pass
def get_shop(self, id):
return None
class PostgresShops(Shops):
def __init__(self, database_url):
self.database_url = database_url
def create_schema(self):
with psycopg2.connect(self.database_url) as conn:
with conn.cursor() as curs:
curs.execute("""CREATE TABLE IF NOT EXISTS SHOPS (
ID varchar(255) UNIQUE NOT NULL,
HOSTNAME varchar(255) NOT NULL
)""")
def create_or_update_shop(self, shop):
print("Create/update shop %s (%s)" % (shop.hostname, shop.id))
sql = ''
if self.get_shop(shop.id):
sql = "UPDATE SHOPS SET HOSTNAME = %s WHERE ID=%s"
else:
sql = "INSERT INTO SHOPS (HOSTNAME, ID) VALUES(%s, %s)"
with psycopg2.connect(self.database_url) as conn:
with conn.cursor() as curs:
curs.execute(sql, (shop.hostname, shop.id))
def get_shop(self, id):
with psycopg2.connect(self.database_url) as conn:
with conn.cursor() as curs:
curs.execute("SELECT * FROM SHOPS WHERE ID=%s", (id,))
entry = curs.fetchone()
if entry:
return Shop(id=entry[0],
hostname=entry[1])
return None
def get_shop_id(installation):
return \
requests.get('%s/shop-id' % installation.api_url, \
headers={
"Accept": "application/hal+json",
"Authorization": "Bearer %s" % installation.access_token
}).json() \
.get('shopId', None)