在 Python 中构建 API 调用 [英] Structuring API Calls in Python

查看:32
本文介绍了在 Python 中构建 API 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难确定构建数据以调用包含比特币价格的各种 API 的最佳做法是什么.我希望能够调用多个 API,而没有重复的代码.

I'm having difficulty in determining what the best practices are for structuring data for calling various APIs that contain bitcoin prices. I want to be able to call multiple APIs, without duplicate code.

我最初的想法是为我将要调用的每个 API 构建类,并将它们的属性(api_id、url 和 json_tree(我想从中提取数据的 json 路径)提供给 BtcAPI 类,然后吐出他们出去.

My initial thought was to build classes for each of the APIs I would be calling, and feed their attributes (api_id, url, and json_tree (json path I'd like to pull data from) to the BtcAPI class, and spit them out.

*注意,在阅读之前,虽然 BtcAPI 可以工作,但 Coindesk/Bitstamp 类还没有与该类交互.我想问一下,在我遇到麻烦之前我应该​​怎么做...*

*Note, before reading that while BtcAPI works, the Coindesk/Bitstamp classes do not yet interact with that class. I want to ask about the way I should do this before I go to the trouble...*

现在,我想知道我是否应该将它们列出来,例如:

Now, I'm wondering if I shouldn't make them lists, like:

coindesk = ['http://www.something.com', 'coindesk', '["time"]["updated"]']

...然后遍历它们中的每一个.或dicts,或任何其他东西.此处指示的是什么数据结构?

...and just iterate through each of them. Or dicts, or any variety of other thing. What data structure is indicated here?

我基本上是在寻找一些代码审查(由于此代码不起作用,我不想将其发送到代码审查堆栈)和对最佳实践的理解:告诉我你认为我在哪里搞砸了,我能做些什么来更好地构建这些数据?我是一个 python 和 oop noob.我可以在程序上做到这一点,但这将是丑陋和多余的.我想我使用的类也有点错误.见解?帮助?谢谢!

I am basically looking for a bit of a code review (since this code isn't working, I don't want to send it to the code review stack) and an understanding of best practices: tell me where you think I'm screwing up horribly, and what I can do to structure this data better? I'm a python and oop noob. I could do this procedurally, but it would be ugly and superfluous. I imagine I'm using classes a bit wrong, too. Insights? Help? Thank you!

谢谢!

import json
import urllib.request

#The BtcAPI class works well when you feed it static variables. It returns json.

class BtcAPI:

    def __init__(self, url, api_id):
        self.url = url
        self.api_id = api_id

    def btc_api_call(self):

        hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'     }
        req = urllib.request.Request(url, headers=hdr)
        readdata = urllib.request.urlopen(req)
        json_data = readdata.read()

        json_dict = json.loads(json_data)
        return(json_dict)

class Coindesk:


    api_id = 'Coindesk'
    url = 'https://api.coindesk.com/v1/bpi/currentprice.json'               
    json_tree = json_dict['time']['updated']

    def __init__(self):

        self.current_us_price = current_us_price

class Bitstamp:


    api_id = 'Bitstamp'
    url = 'https://www.bitstamp.net/api/ticker/'               
    json_tree = json_dict['last']

    def __init__(self):

        self.current_us_price = current_us_price

coindesk_url = Coindesk()
coindeskoutput = coindesk_url.url
print(coindeskoutput)

推荐答案

如果您想要一段通用代码,我建议您将代码和配置数据分离为 2 个文件.通过这种方式,您可以管理配置数据(URL、要检索的 JSON 属性),而无需修改实际的 Python 代码.这通常被认为是一种很好的做法,但它意味着管理两个文件而不是一个文件,因此如果您的项目非常小,这可能会有点负担.

If you want a generic piece of code, I would suggest that you dissociate your code and your configuration data into 2 files. This way you can manage your configuration data (URL, JSON attributes you want to retrieve) without having to modify your actual Python code. This is usually considered a good practice but it implies to manage two files instead of one so it can be a bit of a burden if you are on a very small project.

以您的情况为例,您可以:

Example in your case, you can have:

  • 配置文件
  • bitcoin_api.py

conf.ini

如下所示的配置文件:

[COINDESK]
url: https://api.coindesk.com/v1/bpi/currentprice.json
response: time.updated

[BITSTAMP]
url: https://www.bitstamp.net/api/ticker
response: last

bitcoin_api.py

您的代码如下所示:

import configparser
import requests
import os


class BitcoinAPI:
    def __init__(self, API):
        config = configparser.ConfigParser()
        config.read(os.path.dirname(__file__) + '/conf.ini')
        self.url = config.get(API, 'url')
        self.attribute = config.get(API, 'response')
        self.header = {'content-type': 'application/json'}

    def get(self):
        response = requests.get(self.url, headers=self.header)
        response = response.json()

        # Browse the response to fetch only the attributes you want
        self.attribute = self.attribute.split('.')
        depth = len(self.attribute)
        for i in range(depth):
            response = response[self.attribute[i]]

        print(response)
        return response

然后你可以在你的主脚本中调用你的类:

Then you can call you class in your main script:

import bitcoin_api
result = bitcoin_api.BitcoinAPI('COINDESK').get()
result = bitcoin_api.BitcoinAPI('BITSTAMP').get()

这篇关于在 Python 中构建 API 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆