How can I connect Mysql to Cloud Functions?

Inside cloud functions I have this functions (entry point)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def hello_http(request):
</code>
<code>def hello_http(request): </code>
def hello_http(request):

Inside that function I have all this code
What this does Is to download spreadsheet of prices like btc_usd and then put this data frame on mysql google data base

To call this function I use http
And then on Cloud scheduler I pasted the url using POST method

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
import requests
import sqlalchemy
import mysql.connector
import pandas as pd
import numpy as np
import requests
class BitsoAPI:
def __init__(self, api_key, api_secret):
self.api_key = api_key
self.api_secret = api_secret
self.base_url = "https://api.bitso.com/v3/"
def get_daily_prices(self, book="btc_usd"):
response = requests.get('https://api.bitso.com/v3/trades/?book='+book)
json_response = response.json()
datos = json_response['payload']
df = pd.DataFrame(datos)
df['fecha'] = pd.to_datetime(df['created_at'],utc=True, format='ISO8601')
del df['created_at']
return df
def get_books(self):
'''Returns a list with names of avaiable books for book parameter from
get daily prices function'''
response = requests.get(self.base_url + 'available_books/')
json_response = response.json()
books = [book['book'] for book in json_response['payload']]
return books
def store_data_in_sql(df, table_name='ok'):
# Establish connection to MySQL database
connection = mysql.connector.connect(
host=db_host,
user=db_user,
password=db_password,
database=db_name
)
# Create MySQL cursor
cursor = connection.cursor()
# Insert the DataFrame into the MySQL table
df.to_sql(table_name, con=engine, if_exists='append', index=False)
# Commit changes and close connection
connection.commit()
connection.close()
for book in books:
bitso_api = BitsoAPI(api_key, api_secret)
df = bitso_api.get_daily_prices(book=book)
store_data_in_sql(df,table_name=book)
</code>
<code>import pandas as pd import requests import sqlalchemy import mysql.connector import pandas as pd import numpy as np import requests class BitsoAPI: def __init__(self, api_key, api_secret): self.api_key = api_key self.api_secret = api_secret self.base_url = "https://api.bitso.com/v3/" def get_daily_prices(self, book="btc_usd"): response = requests.get('https://api.bitso.com/v3/trades/?book='+book) json_response = response.json() datos = json_response['payload'] df = pd.DataFrame(datos) df['fecha'] = pd.to_datetime(df['created_at'],utc=True, format='ISO8601') del df['created_at'] return df def get_books(self): '''Returns a list with names of avaiable books for book parameter from get daily prices function''' response = requests.get(self.base_url + 'available_books/') json_response = response.json() books = [book['book'] for book in json_response['payload']] return books def store_data_in_sql(df, table_name='ok'): # Establish connection to MySQL database connection = mysql.connector.connect( host=db_host, user=db_user, password=db_password, database=db_name ) # Create MySQL cursor cursor = connection.cursor() # Insert the DataFrame into the MySQL table df.to_sql(table_name, con=engine, if_exists='append', index=False) # Commit changes and close connection connection.commit() connection.close() for book in books: bitso_api = BitsoAPI(api_key, api_secret) df = bitso_api.get_daily_prices(book=book) store_data_in_sql(df,table_name=book) </code>
import pandas as pd
import requests
import sqlalchemy
import mysql.connector
import pandas as pd
import numpy as np 
import requests




class BitsoAPI:
    def __init__(self, api_key, api_secret):
        self.api_key = api_key
        self.api_secret = api_secret
        self.base_url = "https://api.bitso.com/v3/"
    
    def get_daily_prices(self, book="btc_usd"):
        response = requests.get('https://api.bitso.com/v3/trades/?book='+book)
        json_response = response.json()
        datos = json_response['payload']
        df = pd.DataFrame(datos)
        df['fecha'] = pd.to_datetime(df['created_at'],utc=True, format='ISO8601')
        del df['created_at']
        return df 
    
    def get_books(self):
        '''Returns a list with names of avaiable books for book parameter from
        get daily prices function'''
        response = requests.get(self.base_url + 'available_books/')
        json_response = response.json()
        books = [book['book'] for book in json_response['payload']]
        return books  




def store_data_in_sql(df, table_name='ok'):
    # Establish connection to MySQL database
    connection = mysql.connector.connect(
        host=db_host,
        user=db_user,
        password=db_password,
        database=db_name
    )

    # Create MySQL cursor
    cursor = connection.cursor()

    # Insert the DataFrame into the MySQL table
    df.to_sql(table_name, con=engine, if_exists='append', index=False)

    # Commit changes and close connection
    connection.commit()
    connection.close()




for book in books: 
    bitso_api = BitsoAPI(api_key, api_secret)
    df = bitso_api.get_daily_prices(book=book)
    store_data_in_sql(df,table_name=book)

But then I got ‘Failed’ everytime I run it on Scheduler, even tho I could created the function on Cloud Functions

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật