Implementing a parent method call as a child method with its own attributes

偶尔善良 提交于 2020-12-15 06:06:04

问题


I'm currently working on my Python Keitaro Admin API request-library which I called keitaropy. Keitaro Admin API docs

Keitaro install on domain and have path to api, I'll use https://example.com/admin_api/v1 in this question. To get a response from the API I need to send 2 types of requests: GET, POST

I want to work with Affiliate Networks, Campaigns, Streams, Offers and Traffic Sources. I'll call them targets. Every target has different path in url like: to get all campaigns I need to send GET request to https://example.com/admin_api/v1/campaigns

  1. GET methods realization:

All targets will have same functionality and only 1 thing will be different - target path in url

I want to work with the library like this:


    from keitaropy import Keitaro
    
    # Keitaro installed on https://example.com/admin_api/v1 with 'api_key'
    app = Keitaro('https://example.com/admin_api/v1', 'api_key')
    offer = app.offer.get(123) # get by id
    offer2 = app.offer.get(46)
    campaign = app.campaign.get(1)
    offers = app.offer.get() # get all
    campaigns = app.campaign.get()

    # Keitaro installed on https://otherurl.com/admin_api/v1 with 'other_api_key'
    app2 = Keitaro('https://otherurl.com/admin_api/v1', 'other_api_key')
    other_offer = app2.offer.get(1)
    traffic_source = app2.source.get(888)
    offers_of_app2 = app2.offer.get()

This solution will allow users in future user different keitaro apps and their methods in one python module

What I achieve:

app.py

    from keitaropy import Offer
    
    offer = Offer(url, api_key)
    africasino = offer.get('187')

keitaropy package

    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()

    
    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)
    
    
    class AffNetwork(Keitaro):
        def __init__(self, base_url, api_key):
            self.target = 'affiliate_networks'
            super().__init__(base_url, api_key, self.target)

Question: how to call parent get() method with target attribute of child class without storing child attribute target in parent (like in first code snippet above)

  1. POST requests realization

Request pattern is the same for all target only request body is different

What I achieve:

app.py

from keitaropy import AffNetwork

app = AffNetwork('https://example.com/admin_api/v1', 'SADF765sdfASD8gfffsdklhjfg56hhvfsafw34')
body = {
    "name": 'DGB',
    "offer_param": 'sub={subid}',
    "postback_url": 'http://example.com/75tf326/postback?subid={sub}&status=sale&payout={sum}&currency=USD&from=DGB'
}
created_affn = app.post(data)

method post() of parent class Keitaro

def post(self, data):
        url = add_target_path(self.base_url, self.target)
        if data:
            response = requests.post(url, data=data,
                headers=self.headers)
        return response.json()

Question: how to set required and expected keys in data before parent method post() call

来源:https://stackoverflow.com/questions/64026236/implementing-a-parent-method-call-as-a-child-method-with-its-own-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!