tejado / pgoapi

Pokemon Go API lib
Other
1.4k stars 445 forks source link

Question: friendly explaination of how get_player() works? #126

Closed trippyhat closed 8 years ago

trippyhat commented 8 years ago

Can someone give a friendly walkthrough of how get_player() works?

The client program instantiates a pgoapi object:

api = pgoapi.PGoApi()

# Then I can do:
api.get_player()

In pgoapi/pgoapi.py, all I see is:

class PGoApi:
     ...

     self.get_player()

     ....

But I'm confused at this point. Where is self.get_player() defined? ie, the actual code that handles that function? I tried grepping through the files for def get_player but it's never declared anywhere.

elliottcarlson commented 8 years ago

https://github.com/tejado/pgoapi/blob/master/pgoapi/pgoapi.py#L108-L128

This is using the getattr magic method (http://python-reference.readthedocs.io/en/latest/docs/dunderattr/getattr.html) to add the "get_player" request if it exists as an RPC defined method (https://github.com/tejado/pgoapi/blob/81e786cabf027a1c8fbd1e9a07e1c11aa3d8ee8b/pgoapi/protos/POGOProtos_src/Networking/Requests/RequestType.proto#L10)

trippyhat commented 8 years ago

@elliottcarlson Thanks. Could you explain a bit how the name get_player is able to get the Enum GET_PLAYER = 2;, which then gets the function definition defined in GetPlayerMessage.proto?

So RequestType.Value("get_player") returns the enum GET_PLAYER = 2? How does this then get the corresponding GetPlayerMessage.proto?

tejado commented 8 years ago

Over converting the string "get_player" to GetPlayer, add "Message to it and instantiate dynamically an object with this name (GetPlayerMessage) https://github.com/tejado/pgoapi/blob/master/pgoapi/rpc_api.py#L144

trippyhat commented 8 years ago

@tejado Thanks! I think I get it now.