# Code Blocks

By [TriBridge](https://paragraph.com/@tribridge) · 2022-10-29

---

python codes
------------

### reflection method:

    # -*- coding: UTF-8 -*-
    import requests
            
    
    class TP_handler:
        def __init__(self, token, timeout=500):
            self.__header = {
               'APIToken': token,
               'Content-Type': 'application/json;charset=UTF-8'
            }
            self.__api = {
                'cn': 'https://api.xxxxxxxxx.cn/',
                'us': 'https://api.xxxxxxxxx.com/'
            }
            self.route = 'cn'
            self.timeout = timeout
            self.__funcs = {
                "download_order": ('OrderNotification', 'GET'),
                "update_order": ('OrderNotification', 'PUSH'),
                "create_order": ('OrderNotification', 'POST')
            }
        
        def add_api(self, func):
            self.__funcs.update(func)
        
        def __get_api(self, route, interface):
            api = self.__api.get(route, None)
            if api is None:
                raise Exception('Unknown Route: {}'.format(route))
            return api + interface
            
        def __getattr__(self, attr):
            def func(*args, **kwargs):
                print(attr, args, kwargs)
                funcs = self.__funcs.get(attr, None)
                if funcs is None:
                    print('Unknown Method')
                else:
                    interface = funcs[0]
                    route = kwargs.get('route', self.route)
                    api = self.__get_api(route, interface)
                    headers = kwargs.get('headers', self.__header)
                    timeout = kwargs.get('timeout', self.timeout)
                    data = kwargs.get('data', None)
                    params = kwargs.get('params', None)
                    res = requests.request(method=funcs[1], url=api, headers=headers, timeout=timeout, data=data, params=params)
                    res.encoding = 'utf-8'
                    return res.json()
            return func
      
            
    if __name__ == '__main__':
        m = TP_handler('e7d82e9-a837')
        params = {
            'OrderId': '113-8302-60798',
            'PageSize': 10,
            'PageNumber': 1
        }
        n = m.download_order(params=params)
        print(n)

---

*Originally published on [TriBridge](https://paragraph.com/@tribridge/code-blocks)*
