【发布时间】:2020-09-23 07:44:20
【问题描述】:
开始使用我的 Python Keitaro Admin API 库。我需要请求几个目标。像优惠、活动、流媒体或附属网络。他们有相似的请求 url,例如:
-
https://example.com/v1/admin_api/offers如果我需要与优惠互动 -
https://example.com/v1/admin_api/campaigns如果我需要与广告系列互动 等等。
我想在 python 代码中与 API 交互,如下所示: 使用类变量 url 和 api_key 创建类似对象 Keitaro。以及 get 和 post 等 keitaro 方法。然后将变量 'target' 添加到子类,这样我就可以使用报价或活动的 'target' 变量调用 Keitaro get/post 方法。
所需的代码模式:
from keitaropy import Keitaro
app = Keitaro('https://example.com/', 'api_key')
offer_of_app = app.offer.get(123) # get by id
offers_of_app = app.offer.get() # get all
app2 = Keitaro('https://otherurl.com/', 'other_api_key')
offer_of_app2 = app2.offer.get(11)
offers_of_app2 = app2.offer.get()
为什么我认为这段代码 sn-p 比下面的代码 sn-p 更好:我不需要导入像 Offer、Streams、Campaigns 这样的子类。我可以导入 Keitaro 类并通过调用这些类作为上述方法 offer/campaign/stream 来使用子对象的目标变量。
我取得了什么成就:
from keitaropy import Offer
offer_app = Offer('https://example.com/', 'api_key')
offer = offer.get(123)
offers = offer.get()
keitaropy 代码:
import requests
import json
def add_target_path(base_url, target, separator = '/'):
if base_url.endswith(separator):
url = base_url + target
else:
url = base_url + separator + target
return url
class Keitaro:
def __init__(self, base_url, api_key, target):
self.headers = { 'Api-Key': api_key }
self.base_url = base_url
self.target = target
def get(self, target_id = None):
url = add_target_path(self.base_url, self.target)
if target_id:
# get by id
url = add_target_path(url, target_id)
# if no id get all
response = requests.get(url, headers=self.headers)
return response.json()
def post(self, data):
pass
class Offer(Keitaro):
def __init__(self, base_url, api_key):
self.target = 'offers'
super().__init__(base_url, api_key, self.target)
class Campaign(Keitaro):
def __init__(self, base_url, api_key):
self.target = 'campaigns'
super().__init__(base_url, api_key, self.target)
class Stream(Keitaro):
def __init__(self, base_url, api_key):
self.target = 'streams'
super().__init__(base_url, api_key, self.target)
class AffNetwork(Keitaro):
def __init__(self, base_url, api_key):
self.target = 'affiliate_networks'
super().__init__(base_url, api_key, self.target)
我不喜欢将子变量“target”存储在父类中只是为了调用父方法 get 的想法,但没有任何想法可以更好地做到这一点。如果你有什么想法请和我分享
此外,子类 Campaign、Offer 等也可以有自己的方法,这些方法不是父类中固有的。
【问题讨论】:
-
如果有任何示例如何实现第一个代码 sn-p 仍然会很好!所以我可以将父属性用于多个优惠,例如
-
在这个问题中添加了更多细节stackoverflow.com/questions/64026236/…
标签: python python-3.x oop python-requests