-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
170 lines (151 loc) · 5.55 KB
/
Copy pathmain.py
File metadata and controls
170 lines (151 loc) · 5.55 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from fastapi import FastAPI
import requests
import pymysql
import json
app = FastAPI()
@app.get('/')
def mainhome():
main={'Main Home' : 'For procedure go to /docs'}
return main
@app.get("/suitecrm")
async def get_leads():
"""
This Get request obtain access to SuiteCrm and obtain the Session ID, and achieve to fetch the fields obtained and filtered by Phone Work, First name and Last name
"""
# OBTAINING THE SESSION ID
session = requests.Session()
# Send a login request to the SuiteCRM API to obtain a session ID
url = 'https://suitecrmdemo.dtbc.eu/service/v4/rest.php'
payload = {'method': 'login', 'input_type': 'JSON', 'response_type': 'JSON', 'rest_data': json.dumps({'user_auth': {'user_name': 'Demo', 'password': 'f0258b6685684c113bad94d91b8fa02a'}, 'application_name': 'RestTest'})}
response = session.post(url, data=payload)
# Check if the login was successful
response_json = response.json()
if 'id' not in response_json:
print('Failed to log in to SuiteCRM API')
session_id = response_json['id']
# Using id session to fetch the fields.
url = 'https://suitecrmdemo.dtbc.eu/service/v4/rest.php'
payload = {
'method': 'get_entry_list',
'input_type': 'JSON',
'response_type': 'JSON',
'rest_data': json.dumps({
'session': session_id,
'module_name': 'Leads',
'query': '',
'order_by': '',
'offset': '',
'select_fields': ['phone_work', 'first_name', 'last_name'],
'link_name_to_fields_array': [],
'max_results': '',
'deleted': ''
})
}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post(url, data=payload, headers=headers)
theleads = response.json()['entry_list']
return theleads
#SETTING UP THE DATABASE AND TABLE
@app.post("/createdb")
def create_database():
"""
This endpoint creates a database called 'leads_db' in Sql.
"""
conn = pymysql.connect(
host='localhost',
user='root',
password='yourdbpassword'
)
cur = conn.cursor()
cur.execute('CREATE DATABASE IF NOT EXISTS leads_db')
conn.commit()
cur.close()
conn.close()
@app.post('/createtable')
def create_table():
"""
This endpoint creates a table needed to initiate the insertion of data. it's configured to access as a root user.
"""
conn = pymysql.connect(
host='localhost',
user='root',
password='yourdbpassword',
db='leads_db'
)
cur = conn.cursor()
cur.execute('''
CREATE TABLE IF NOT EXISTS leads (
id INT NOT NULL AUTO_INCREMENT,
phone_work VARCHAR(255),
first_name VARCHAR(255),
last_name VARCHAR(255),
PRIMARY KEY (id)
)
''')
conn.commit()
cur.close()
conn.close()
#STORING LEADS DATA ON MYSQL. it uses the data returned in the function "get_leads".
@app.post('/storeleads')
def store_leads():
"""
This endpoint will store the leads into the leads table in the Database
"""
session = requests.Session()
# Send a login request to the SuiteCRM API to obtain a session ID
url = 'https://suitecrmdemo.dtbc.eu/service/v4/rest.php'
payload = {'method': 'login', 'input_type': 'JSON', 'response_type': 'JSON', 'rest_data': json.dumps({'user_auth': {'user_name': 'Demo', 'password': 'f0258b6685684c113bad94d91b8fa02a'}, 'application_name': 'RestTest'})}
response = session.post(url, data=payload)
# Check if the login was successful
response_json = response.json()
if 'id' not in response_json:
print('Failed to log in to SuiteCRM API')
session_id = response_json['id']
# Using id session to fetch the fields.
url = 'https://suitecrmdemo.dtbc.eu/service/v4/rest.php'
payload = {
'method': 'get_entry_list',
'input_type': 'JSON',
'response_type': 'JSON',
'rest_data': json.dumps({
'session': session_id,
'module_name': 'Leads',
'query': '',
'order_by': '',
'offset': '',
'select_fields': ['phone_work', 'first_name', 'last_name'],
'link_name_to_fields_array': [],
'max_results': '',
'deleted': ''
})
}
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.post(url, data=payload, headers=headers)
theleads = response.json()['entry_list']
#From here , leads go to the database
conn = pymysql.connect(
host='localhost',
user='root',
password='yourdbpassword',
db='leads_db'
)
cur = conn.cursor()
for lead in theleads:
phone_work = lead['name_value_list']['phone_work']['value']
first_name = lead['name_value_list']['first_name']['value']
last_name = lead['name_value_list']['last_name']['value']
cur.execute(f"INSERT INTO leads (phone_work, first_name, last_name) VALUES ('{phone_work}', '{first_name}', '{last_name}')")
conn.commit()
cur.close()
conn.close()
#Integrate Bitcoin-USD prices API into FastAPI
# With this new endpoint in FastAPI , it returns the current bitcoin-usd price
@app.get('/btc-usd-price')
def get_btc_usd_price():
"""
This endpoint is working with a remote api , it retrieves the actual value of a Bitcoin
"""
url = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd'
response = requests.get(url)
btc_usd_price = response.json()['bitcoin']['usd']
return {'btc_usd_price': btc_usd_price}