如何在python中连接Riot API

时间:2019-06-19 02:59:13

标签: python api riot

我试图连接到Riot api以访问召唤者信息。我写了3个文件:

  1. “ RiotConsts”:设置一些常量的文件,例如“ URL”,“ api_version”,“ region”
  2. “ RiotAPI”:主要功能
  3. “ riot_main”:调用我想要的信息。

我收到一个错误:“ RiotAPI”对象没有属性“ _request””。

class RiotAPI(object):
    def __init__(self,api_key,region=Consts.REGIONS['europe_nordic_and_east']):
        self.api_key = api_key
        self.region = region

    def request(self, api_key, params={}):
        args = {'api_key': self.api_key}
        for k,v in params.items():
            if k not in args:
                args[k] = v
        response = requests.get(
            Consts.URL['base'].format(
                proxy = self.region,
                region = self.region,
                url = api_url
                ),
            params=args
            )
        print (response.url)
        return response.json()

    def get_summoner_by_name(self, name):
        api_url = Consts.URL['summoner_by_name'].format(
            version=Consts.API_VERSIONS['summoner'],
            summonerName=name
            )
        return self._request(api_url)

我希望收到召唤者信息,但得到: 'RiotAPI'对象没有属性'_request'

2 个答案:

答案 0 :(得分:1)

用于连接 Riot API 和获取召唤师信息的更简单代码:

class RiotApi(object):
    def __init__(self, api_key: str, region="euw1"):
        self.__RIOT_API_KEY = api_key
        self.__HEADER = {'X-Riot-Token': self.__RIOT_API_KEY}
        self.__REGION = region
        self.__BASE_URL = ".api.riotgames.com/lol/"
        self.__API_URL_SUMMONER_V4 = "https://" + self.__REGION + self.__BASE_URL + "summoner/v4/summoners/"

    def get_summoner_by_name(self, summoner_name: str) -> dict:
        """Summoner Infos and Ids
        @param summoner_name: LoL summoner name
        @return json object of infos and ids
        """
        url = self.__API_URL_SUMMONER_V4 + "by-name/" + summoner_name
        request = requests.get(url, headers=self.__HEADER)
        return request.json()


答案 1 :(得分:0)

如注释中所述,object has no attribute错误是由未定义属性(或方法)的调用引起的-在这种情况下为_request()方法。因此,假设其余代码正确,则可以尝试以下代码:

# class RiotAPI(object):
class RiotAPI:
    def __init__(self,api_key,region=Consts.REGIONS['europe_nordic_and_east']):
        self.api_key = api_key
        self.region = region

    # def request(self, api_key, params={}):
    def request(self, api_url, params={}):
        args = {'api_key': self.api_key}
        for k,v in params.items():
            if k not in args:
                args[k] = v
        response = requests.get(
            Consts.URL['base'].format(
                proxy = self.region,
                region = self.region,
                url = api_url
                ),
            params=args
            )
        print (response.url)
        return response.json()

    def get_summoner_by_name(self, name):
        api_url = Consts.URL['summoner_by_name'].format(
            version=Consts.API_VERSIONS['summoner'],
            summonerName=name
            )
        #return self._request(api_url)
        return self.request(api_url)