0

I tried to connect to the Riot api to access the summoner info. I wrote 3 files:

  1. "RiotConsts": a file setting some constants such as "URL", "api_version", "region"
  2. "RiotAPI": the main functions
  3. "riot_main": to call the info I want.

I am receiving an error: "RiotAPI' object has no attribute '_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)

I expect to receive the summoner info but I got: 'RiotAPI' object has no attribute '_request'

JackeyOL
  • 313
  • 2
  • 16
  • Hi, welcome to stackoverflow. The error is expected because there isn't any `_request` attribute defined in `RiotAPI` class above. `object` class also doesn't have `request` attribute). Where do you expect the `_request` is defined? Also, which version of python are you using? – Yohanes Gultom Jun 19 '19 at 03:51
  • I am a new programer and I am still learning python. I just read some people writing "self._request" but I am not quite sure how it works. I am using python 3.7. Please let me know if you know how to fix the problem – JackeyOL Jun 20 '19 at 16:12

2 Answers2

1

Simpler code to connect Riot API and to have summoner infos :

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


TheSmartMonkey
  • 857
  • 1
  • 7
  • 23
  • Should I call the first function first (by inputting api_key and region) and then call the second function (by inputting summoner name) for it to work?@TheSmartMonkey – JackeyOL Jul 29 '21 at 20:08
  • First initialize the class `ra = RiotApi("api_key")` then call the function `ra.get_summoner_by_name("summoner_name")` if you want to understand how it works read this article https://www.tutorialspoint.com/python/python_classes_objects.htm – TheSmartMonkey Aug 02 '21 at 16:36
0

As mentioned in comment, object has no attribute error is caused by invocation of undefined attribute (or method) -- in this case _request() method. So, assuming the rest of the code are correct, you can try this code:

# 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)
Yohanes Gultom
  • 3,782
  • 2
  • 25
  • 38