-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
891 lines (763 loc) · 33.5 KB
/
main.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
# from MySocketIO import MySocketManger
import enum
import httpx
import pymongo
from socketio.asyncio_server import AsyncServer
from LabelProvider import DirectlyFromStallionAPI, RetrieveOnlyReadyLabel, StallionCsvFileManager, StallionLabelManager
from MyEmailService import send_verification_email
import ujson
import pprint
import ssl
from datetime import datetime, timedelta
from typing import List, Optional
import jwt
from bson.objectid import ObjectId
from fastapi import FastAPI, Depends, Body, HTTPException, status, BackgroundTasks, Query, UploadFile, File
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse, StreamingResponse
from fastapi.security import OAuth2PasswordRequestForm
from pydantic import BaseModel
from redis import ResponseError
from starlette.middleware.cors import CORSMiddleware
from starlette.requests import Request
from EtsyAPISession import EtsyAPISession
from EtsyShopManager import syncShop
from auth import AuthHandler
from database import MongoDB, MyRedis, EtsyShopConnection, ReceiptNoteStatus, UpdateEtsyShopConnection, InvitationEmail, User, \
ReceiptNote, CreateReceiptNote, UpdateReceiptNote
from oauth2 import (oauth2_schema,
is_authenticated,
verify_password)
from schemas import UserData, ReceiptStatus
from EtsyAPI import EtsyAPI, create_etsy_api_with_etsy_connection
from endpoints import assignments
from endpoints import check_same_address_same_name
from endpoints import new_search
from endpoints import shipments
from endpoints import connections_info
from MySocketManager import MySocketManager as SocketManager
# from MyScheduler import MyScheduler
from config import ENV_MODE, FRONTEND_URI, JWT_SECRET, SCHEDULED_JOB_INTERVAL, SCHEDULED_JOB_OFFSET
import tempfile
from MyLogger import Logger
logging = Logger().logging
logging.info("Logging singleton test message.")
mongodb = MongoDB()
r = MyRedis().r
# myScheduler = MyScheduler()
context = ssl.create_default_context()
email_invitation_link = FRONTEND_URI + "/#/register?email={email}&verification_code={verification_code}"
auth_handler = AuthHandler()
origins = [
FRONTEND_URI,
"https://localhost:3000"
]
print(origins)
app = FastAPI()
socketio_manager = SocketManager(app)
sm: AsyncServer = socketio_manager.get_socket_manager()
connected_users = set()
@sm.on('connect')
async def on_connect(sid, *args, **kwargs):
print('a user connected ', sid)
@sm.on('disconnect')
async def on_disconnect(sid, *args, **kwargs):
sm.leave_room(sid, room='orderaio')
ss = await sm.get_session(sid)
connected_users.remove(ss['username'])
print('a user disconnected ', sid)
@sm.on('user_connected')
async def on_user_connected(sid, data, *args, **kwargs):
print(data)
await sm.save_session(sid, {'username': data['username']})
sm.enter_room(sid, room='orderaio')
connected_users.add(data['username'])
# @sm.on('user_connect')
# async def on_user_connect(sid, data, *args, **kwargs):
# print(data)
# connected_users.add(data['username'])
# sm.enter_room(sid, 'orderaio')
# socket_manager = MySocketManger(app).socket_manager
# @app.sio.on('connection')
# async def handle_connection(sid, *args, **kwargs):
# logging.info(sid)
# print(dir(socket_manager))
# print(dir(app.sio))
@app.on_event("startup")
async def startup_event():
...
# logging.info("FastAPI startup_event")
# etsy_connections = await mongodb.db["EtsyShopConnections"].find().to_list(100)
# job_offset = 0
# if ENV_MODE != "DEV":
# logging.info("... Production Environment ...")
# myScheduler.scheduler.start()
# else:
# logging.info("This is development environment.")
# for etsy_connection in etsy_connections:
# _id = str(etsy_connection["_id"])
# myScheduler.add_job(
# _id,
# EtsyShopManager.syncShop,
# "interval",
# minutes=SCHEDULED_JOB_INTERVAL + job_offset,
# kwargs={"etsy_connection_id": _id},
# id=f"{_id}:syncShopProcess",
# name=f"{_id}:syncShopProcess",
# # jobstore="mongodb"
# )
# logging.info(f"{etsy_connection['etsy_shop_name']} has been registered to the job list.")
# job_offset += SCHEDULED_JOB_OFFSET
# # from test_delete_me import test
# # myScheduler.scheduler.add_job(test, "interval", seconds=5)
# myScheduler.scheduler.print_jobs()
@app.on_event("shutdown")
async def shutdown_event():
await mongodb.client.close()
r.close()
app.include_router(assignments.router)
app.include_router(check_same_address_same_name.router)
app.include_router(new_search.router)
app.include_router(shipments.router)
app.include_router(connections_info.router)
@app.get("/")
async def root():
# myScheduler.scheduler.print_jobs()
print(connected_users)
return {
"root": "boot",
'connected_users': connected_users
}
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def test(text):
print("TEST", text)
# class SyncShopProcess(BaseModel):
# etsy_connection_id: str
# @app.post('/apscheduler/test')
# async def apscheduler_test(etsy_connection_id: SyncShopProcess):
# etsy_connection_id = etsy_connection_id.dict().get('etsy_connection_id')
# async with httpx.AsyncClient() as client:
# res = await client.post('http://127.0.0.1:8000/apscheduler/add/syncShopProcess', json={'etsy_connection_id': etsy_connection_id})
# res_json = res.json()
# return res_json
@app.get('/receipt/label/{receipt_id}')
async def get_label_by_receipt_id(receipt_id: int, user: UserData = Depends(is_authenticated)):
# label_bytes = await StallionLabelManager.get_label_by_receipt_id(receipt_id)
# label_bytes = await DirectlyFromStallionAPI.get_label_by_receipt_id(receipt_id)
label_bytes = await RetrieveOnlyReadyLabel.get_label_by_receipt_id(receipt_id)
if label_bytes is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"There is no label associated with the given Receipt ID ({receipt_id})")
def iterfile():
temp_label_file = tempfile.NamedTemporaryFile()
temp_label_file.write(label_bytes)
logging.info(temp_label_file.name)
with open(temp_label_file.name, 'rb') as label_file:
yield from label_file
temp_label_file.close()
return StreamingResponse(iterfile(), media_type='application/pdf', headers={'content-disposition': f'attachment; filename="{receipt_id}.pdf"'})
@app.post("/uploadstallioncsvfile")
async def create_upload_file( background_tasks: BackgroundTasks, file: UploadFile = File(...), user: UserData = Depends(is_authenticated)):
logging.info(file)
logging.info(dir(file))
file_content = await file.read()
background_tasks.add_task(StallionCsvFileManager.process_csv_file, file_content)
# await StallionCsvFileManager.process_csv_file(file_content)
return {"filename": file.filename}
@app.get("/users")
async def get_all_users(user: UserData = Depends(is_authenticated)):
all_users = await mongodb.db["Users"].find(projection={"username": True, "email": True}).to_list(length=None)
return all_users
@app.post('/user/note', response_model=ReceiptNote)
async def create_note(note_data: CreateReceiptNote = Body(...), user: UserData = Depends(is_authenticated)):
logging.info(note_data)
note_data = jsonable_encoder(note_data)
insert_note_data = await mongodb.db["Notes"].insert_one({
"receipt_id": note_data["receipt_id"],
"note": note_data["note"],
"created_by": user.user,
"status": note_data["status"],
"created_at": datetime.now()
})
inserted_note_Data = await mongodb.db["Notes"].find_one({"_id": insert_note_data.inserted_id})
logging.info(inserted_note_Data)
inserted_note_Data["_id"] = str(inserted_note_Data["_id"])
return inserted_note_Data
class GetNoteBody(BaseModel):
receipt_ids: Optional[List[int]] = None
@app.post("/user/multiple_notes")
async def get_multiple_notes(body: GetNoteBody = Body(...), user: UserData = Depends(is_authenticated)):
notes = []
async for note in mongodb.db["Notes"].find({"receipt_id": {"$in":body.dict().get('receipt_ids')}}, projection={"_id": False}):
notes.append(note)
if notes is not None and len(notes) != 0:
return notes
else:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"No notes found for given receipt ids.")
@app.get("/user/note/{receipt_id}", )
async def get_note_by_receipt_id(receipt_id: str, user: UserData = Depends(is_authenticated)):
logging.info(receipt_id)
logging.info(receipt_id.split(','))
receipt_ids = [int(_) for _ in receipt_id.split(',')]
# notes = await mongodb.db["Notes"].find({"receipt_id": {"$in":receipt_ids}})
notes: List[dict] = []
async for note in mongodb.db["Notes"].find({"receipt_id": {"$in":receipt_ids}}, projection={"_id": False}):
notes.append(note)
note = notes
if note is not None and len(note) != 0:
# logging.info(f"Note is not None {note}")
# note["_id"] = str(note["_id"])
return notes
else:
logging.info("Note is None")
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"There is no note found for {receipt_id}")
@app.put("/user/note/{receipt_id}", response_model=ReceiptNote)
async def update_note(receipt_id: int, note_data: UpdateReceiptNote = Body(...),
user: UserData = Depends(is_authenticated)):
note_data = jsonable_encoder(note_data)
receipt_note = await mongodb.db["Notes"].find_one({'_id': receipt_id})
if receipt_note is not None:
try:
receipt_note['created_by']
except KeyError:
note_data['created_by'] = user.user
note_data["updated_at"] = datetime.now()
note_data["updated_by"] = user.user
logging.info(note_data)
# update_fields = {k: v for k, v in note_data.items() if v is not None}
update_note_data = await mongodb.db["Notes"].update_one({"receipt_id": receipt_id}, {
"$set": note_data
})
if update_note_data.modified_count == 1:
note = await mongodb.db["Notes"].find_one({"receipt_id": receipt_id})
note["_id"] = str(note["_id"])
return note
@app.post('/auth/logout')
async def logout(request: Request):
response = JSONResponse({"status": "logged_out"})
response.set_cookie(
oauth2_schema.token_name,
"",
expires=0,
httponly=True,
secure=True,
)
return response
@app.post('/auth/token')
async def authenticate(form_data: OAuth2PasswordRequestForm = Depends()):
"""
Verify login details and issue JWT in httpOnly cookie.
Raises:
HTTPException: 401 error if username or password are not recognised.
"""
user = await mongodb.db['Users'].find_one({"username": form_data.username})
# print(user)
# print(verify_password(form_data.password, user['password']))
if (user is None) or (not verify_password(form_data.password, user['password'])):
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail='Invalid username and/or password')
payload = {
"user": user["username"],
"user_id": user["_id"],
"scopes": user["scopes"] if user["scopes"] is not None else [""]
}
issued_at = datetime.utcnow()
expires_in = timedelta(hours=6)
expire = issued_at + expires_in
# print(expire)
payload.update({"exp": expire, "iat": issued_at, "sub": "jwt-cookies-test"})
encoded_jwt = jwt.encode(
payload,
JWT_SECRET,
algorithm="HS256"
)
response = JSONResponse({"status": "authenticated"})
response.set_cookie(
oauth2_schema.token_name,
encoded_jwt,
expires=expires_in.seconds,
httponly=True,
secure=True,
)
return response
@app.get('/auth/test')
async def auth_test(_request: Request, user: UserData = Depends(is_authenticated)):
"""Return sample JSON iff the user is authenticated.
"""
return {"status": "ok", "user": user}
# @app.post('/authenticate')
# def authenticatex(payload=Depends(auth_handler.auth_wrapper)):
# return {
# "payload": payload
# }
@app.post('/invite')
async def invite(background_tasks: BackgroundTasks, invitation_details: InvitationEmail = Body(...),
user: UserData = Depends(is_authenticated), ):
logging.info(invitation_details)
created_at = invitation_details.created_at
if "admin" not in user.scopes:
raise HTTPException(status_code=400, detail='You have no permission to use this endpoint')
# invitation_details.created_at = datetime.now()
invitation_details = jsonable_encoder(invitation_details)
check_email_result = await mongodb.db["InvitationEmails"].find_one({"email": invitation_details["email"]})
if check_email_result is not None:
if check_email_result["is_registered"]:
raise HTTPException(status_code=400,
detail=f'{invitation_details["email"]} has already been registered.')
else:
raise HTTPException(status_code=400,
detail=f'Invitation email has been already sent to {invitation_details["email"]}')
invitation_details["created_at"] = created_at
invite_email_result = await mongodb.db["InvitationEmails"].insert_one(invitation_details)
inserted_email_invitation = await mongodb.db["InvitationEmails"].find_one({
"_id": invite_email_result.inserted_id
})
logging.info(inserted_email_invitation)
# body={
# 'verification_code': inserted_email_invitation['verification_code'],
# 'register_email_url': email_invitation_link.format(
# email=inserted_email_invitation['email'],
# verification_code=inserted_email_invitation['verification_code'])}
# ms = get_mail_service()
# message = MessageSchema(
# subject="[OrderAIO] - Register - Verification Code",
# recipients=[inserted_email_invitation['email']],
# template_body=body,
# subtype='html',
# )
try:
await send_verification_email(
inserted_email_invitation['verification_code'],
inserted_email_invitation['email'],
email_invitation_link.format(
email=inserted_email_invitation['email'],
verification_code=inserted_email_invitation['verification_code']
)
)
except Exception as e:
logging.exception(e)
await mongodb.db["InvitationEmails"].delete_one({"_id": invite_email_result.inserted_id})
raise HTTPException(status_code=400,
detail=f"Something went wrong. Verification email couldn't sent.")
else:
return {**inserted_email_invitation, "status": "Waiting for verification"}
# background_tasks.add_task(
# send_verification_email,
# inserted_email_invitation['email'],
# inserted_email_invitation['verification_code'],
# email_invitation_link.format(
# email=inserted_email_invitation['email'],
# verification_code=inserted_email_invitation['verification_code'])
# )
@app.post("/register", response_model=User, response_model_exclude_defaults=True)
async def register(register_details: User = Body(...)):
created_at = register_details.created_at
register_details = jsonable_encoder(register_details)
# check if the given email is in the InvitationEmails Collection
logging.info(register_details)
email_query_result = await mongodb.db["InvitationEmails"].find_one({"email": register_details["email"]})
if email_query_result is None:
raise HTTPException(status_code=400, detail='This email has not been invited or verification code has been expired.')
if email_query_result["is_registered"]:
raise HTTPException(status_code=400, detail='This email has already been registered')
verification_code = email_query_result['verification_code']
if register_details['verification_code'] != verification_code:
raise HTTPException(status_code=400, detail='Invalid verification code')
check_username_result = await mongodb.db["Users"].find_one({"username": register_details['username']})
if check_username_result is not None:
raise HTTPException(status_code=400, detail='This username is already taken')
# hashed_password = auth_handler.get_password_hash(register_details['password'])
register_details["created_at"] = created_at
register_user_result = await mongodb.db["Users"].insert_one(register_details)
check_inserted_user = await mongodb.db["Users"].find_one({"_id": register_user_result.inserted_id})
if check_inserted_user is None:
raise HTTPException(status_code=400, detail='There was an error while registering. Please try again later')
updated_email_invitation = await mongodb.db["InvitationEmails"].update_one({"email": register_details['email']}, {
"$set": {
"is_registered": True
}
})
logging.info(f"upserted id: {updated_email_invitation.upserted_id}")
find_email_invitation = await mongodb.db['InvitationEmails'].find_one({"email": email_query_result["email"]})
logging.info(find_email_invitation)
find_email_invitation["created_at"] = find_email_invitation["created_at"].isoformat()
return JSONResponse(status_code=status.HTTP_200_OK, content=find_email_invitation)
class LoginDetails(BaseModel):
username: str
password: str
# @app.post("/login")
# async def login(auth_details: LoginDetails = Body(...)):
# auth_details = jsonable_encoder(auth_details)
# user = await mongodb.db["Users"].find_one({"username": auth_details['username']})
# if (user is None) or (not auth_handler.verify_password(auth_details['password'], user['password'])):
# raise HTTPException(status_code=401, detail='Invalid username and/or password')
# token = auth_handler.encode_token(user['_id'], user['username'], user['is_admin'])
# return {
# "token": token
# }
@app.get("/search")
async def search(request: Request,
from_date: Optional[datetime] = None,
to_date: Optional[datetime] = None,
query: Optional[str] = None,
receipt_status: Optional[ReceiptNoteStatus] = None,
shop_name: Optional[str] = None,
projection: Optional[List[str]] = Query(None),
user: UserData = Depends(is_authenticated)):
logging.info(projection)
# path = request.url.path + "?" + request.url.query
# cached = r.get(path)
# if cached is not None:
# logging.info(f"{path} is already cached.")
# res = ujson.loads(cached)
# return res
mongodb_filter = {}
collation = {}
if from_date is None and to_date is None and query is None and shop_name is None and receipt_status is None:
return {"error": "No query parameter(s) provided."}
if shop_name is not None:
mongodb_filter["shop_name"] = shop_name
if from_date is not None and to_date is not None:
mongodb_filter["max_due_date"] = {
"$lte": to_date,
"$gte": from_date
}
elif from_date is not None and to_date is None:
mongodb_filter["max_due_date"] = {
"$lte": from_date,
"$gte": from_date
}
elif to_date is not None and from_date is None:
mongodb_filter["max_due_date"] = {
"$lte": to_date,
"$gte": to_date
}
logging.info(mongodb_filter)
if query is not None:
is_int: bool = False
try:
int(query)
except ValueError:
is_int = False
else:
query = int(query)
is_int = True
if is_int:
mongodb_filter["receipt_id"] = query
else:
mongodb_filter["name"] = query
collation["locale"] = "en"
collation["strength"] = 1
proj = {"_id": False}
if projection is not None and len(projection) > 0:
for p in projection:
if p == "_id":
continue
proj[p] = True
# if note_status is not None:
# receipts = await mongodb.db['Receipts'].aggregate(
# [
# {
# "$lookup": {
# "from": "Notes",
# "localField": "receipt_id",
# "foreignField": "receipt_id",
# "as": "Note"
# }
# }, {
# "$match": {
# "Note.status": note_status
# }
# }, {
# "$unwind": "$Note"
# }
# ]
# )
logging.info(mongodb_filter)
receipts = await mongodb.db["Receipts"].find(mongodb_filter,
collation=collation if collation is not None and len(
collation.keys()) > 0 else None,
projection=proj).sort("max_due_date", pymongo.ASCENDING).to_list(10000)
if receipt_status is not None:
notes = await mongodb.db['Notes'].find({'receipt_id': { '$in': [receipt['receipt_id'] for receipt in receipts] }}).to_list(99999)
logging.info(notes)
logging.info(len(notes))
new_notes = []
for i, note in enumerate(notes):
logging.info(f"{note['receipt_id']} : {note['status']} : {receipt_status}")
if note['status'] == receipt_status:
# notes.pop(i)
new_notes.append(note['receipt_id'])
# notes_ids = [note['receipt_id'] for note in notes]
notes_ids = new_notes
logging.info(len(notes_ids))
new_receipts = []
for i, receipt in enumerate(receipts):
logging.info(f"{receipt['receipt_id']} : {receipt['receipt_id'] in notes_ids}")
if receipt['receipt_id'] in notes_ids:
# receipts.pop(i)
new_receipts.append(receipt)
receipts = new_receipts
for receipt in receipts:
try:
receipt["max_due_date"] = receipt["max_due_date"].isoformat()
receipt["min_due_date"] = receipt["min_due_date"].isoformat()
except KeyError:
continue
try:
# r.set(path, ujson.dumps(receipts), ex=1800, nx=True)
...
except ResponseError as e:
print(e)
finally:
return receipts
@app.get("/async_etsy/sync/{etsy_connection_id}")
async def sync(etsy_connection_id: str, background_tasks: BackgroundTasks, user: UserData = Depends(is_authenticated)):
is_running = r.get(f"{etsy_connection_id}:is_running")
if is_running == "True":
return {
"background-task": "already running"
}
background_tasks.add_task(
func=syncShop,
etsy_connection_id=etsy_connection_id,
db=mongodb.db,
r=r
)
return {
"background-task": "processing"
}
@app.get('/receipts/{etsy_connection_id}/{receipt_id}')
async def get_receipt_by_id(etsy_connection_id: str, receipt_id: str, user: UserData = Depends(is_authenticated)):
etsy_api = await create_etsy_api_with_etsy_connection(mongodb.db, etsy_connection_id, 1)
# shop_id = etsy_connection["etsy_shop_id"]
receipt = etsy_api.getShop_Receipt2(receipt_id)
print(receipt)
return JSONResponse(status_code=status.HTTP_202_ACCEPTED, content=receipt)
@app.get('/listings/{etsy_connection_id}/{listing_id}')
async def get_listing(etsy_connection_id: str, listing_id: int):
etsy_api = await create_etsy_api_with_etsy_connection(mongodb.db, etsy_connection_id, 1)
etsy_api.getListing(listing_id)
@app.get('/shipping/templates/{etsy_connection_id}/{shipping_template_id}')
async def get_shipping_template(etsy_connection_id: str, shipping_template_id: int):
etsy_api = await create_etsy_api_with_etsy_connection(mongodb.db, etsy_connection_id, 1)
etsy_api.getShippingTemplate(shipping_template_id)
@app.get("/shops/{etsy_connection_id}/receipts/{status}/")
async def get_receipts_by_status(etsy_connection_id: str, status: ReceiptStatus):
(etsy_connection, etsy_api) = await create_etsy_api_with_etsy_connection(mongodb.db, etsy_connection_id)
shop_id = etsy_connection["etsy_shop_id"]
etsy_api.findAllShopReceiptsByStatus(shop_id, status)
@app.get("/receipts/{etsy_connection_id}")
async def get_all_receipts_by_etsy_connection(etsy_connection_id: str,
min_created: Optional[int] = None,
max_created: Optional[int] = None,
min_last_modified: Optional[int] = None,
max_last_modified: Optional[int] = None,
was_paid: Optional[bool] = None,
was_shipped: Optional[bool] = None,
user: UserData = Depends(is_authenticated)):
# _id: ObjectId = ObjectId(etsy_connection_id)
# etsy_connection = await mongodb.db["EtsyShopConnections"].find_one({"_id": _id})
# etsy_api_session = EtsyAPISession(etsy_connection["etsy_oauth_token"], etsy_connection["etsy_oauth_token_secret"],
# client_key=etsy_connection["app_key"],
# client_secret=etsy_connection["app_secret"])
# etsy_api = EtsyAPI(etsy_api_session.get_etsy_api_session())
(etsy_connection, etsy_api) = await create_etsy_api_with_etsy_connection(mongodb.db, etsy_connection_id)
shop_id = etsy_connection["etsy_shop_id"]
all_receipts = etsy_api.findAllShopReceipts(shop_id,
min_created=min_created,
max_created=max_created,
min_last_modified=min_last_modified,
max_last_modified=max_last_modified,
was_paid=was_paid,
was_shipped=was_shipped)
# pprint.pprint(all_receipts)
return JSONResponse(status_code=status.HTTP_202_ACCEPTED, content=all_receipts)
@app.get('/search/{etsy_connection_id}')
async def searchTest(etsy_connection_id: str):
(etsy_connection, etsy_api) = await create_etsy_api_with_etsy_connection(mongodb.db, etsy_connection_id)
shop_id = etsy_connection["etsy_shop_id"]
res = etsy_api.searchAllShopReceipts(shop_id)
return JSONResponse(status_code=status.HTTP_202_ACCEPTED, content=res)
@app.get('/transactions/{receipt_id}/{etsy_connection_id}')
async def get_all_transactions_by_receipt_id(receipt_id: str, etsy_connection_id: str,
user: UserData = Depends(is_authenticated)):
etsy_api = await create_etsy_api_with_etsy_connection(mongodb.db, etsy_connection_id, 1)
all_transactions_by_receipt_id = etsy_api.findAllShop_Receipt2Transactions(receipt_id)
return JSONResponse(status_code=status.HTTP_202_ACCEPTED, content=all_transactions_by_receipt_id)
@app.get('/images/{listing_id}/{listing_image_id}/{etsy_connection_id}')
async def get_image_of_transaction(listing_id: str, listing_image_id: str, etsy_connection_id: str,
user: UserData = Depends(is_authenticated)):
etsy_api = await create_etsy_api_with_etsy_connection(mongodb.db, etsy_connection_id, 1)
images = etsy_api.getImage_Listing(listing_id, listing_image_id)
return JSONResponse(status_code=status.HTTP_202_ACCEPTED, content=images.json())
class ConnectEtsyAppDetails(BaseModel):
app_key: Optional[str]
app_secret: Optional[str]
@app.post("/connect/etsy")
async def connect_etsy_shop(app_details: ConnectEtsyAppDetails = Body(...), user: UserData = Depends(is_authenticated)):
print(app_details)
request_token_dict: dict = EtsyAPISession.get_request_token(app_details.app_key, app_details.app_secret)
app_details = jsonable_encoder(app_details)
app_details["created_at"] = datetime.utcnow()
new_etsy_connection = await mongodb.db["EtsyShopConnections"].insert_one({
"verified": False,
"app_key": app_details["app_key"],
"app_secret": app_details["app_secret"],
"request_temporary_oauth_token": request_token_dict["oauth_token"],
"request_temporary_oauth_token_secret": request_token_dict["oauth_token_secret"],
"created_at": app_details["created_at"]
})
response = {
"login_url": request_token_dict["login_url"],
"etsy_connection_documentid": str(new_etsy_connection.inserted_id)
}
return JSONResponse(status_code=status.HTTP_200_OK, content=response)
class VerifyEtsyConnection(BaseModel):
oauth_verifier: str
temp_oauth_token: str
etsy_connection_id: str
@app.put("/verify/etsy")
async def verify_request_tokens(verify_body: VerifyEtsyConnection = Body(...),
user: UserData = Depends(is_authenticated)):
etsy_connection_id: ObjectId = ObjectId(verify_body.etsy_connection_id)
etsy_connection = await mongodb.db["EtsyShopConnections"].find_one({"_id": etsy_connection_id})
if etsy_connection is None or etsy_connection["verified"]:
raise HTTPException(status_code=400, detail="Etsy connection id not found or it has already been connected.")
access_token_dict: dict = EtsyAPISession.get_access_token(
resource_owner_key=verify_body.temp_oauth_token,
resource_owner_secret=etsy_connection['request_temporary_oauth_token_secret'],
verifier=verify_body.oauth_verifier.split("#")[0],
client_key=etsy_connection['app_key'],
client_secret=etsy_connection['app_secret']
)
if len(access_token_dict.keys()) == 0:
await mongodb.db["EtsyShopConnections"].delete_one({"_id": etsy_connection_id})
raise HTTPException(status_code=400, detail="Request token has expired.")
update_etsy_connection_result = await mongodb.db["EtsyShopConnections"].update_one({"_id": etsy_connection_id}, {
"$set": {
"etsy_oauth_token": access_token_dict["oauth_token"],
"etsy_oauth_token_secret": access_token_dict["oauth_token_secret"],
"verified": True,
"updated_at": datetime.utcnow()
}
})
if update_etsy_connection_result.modified_count == 1:
etsy_api_session = EtsyAPISession(access_token_dict["oauth_token"],
access_token_dict["oauth_token_secret"],
client_key=etsy_connection["app_key"],
client_secret=etsy_connection["app_secret"])
etsy_api = EtsyAPI(etsy_api_session.get_etsy_api_session())
all_shops = etsy_api.findAllUserShops()
shop_id = None
shop_name = None
shop_url = None
shop_banner_url = None
shop_icon_url = None
all_shops_json = all_shops.json()
if all_shops_json['count'] != 0:
shop_id = all_shops_json['results'][0]['shop_id']
shop_name = all_shops_json['results'][0]['shop_name']
shop_url = all_shops_json['results'][0]['url']
shop_banner_url = all_shops_json['results'][0]['image_url_760x100']
shop_icon_url = all_shops_json['results'][0]['icon_url_fullxfull']
user = etsy_api.getUser()
etsy_owner_email = None
if user.json()['count'] != 0:
etsy_owner_email = user.json()['results'][0]['primary_email']
update_etsy_connection_shop_details_result = await mongodb.db["EtsyShopConnections"].update_one(
{"_id": etsy_connection_id}, {
"$set": {
"etsy_owner_email": etsy_owner_email,
"etsy_shop_id": shop_id,
"etsy_shop_name": shop_name,
"shop_url": shop_url,
"shop_icon_url": shop_icon_url,
"shop_banner_url": shop_banner_url,
"updated_at": datetime.utcnow()
}
})
if update_etsy_connection_shop_details_result.modified_count == 1:
if shop_id is not None and shop_name is not None:
return JSONResponse(
status_code=status.HTTP_200_OK,
content={
"detail": f"{shop_name}:{shop_id} Successfully integrated into OrderAIO.\n({etsy_owner_email})"
}
)
else:
return JSONResponse(
status_code=status.HTTP_200_OK,
content={
"detail": f"No Etsy Shop associated with {etsy_owner_email.upper()} Found."
}
)
else:
return JSONResponse(
status_code=status.HTTP_200_OK,
content={
"detail": f"Etsy api tokens successfully fetched but couldn't update your shop_name, shop_id and email etc."
}
)
else:
raise HTTPException(status_code=400, detail="Something went wrong! while getting getting your etsy api tokens.")
@app.get("/connections/etsy", response_model=List[EtsyShopConnection])
async def get_all_etsy_connections(user: UserData = Depends(is_authenticated)):
etsy_connections = await mongodb.db["EtsyShopConnections"].find(projection={
"app_key": False,
"app_secret": False,
"etsy_oauth_token": False,
"etsy_oauth_token_secret": False,
"request_temporary_oauth_token": False,
"request_temporary_oauth_token_secret": False
}).to_list(100)
pprint.pprint(etsy_connections)
return etsy_connections
@app.get("/connection/etsy/{etsy_connection_id}", response_model=EtsyShopConnection)
async def get_etsy_connection(etsy_connection_id: str, user: UserData = Depends(is_authenticated)):
_id = ObjectId(etsy_connection_id)
etsy_connection = await mongodb.db.EtsyShopConnections.find_one(filter={
"_id": _id
}, projection={
"app_key": False,
"app_secret": False,
"etsy_oauth_token": False,
"etsy_oauth_token_secret": False,
"request_temporary_oauth_token": False,
"request_temporary_oauth_token_secret": False
})
if etsy_connection is not None:
return etsy_connection
raise HTTPException(status_code=404, detail=f"EtsyShopConnection {_id} not found")
@app.put("/connection/etsy/{id}", response_model=EtsyShopConnection)
async def update_etsy_connection(id: str, etsy_connection_fields: UpdateEtsyShopConnection = Body(...),
user: UserData = Depends(is_authenticated)):
id = ObjectId(id)
etsy_connection = {k: v for k, v in etsy_connection_fields.dict().items() if v is not None}
if len(etsy_connection) >= 1:
update_result = await mongodb.db["EtsyShopConnections"].update_one({
"_id": id
}, {
"$set": etsy_connection
})
if update_result.modified_count == 1:
updated_etsy_connection = await mongodb.db["EtsyShopConnections"].find_one({"_id": id})
if updated_etsy_connection is not None:
return updated_etsy_connection
existing_etsy_connection = await mongodb.db["EtsyShopConnections"].find_one({"_id": id})
if existing_etsy_connection is not None:
return existing_etsy_connection
raise HTTPException(status_code=404, detail=f"EtsyShopConnection {id} not found")
@app.delete("/connection/etsy/{id}")
async def delete_etsy_connection(id: str, user: UserData = Depends(is_authenticated)):
id = ObjectId(id)
delete_result = await mongodb.db["EtsyShopConnections"].delete_one({
"_id": id
})
if delete_result.deleted_count == 1:
return JSONResponse(status_code=status.HTTP_204_NO_CONTENT)
raise HTTPException(status_code=404, detail=f"EtsyShopConnection {id} not found")