如何从另一个python文件调用def python

时间:2020-04-28 22:25:47

标签: python api flask

嗨,我有Python脚本pruebas.py,想从另一个实际上是Flask(API)的python脚本中调用。声音有线,但为避免CORS问题,它在后端运行(在可行的地方),我可以从外部API检索。

这是从externa API检索数据的文件代码:

import json, hmac, hashlib, time, requests, base64
from requests.auth import AuthBase

API_KEY = "xxxxxxxxxxxxxx"
API_SECRET = "xxxxxxxxxxxxxxxxxxxx"
API_PASS = "xxxxxxxxxxxxxxxxxxxxxxxxxx"

# Create custom authentication for Exchange
class CoinbaseExchangeAuth(AuthBase):
    def __init__(self, api_key, secret_key, passphrase):
        self.api_key = api_key
        self.secret_key = secret_key
        self.passphrase = passphrase

    def __call__(self, request):
        timestamp = str(time.time())
        message = timestamp + request.method + request.path_url + (request.body or b'').decode()
        hmac_key = base64.b64decode(self.secret_key)
        signature = hmac.new(hmac_key, message.encode(), hashlib.sha256)
        signature_b64 = base64.b64encode(signature.digest()).decode()

        request.headers.update({
            'CB-ACCESS-SIGN': signature_b64,
            'CB-ACCESS-TIMESTAMP': timestamp,
            'CB-ACCESS-KEY': self.api_key,
            'CB-ACCESS-PASSPHRASE': self.passphrase,
            'Content-Type': 'application/json'
        })
        return request

api_url = 'https://api.pro.coinbase.com/'

auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS)

r = requests.get(api_url + 'accounts/', auth=auth)
#print(json.dumps(r.json(), indent=2, sort_keys=True))

烧瓶中的代码:

#import sumTwoNumbers
import pruebas
from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

#class sumNumbers(Resource):
#    def get(self, first_number, second_number):
#        return {'data': sumTwoNumbers.sumTwo(first_number,second_number)}

class gdd(Resource):
    def get(self):
        return {'data': gdd.requests.get(api_url + 'accounts/', auth=auth)}

api.add_resource(gdd, '/gdd/')

if __name__ == '__main__':
     app.run(debug=True, host= '0.0.0.0')

感谢您的帮助,对不起,我不是开发人员。

目标是调用我的flask API,该API将从外部API中检索信息。

我收到此错误:

[28/Apr/2020 22:28:50] "GET /gdd/ HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/developer/venv/lib/python3.6/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/developer/venv/lib/python3.6/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/developer/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 272, in error_router
    return original_handler(e)
  File "/home/developer/venv/lib/python3.6/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/developer/venv/lib/python3.6/site-packages/flask/_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/home/developer/venv/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/developer/venv/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/developer/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 272, in error_router
    return original_handler(e)
  File "/home/developer/venv/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/developer/venv/lib/python3.6/site-packages/flask/_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/home/developer/venv/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/developer/venv/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/developer/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 468, in wrapper
    resp = resource(*args, **kwargs)
  File "/home/developer/venv/lib/python3.6/site-packages/flask/views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/developer/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 583, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/home/developer/venv/Scripts/flask/app.py", line 15, in get
    return {'data': gdd.requests.get(api_url + 'accounts/', auth=auth)}
AttributeError: type object 'gdd' has no attribute 'requests'

1 个答案:

答案 0 :(得分:0)

您似乎想要的是:

替换

gdd.requests.get(api_url + 'accounts/', auth=auth)

使用:

import requests    

requests.get(api_url + 'accounts/', auth=auth)

文档

也许您可能还没有参考过Flask-Restful或不要求文档,所以我将其链接粘贴到此处,并鼓励您进行查看。

资源类别https://flask-restful.readthedocs.io/en/latest/api.html#flask_restful.Resource

请求https://requests.readthedocs.io/en/master/