-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnowflake_query_handler.py
More file actions
55 lines (40 loc) · 1.8 KB
/
snowflake_query_handler.py
File metadata and controls
55 lines (40 loc) · 1.8 KB
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
import snowflake.connector
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric import dsa
from cryptography.hazmat.primitives import serialization
class snowflake_query_handler():
def __init__(self, snowflake_account, snowflake_warehouse, snowflake_user, snowflake_user_role, snowflake_user_key, snowflake_user_key_phrase):
self.snowflake_account = snowflake_account
self.snowflake_warehouse = snowflake_warehouse
self.snowflake_user = snowflake_user
self.snowflake_user_role = snowflake_user_role
self.snowflake_user_key = snowflake_user_key.encode()
self.snowflake_user_key_phrase = snowflake_user_key_phrase.encode()
def connect(self):
p_key= serialization.load_pem_private_key(
data=self.snowflake_user_key,
password=self.snowflake_user_key_phrase,
backend=default_backend()
)
pkb = p_key.private_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption())
ctx = snowflake.connector.connect(
user= self.snowflake_user,
account=self.snowflake_account,
private_key=pkb,
warehouse=self.snowflake_warehouse,
role=self.snowflake_user_role
)
cs = ctx.cursor(snowflake.connector.DictCursor)
return cs
def execute_query(self, query):
con = self.connect()
try:
return con.execute(query).fetchall()
except NameError:
print(NameError)
finally:
con.close()