from fastapi import FastAPI from pydantic import BaseModel from fastapi.responses import RedirectResponse, HTMLResponse import urllib.parse import requests, json, time from password_generator import generate_password import threading user_credentials_google_apps_script_url = "https://script.google.com/macros/s/AKfycbzm5dC7jMJ4y7Cdu-sW05kozA6HgcTp2zoHi7BjJQ7Hhvp0exihsllgBsiqMoa68i6-XA/exec" app_link = "https://huggingface.co/spaces/Upto12forenglish/ZidTurboApp/edit/main/main.py" def send_email_credentials(email, username, password, app_link): url = "https://hook.eu2.make.com/soremxx4ove87uk7hmghfuhvtl3gaqc6" payload = { "email": email, "username": username, "password": password, "app_link": app_link } try: response = requests.post(url, json=payload) response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code print(f"Status Code: {response.status_code}") except requests.exceptions.RequestException as e: print(f"An error occurred: {e}") if e.response is not None: print(f"Response Content: {e.response.content.decode()}") # On getting the store credentials, Get the store id and info using "https://api.zid.sa/v1/managers/account/profile" endpoint def get_store_id(access_token, authorization): print("Entered get store id function") time.sleep(0.5) # Set up the authorization header headers = { 'Authorization': 'Bearer ' + authorization, 'X-Manager-Token': access_token, } # Make the GET request to the API endpoint response = requests.get('https://api.zid.sa/v1/managers/account/profile', headers=headers) response_data = response.json() store_name = response_data['user']['name'] store_id = response_data['user']['store']['id'] store_username = response_data['user']['username'] store_manager_email = response_data['user']['email'] store_manager_mobile = response_data['user']['mobile'] # Print the JSON response print("Store name is ", store_name) print("Store id is ", store_id) print("Username is ", store_username) print("Email is ", store_manager_email) print("Phone is ", store_manager_mobile) print("Exited get store id function") return store_name, store_id, store_username, store_manager_email, store_manager_mobile # On new app installation, add the store info to the database sheet def create_new_user(access_token, authorization, refresh_token, expires_in): print("Entered create new user function") # Call get_store_id to retreive store information store_name, store_id, username, email, mobile = get_store_id(access_token, authorization) time.sleep(0.5) # Generate user password password = generate_password() print("Password was generated successfully, ", password) time.sleep(0.5) # Send the store info to google app script to register the new user information to the user database try: response = requests.get(user_credentials_google_apps_script_url, params={"accessToken": access_token, "authorization": authorization, "refreshToken":refresh_token, "expiresIn": expires_in, "storeName": store_name, "storeID": store_id, "username": username, "storeManagerEmail": email, "mobileNumber": mobile, "storeManagerpassword": password}) responseData = json.loads(response.text) print(responseData) # Put the result in the queue except Exception as e: print(str(e)) # Put the error message in the queue time.sleep(0.5) print("Now sending the credentials email to the store owner") # Send an email with user credentials # send_email(email, username, password, app_link) send_email_credentials(email, username, password, app_link) print("Exited create new user function") def process_oauth_response(response_data): try: # Extract data access_token = response_data["access_token"] token_type = response_data["token_type"] expires_in = response_data["expires_in"] authorization = response_data["authorization"] refresh_token = response_data["refresh_token"] print("Access Token: ", access_token) print("Authorization: ", authorization) print("Refresh Token: ", refresh_token) print("Expires in: ", expires_in) create_new_user(access_token, authorization, refresh_token, expires_in) except KeyError as e: print(f"Missing expected key in response data: {e}") except Exception as e: print(f"An error occurred while processing the response: {e}") app = FastAPI() class Message(BaseModel): message: str @app.get("/", response_class=HTMLResponse) async def read_root(): return """
Your ultimate assistant for effortless product management.