-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_users.py
More file actions
58 lines (43 loc) · 1.33 KB
/
sync_users.py
File metadata and controls
58 lines (43 loc) · 1.33 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
56
57
58
import sys
import sqlite3
import requests
from contextlib import closing
from dataclasses import dataclass
FILE = "users.db"
@dataclass
class User:
id: int
first_name: str
last_name: str
address: str
email: str
def export_users_to_salesforce(db, instance_url, access_token):
with closing(db.cursor()) as cursor:
cursor.execute("SELECT id, first_name, last_name, address, email FROM users")
for row in cursor:
send_user_to_salesforce(User(*row), instance_url, access_token)
def send_user_to_salesforce(user, instance_url, access_token):
url = f"{instance_url}/services/data/v52.0/sobjects/User"
headers = {"Authorization": f"Bearer {access_token}"}
data = {
"FirstName": user.first_name,
"LastName": user.last_name,
"Address": user.address,
"Email": user.email,
}
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response
if __name__ == "__main__":
if len(sys.argv) != 3:
print(f"error: {sys.argv[0]} <URL> <TOKEN>")
exit(1)
_, instance_url, access_token = sys.argv
db = sqlite3.connect(FILE)
try:
with db:
export_users_to_salesforce(db, instance_url, access_token)
except KeyboardInterrupt:
pass
finally:
db.close()