CodeforcesApiPy icon indicating copy to clipboard operation
CodeforcesApiPy copied to clipboard

'function' object has no attribute 'urlencode'

Open Lumonike opened this issue 1 year ago • 1 comments

Describe the bug I installed the package into a virtual environment and proceeded to attempt the following code:

import codeforces_api
import json

SECRETS = json.load(open("secret.json"))

cf_api = codeforces_api.CodeforcesApi(api_key = SECRETS["key"], secret = SECRETS["secret"])

print(cf_api.contest_list(gym = True))

which of course raised the error

Traceback (most recent call last):
  File "/root/lumonike/cf_api/main.py", line 8, in <module>
    print(cf_api.contest_list(gym = True))
  File "/root/lumonike/cf_api/env/lib/python3.10/site-packages/codeforces_api/api_requests.py", line 112, in contest_list
    for contest in self._make_request(
  File "/root/lumonike/cf_api/env/lib/python3.10/site-packages/codeforces_api/api_requests.py", line 49, in _make_request
    request_data = self.generate_request(method, **payload)
  File "/root/lumonike/cf_api/env/lib/python3.10/site-packages/codeforces_api/api_request_maker.py", line 71, in generate_request
    api_signature += requests.urllib3.request.urlencode(fields, safe=";")
AttributeError: 'function' object has no attribute 'urlencode'

To Reproduce

  1. follow steps here: https://codeforces.com/blog/entry/74291
  2. run code listed above

Expected behavior The error mentioned above will be raised

Environment:

  • Python version: 3.10.12
  • Module version: 2a5f8e8
  • OS version: Ubuntu 22.04

Possible Solution I believe switching line 71 of api_request_maker.py: api_signature += requests.urllib3.request.urlencode(fields, safe=";") into

api_signature += urlencode(fields, safe=";")

with the addition of this module from urllib.parse import urlencode

completely solve this issue.

Lumonike avatar Mar 21 '24 19:03 Lumonike

I have the same issue, Python 3.11.

A bit of archaeology here - this is the old code here:

https://github.com/urllib3/urllib3/blob/82f8011678714baffcba1a311c50fd0de5dc7250/src/urllib3/request.py#L4

It seems that urllib3 moved on from this quite a while ago, in this changeset:

https://github.com/urllib3/urllib3/commit/cff8b5ba3d2ba9338e31c102e1e91273124df297

--

Fix is just to use urllib in the standard Python3 library (just tested)

Replace:

        api_signature += requests.urllib3.request.urlencode(fields, safe=";")

with:

        api_signature += urllib.parse.urlencode(fields, safe=";")

jpz avatar May 17 '24 13:05 jpz

getting same error, how to fix it without making change in module file.

SanjaySRocks avatar Jun 30 '24 15:06 SanjaySRocks

getting same error, how to fix it without making change in module file.

Hello! Just fixed that, I'll release fixed version soon. Sorry for inconvenience!

VadVergasov avatar Jun 30 '24 17:06 VadVergasov