codingame_tools.client.common¶
common
¶
Low-level client, exceptions, and wire-protocol schemas shared across the CodinGame API client.
BROWSER_LOGIN_SUBDIR
module-attribute
¶
BROWSER_LOGIN_SUBDIR = 'browser-login'
Subdirectory under a profile's private storage directory in which browser persistent session
state for login is stored, e.g., profiles/<profile_name>/browser-login.
CLIENT_APP_NAME
module-attribute
¶
CLIENT_APP_NAME = 'codingame'
The default name of the application for the purpose of isolating app-specific files (cached credentials, etc.).
DEFAULT_PROFILE_NAME
module-attribute
¶
DEFAULT_PROFILE_NAME = 'default'
The default profile name for managing independent sets of credentials and browser session state.
PROFILES_SUBDIR
module-attribute
¶
PROFILES_SUBDIR = 'profiles'
Subdirectory under an app's private storage directory under which all per-profile state
(credentials, browser session state, etc.) is stored, e.g., profiles/<profile_name>/....
CG_SESSION_TOKEN_ENV_VAR
module-attribute
¶
CG_SESSION_TOKEN_ENV_VAR = 'CODINGAME_SESSION'
The name of the environment variable that can be set to provide the CodinGame cg_session cookie for authentication.
REMEMBER_ME_TOKEN_ENV_VAR
module-attribute
¶
REMEMBER_ME_TOKEN_ENV_VAR = 'CODINGAME_REMEMBER_ME'
The name of the environment variable that can be set to provide the CodinGame remember_me cookie for authentication.
CgCredentials
dataclass
¶
CgCredentials(extra_data=dict(), remember_me_cookie=None, cg_session_cookie=None)
Bases: JSONWizardX
Persistable CodinGame session credentials.
Both cookies are optional since a partially-completed browser login may capture
only the rememberMe cookie before the cgSession cookie becomes available.
extra_data
class-attribute
instance-attribute
¶
extra_data = field(default_factory=dict)
Unrecognized fields encountered when loading a credentials file, preserved so that
round-tripping through saves()/loads() does not silently drop data.
remember_me_cookie
class-attribute
instance-attribute
¶
remember_me_cookie = None
Value of the CodinGame rememberMe cookie, used to establish a new session.
cg_session_cookie
class-attribute
instance-attribute
¶
cg_session_cookie = None
Value of the CodinGame cgSession cookie for an active session. Required for some
operations (e.g., file upload) that are not supported via rememberMe alone.
CgAuthenticationError
¶
CgAuthenticationError(message=None)
Bases: Exception
Raised when the client is not authenticated and an operation requires authentication.
Source code in codingame_tools/client/common/raw_client.py
215 216 | |
CgClientErrorResponse
dataclass
¶
CgClientErrorResponse(code, extra_data=dict(), message=None)
Bases: JSONWizardX
Represents a well-formed JSON error response from the CodinGame API.
code
instance-attribute
¶
code
The error code string returned by the API; e.g., 'BODY_MUST_BE_JSON_ARRAY'. This property is always present in a well-formed error response, and must not be present in any non-error response.
extra_data
class-attribute
instance-attribute
¶
extra_data = field(default_factory=dict)
Unrecognized fields encountered when loading the error response, preserved.
CgClientHttpError
¶
CgClientHttpError(message=None, *, response=None, content=MISSING, status_code=None)
Bases: Exception
Raised for HTTP-level failures making a request to the CodinGame API. Contains the status
code and content of the response, if available, and--since the client is built on
aiohttp--the underlying aiohttp.ClientResponse too, for debugging purposes.
Create a CgClientHttpError, providing available context.
Parameters:
-
message(str | None, default:None) –Optional error message. If not provided, will use the status code's default phrase
-
response(ClientResponse | None, default:None) –Optional aiohttp.ClientResponse object. If provided, will be used to determine the status code and content if not provided.
-
content(JsonData | bytes | None | _Missing, default:MISSING) –Optional decoded content of the response. If not provided, will attempt to use already cached content bytes read from the response, if provided. If not provided and no cached content is available, will be None.
-
status_code(int | None, default:None) –Optional status code. If not provided, will attempt to read from the response if provided, or 200 otherwise.
Source code in codingame_tools/client/common/raw_client.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
status_code
instance-attribute
¶
status_code = status_code
The HTTP status code of the response; e.g., 400.
raw_message
instance-attribute
¶
raw_message = message or HTTPStatus(status_code).phrase
The unadorned error message provided at construction time. If none was provided, this will be the default phrase for the HTTP status code; e.g., "Bad Request".
content
instance-attribute
¶
content = content
The decoded content of the response, if available. If the response was valid JSON, this will be the decoded JsonData value (which may be a dict, list, str, int, float, bool, or None). If the response could not be decoded as JSON or text, this may be raw bytes. If None, the content was not available.
api_error_response
class-attribute
instance-attribute
¶
api_error_response = CgClientErrorResponse.from_dict(content)
If the response content was a well-formed JSON error response, this will be a CgClientErrorResponse instance.
response
instance-attribute
¶
response = response
The underlying aiohttp response, if one was involved (some errors are raised before any response exists).
normalize
classmethod
¶
normalize(e, *, content=MISSING, response=None)
Normalize an exception raised by aiohttp into a CgClientHttpError, preserving the status code and message. Args: e: The original aiohttp.ClientResponseError exception. content: Optional decoded content of the response. If not provided, will attempt to use already cached content bytes read from the response, if provided. If not provided and no cached content is available, will be None. response: Optional aiohttp.ClientResponse object. If provided, will be used to determine the status code and content if not provided.
Source code in codingame_tools/client/common/raw_client.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
CgDownloadFileResult
¶
Bases: NamedTuple
The result of a successful file download
id
instance-attribute
¶
id
The globally unique ID of the file, as provided by the server at upload time.
content_type
instance-attribute
¶
content_type
The content type of the downloaded file, as provided by the server. Normalized to lowercase.
hash
instance-attribute
¶
hash
The SHA256 hash of the downloaded file content, as a hex string. This can be used to verify the integrity of the downloaded file or to detect changes in local copies.
filename
class-attribute
instance-attribute
¶
filename = None
The filename of the downloaded file, if provided by the server in the Content-Disposition header. Does not include a path. This is typically the original filename of the uploaded file.
create
classmethod
¶
create(id, content, content_type, filename=None, hash=None)
Create a CgDownloadFileResult instance with the given content, content type, and optional filename.
Source code in codingame_tools/client/common/raw_client.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
CgFileUploadError
¶
CgFileUploadError(error_type, error_message, *, field_name='', name, size)
Bases: CgServletError
Raised when the fileupload servlet accepts the HTTP request itself (a 200 OK) but
rejects the uploaded file's content--e.g. an unsupported format--returning an embedded
error object in its response instead of a successful upload entry. Confirmed live (2026-07-27):
uploading a plain-text file returns
{"result": [{"error": {"type": "UNSUPPORT_FILE_ERROR", "message": "Unsupported file: "
"Format not supported"}, "fieldName": "file", "name": "...", "size": ...}]}.
Source code in codingame_tools/client/common/raw_client.py
181 182 183 184 185 186 187 188 189 190 191 192 | |
CgRawClient
¶
CgRawClient(*, profile_name=None, default_http_headers=None, trace_configs=None, app_name=None, settings=None)
Low-level (JsonData) client for the CodinGame API, built on aiohttp.
Create a CgRawClient.
Parameters:
-
profile_name(str | None, default:None) –Optional name of the profile to use for persistent credentials. Allows for multiple independent session profiles; e.g., if multiple CodinGame accounts are used. If None, the default profile name is resolved from
settings(see below). This parameter may be overridden at authenticate() time. -
default_http_headers(dict[str, str] | None, default:None) –Optional default HTTP headers for requests. If None, default headers are used. -
trace_configs(list[TraceConfig] | None, default:None) –Optional list of aiohttp.TraceConfig for the session. If None, an empty list is used.
-
app_name(str | None, default:None) –Optional name of the application using the client. Used to allow different applications to have different cached credentials in the same environment. If None, a default application name is used.
-
settings(CgSettings | None, default:None) –Optional CgSettings to resolve the default profile name from, used only when
profile_nameis None. If not given (andprofile_nameis also not given), the normal config/settings discovery path is used, best-effort--matching how credential resolution elsewhere in this class never requires setup to exist first: if no config.yaml can be found, a synthetic all-defaults CgConfig is used instead of raising (seeresolve_config(allow_default=True)), so this never requirescg config initto have been run. TheCgConfigis not a separate parameter since it's already reachable assettings.config.
Source code in codingame_tools/client/common/raw_client.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
CODINGAME_BASE_URL
class-attribute
instance-attribute
¶
CODINGAME_BASE_URL = 'https://www.codingame.com'
Base URL for the CodinGame website. Used for most API requests, except the "static" endpoint.
CODINGAME_SERVLET_URL
class-attribute
instance-attribute
¶
CODINGAME_SERVLET_URL = CODINGAME_BASE_URL + '/servlet'
Base URL for the CodinGame servlet endpoint. Used for file uploads and downloads.
CODINGAME_SERVICES_URL
class-attribute
instance-attribute
¶
CODINGAME_SERVICES_URL = CODINGAME_BASE_URL + '/services/'
Base URL for the CodinGame "services" requests. Used for most API requests.
CODINGAME_STATIC_BASE_URL
class-attribute
instance-attribute
¶
CODINGAME_STATIC_BASE_URL = 'https://static.codingame.com'
Base URL for the CodinGame static content endpoint. Used for file downloads.
CODINGAME_STATIC_SERVLET_URL
class-attribute
instance-attribute
¶
CODINGAME_STATIC_SERVLET_URL = CODINGAME_STATIC_BASE_URL + '/servlet'
Base URL for the CodinGame static servlet endpoint. Used for file downloads.
profile_name
class-attribute
instance-attribute
¶
profile_name = profile_name
The name of the profile to use for persistent credentials. Allows for multiple independent session profiles; e.g., if multiple CodinGame accounts are used. If None, defaults to the default profile. May be provided at construction or at authenticate() time.
credentials
class-attribute
instance-attribute
¶
credentials = None
If the client is logged in, this will hold the credentials used for authentication.
saved_credentials
class-attribute
instance-attribute
¶
saved_credentials = None
Known contents of the saved credentials, if any. This is used to determine whether the credentials have changed and need to be saved.
login_attempted
class-attribute
instance-attribute
¶
login_attempted = False
Whether a login attempt has been made. If True, further implicit login attempts will not be made.
app_name
class-attribute
instance-attribute
¶
app_name = app_name
The name of the application using the client. Used to allow different applications to have different cached credentials in the same environment. If None, a default application name is used.
default_http_headers
instance-attribute
¶
default_http_headers = default_http_headers or DEFAULT_HEADERS
The HTTP headers used for requests.
codingamer_id
class-attribute
instance-attribute
¶
codingamer_id = None
The codingamer ID of the currently logged-in user, if available. This is derived from the first part of the rememberMe cookie.
session
instance-attribute
¶
session = aiohttp.ClientSession(headers=self.default_http_headers, trace_configs=self._trace_configs)
The aiohttp session used for requests.
close
async
¶
close()
Close the client session.
Source code in codingame_tools/client/common/raw_client.py
499 500 501 | |
set_cookie
¶
set_cookie(name, value=None, *, domain='www.codingame.com')
Set a cookie for the client session.
The cookie will be sent with all requests to the specified domain for the remainder of the client session.
If value is None, the cookie will be deleted.
Source code in codingame_tools/client/common/raw_client.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | |
set_credentials
¶
set_credentials(credentials)
Set the credentials for the client session.
If credentials are provided, they will be used to authenticate/reauthenticate the client session. If not provided, empty credentials are used, effectively logging out the client session.
The client is only considered logged in if both a rememberMe and a cgSession cookie are present; credentials with only one (or neither) are treated the same as no credentials at all. This is because enough CodinGame endpoints require cgSession specifically (not just rememberMe) that a partial session isn't useful in practice.
The rememberMe and cgSession cookies are updated to match the credentials.
Persistent credentials are not affected.
Returns:
-
CgCredentials–The (deep-copied) credentials that are now cached. If there are no credentials, returns an empty CgCredentials() object.
Source code in codingame_tools/client/common/raw_client.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | |
clear_credentials
¶
clear_credentials()
Clear the credentials for the client session, effectively logging out the client session.
Persistent credentials are not affected.
Source code in codingame_tools/client/common/raw_client.py
575 576 577 578 579 580 | |
resolve_credentials
¶
resolve_credentials(*, profile_name=MISSING, remember_me_token=None, cg_session_token=None, credentials=None, force=False)
Resolve the current credentials for the client, with parameter and environment variable overrides.
Resolution order
- If force is False and credentials are already cached in the client, use those values.
- If non-null
remember_me_token/cg_session_tokenare provided, use those values. - If
credentialsis provided, use non-null token values from that object. - check the
REMEMBER_ME_TOKEN_ENV_VAR/CG_SESSION_TOKEN_ENV_VARenvironment variables for overrides. - If neither is provided and force is False, check the in-process cache for the app's credentials.
- If not in the cache, check the per-app private credentials file (which populates the cache on success).
- If none of the above are available, return an empty
CgCredentials()
The result of this function is not cached in the client; it is up to the caller to call set_credentials()
if they want to cache the result.
Parameters:
-
profile_name(str | _Missing | None, default:MISSING) –Optional name of the profile to use for persistent credentials. Allows for multiple independent session profiles; e.g., if multiple CodinGame accounts are used. If not provided or MISSING, defaults to the profile_name provided at client construction time. If None, defaults to the default profile.
-
remember_me_token(str | None, default:None) –Optional override for the
rememberMecookie value. -
cg_session_token(str | None, default:None) –Optional override for the
cgSessioncookie value. -
credentials(CgCredentials | None, default:None) –Optional
CgCredentialsobject to use as the base for resolution. -
force(bool, default:False) –If True, ignore the in-process cache and reload from the credentials file.
Returns:
-
CgCredentials–Resolved
CgCredentialsobject, with parameter and environment variable overrides applied. -
CgCredentials–If there are no valid credentials, returns an empty
CgCredentials()object.
Source code in codingame_tools/client/common/raw_client.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 | |
is_logged_in
¶
is_logged_in()
Return True if the client is logged in (i.e., has valid credentials), False otherwise.
Source code in codingame_tools/client/common/raw_client.py
632 633 634 | |
validate_credentials
async
¶
validate_credentials()
Verifies that current client credentials are valid by making a test request to the CodinGame API. Raises CgAuthenticationError if the credentials are invalid or if the request fails for any reason.
The client session must be logged in (i.e., have valid credentials) before calling this method.
This method can be overridden in subclasses to perform a more specific test request, if desired.
Uses Notification/findUnreadNotifications as the test request rather than
CodinGamer/findCodinGamerPublicInformations, since the latter is public and succeeds even
when unauthenticated--it would not actually detect invalid/expired credentials. findUnreadNotifications
requires authentication (it returns 422 when called without a valid session), and empirically appears
to be side-effect-free (repeated calls return identical results, including seenDate).
Source code in codingame_tools/client/common/raw_client.py
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 | |
authenticate
async
¶
authenticate(*, profile_name=MISSING, remember_me_token=None, cg_session_token=None, credentials=None, force=False, require_credentials=False, validate=False)
Authenticate the client session, at one of three independent strictness levels
(require_credentials x validate; a fourth level, no authentication at all, is
available by simply not calling this method--see service_request's require_login):
require_credentials=False, validate=False (the default): best-effort. Resolves
credentials and applies them to the session if available, but does not raise
if none are available--the session is simply left unauthenticated.
require_credentials=True, validate=False: login required. Raises
CgAuthenticationError if no credentials are available. Does not check that
they are still valid/unexpired.
require_credentials=True, validate=True: validated login required. Raises if no
credentials are available, and separately raises if they fail a live
validation check against the server (e.g. expired/revoked).
(require_credentials=False, validate=True is also accepted: best-effort resolution,
and if that happens to find credentials, they are validated too; if it doesn't, this
is still not an error.)
Resolution order
- If force is False and credentials are already cached in the client, do nothing.
- If non-null
remember_me_token/cg_session_tokenare provided, use those values. - If
credentialsis provided, use non-null token values from that object. - check the
REMEMBER_ME_TOKEN_ENV_VAR/CG_SESSION_TOKEN_ENV_VARenvironment variables for overrides. - If neither is provided and force is False, check the in-process cache for the app's credentials.
- If not in the cache, check the per-app private credentials file (which populates the cache on success).
- If none of the above are available, return an empty
CgCredentials()
Parameters:
-
profile_name(str | _Missing | None, default:MISSING) –Optional name of the profile to use for persistent credentials. Allows for multiple independent session profiles; e.g., if multiple CodinGame accounts are used. If not provided or MISSING, the profile provided at client construction time is used. If None, defaults to the default profile.
-
remember_me_token(str | None, default:None) –Optional override for the
rememberMecookie value. -
cg_session_token(str | None, default:None) –Optional override for the
cgSessioncookie value. -
credentials(CgCredentials | None, default:None) –Optional
CgCredentialsobject to use as the base for resolution. -
force(bool, default:False) –If True, ignore the client session and in-process cache and reload from the credentials file.
-
require_credentials(bool, default:False) –If True, raise CgAuthenticationError if no usable credentials could be resolved. If False (the default), silently leave the session unauthenticated.
-
validate(bool, default:False) –If True, verify that the resolved credentials are valid by making a test request. Has no effect if no credentials were resolved and
require_credentialsis False.
Source code in codingame_tools/client/common/raw_client.py
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 | |
require_authenticate
async
¶
require_authenticate()
Ensure that the client session is logged in (i.e., has both a rememberMe and a cgSession
cookie--see set_credentials for why both are required). Implicitly log in if possible.
If not, raise CgAuthenticationError.
Source code in codingame_tools/client/common/raw_client.py
742 743 744 745 746 747 748 749 | |
get_json_data_response
async
¶
get_json_data_response(response)
Get a JSON-decoded response from an aiohttp response, raising CgClientHttpError if the response could not be decoded at all or if the status code is not 2xx.
Unlike a strict JSON-RPC-style API, CodinGame's services may return any JSON-serializable
value at the top level, not just an object--e.g., a bare array, or a bare null (some
endpoints return null when unauthenticated; others return it as a legitimate "no result"
value even when authenticated, e.g. ClashOfCode/getClashRankByCodinGamerId for a codingamer
who has never played). A successfully-decoded JSON null is returned as Python None--a
valid JsonData value. Every code path that fails to obtain/decode any content at all
raises before returning, so a returned None unambiguously means "the body was the JSON
literal null", never "nothing could be read". This method does not attempt to
distinguish a JSON string value from equivalent raw (non-JSON) text content, though.
Returns:
-
JsonData–The JSON-decoded data: a dict, list, str, int, float, bool, or None.
Raises:
-
CgClientHttpError–If a transport error occurs, if the response content could not be decoded at all, or if the status code is not 2xx.
Source code in codingame_tools/client/common/raw_client.py
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 | |
get_json_dict_response
async
¶
get_json_dict_response(response)
Like get_json_data_response, but additionally requires the decoded content to be a JSON dict.
Convenience wrapper for the common case where an endpoint is known to always return a JSON object on success.
Returns:
-
JsonDict–The JSON-decoded dictionary.
Raises:
-
CgClientHttpError–If a transport error occurs, if the response content could not be decoded at all, if the status code is not 2xx, or if the decoded content is not a dict.
Source code in codingame_tools/client/common/raw_client.py
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 | |
get_json_list_response
async
¶
get_json_list_response(response)
Like get_json_data_response, but additionally requires the decoded content to be a JSON list.
Convenience wrapper for the common case where an endpoint is known to always return a JSON array on success.
Returns:
-
JsonList–The JSON-decoded list.
Raises:
-
CgClientHttpError–If a transport error occurs, if the response content could not be decoded at all, if the status code is not 2xx, or if the decoded content is not a list.
Source code in codingame_tools/client/common/raw_client.py
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 | |
service_request
async
¶
service_request(service_name, func_name, args=None, *, require_login=True)
Make an API request to a CodinGame service endpoint, returning its JSON-decoded response.
This is the most common type of request made to the CodinGame API. It is used for most endpoints, except for file uploads and downloads.
Generates a POST request to the URL https://www.codingame.com/services/{service_name}/{func_name}
with a JSON body of args.
In general, the session must be authenticated.
This is a low-level method that does not distinguish between normal responses and error responses,
and does not assume the response is a JSON object--some endpoints return a bare array, or a bare
null, depending on the service and function called.
Args: service_name: The name of the CodinGame service; e.g., "Vote", or "Contribution". func_name: The name of the function to call within the service; e.g., "findContribution". args: A list of JsonData positional arguments to pass to the function. require_login: If True (the default), the session must be logged in (i.e., have both a rememberMe and a cgSession cookie). If False, the request will be made without requiring authentication, for endpoints that are genuinely public.
Returns: The JSON-decoded response data. May be a successful response or an error response, depending on the service and function called.
Raises: CgAuthenticationError: If the session is not authenticated and cannot implicitly login. CgClientHttpError: If a transport error occurs, if the response content could not be decoded at all, or if the status code is not 2xx.
Source code in codingame_tools/client/common/raw_client.py
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 | |
service_request_to_dict
async
¶
service_request_to_dict(service_name, func_name, args=None, *, require_login=True)
Like service_request, but additionally requires (and type-checks) that the response is a JSON dict.
See service_request for details on the request; see get_json_dict_response for details on
the additional error condition.
Raises: CgAuthenticationError: If the session is not authenticated and cannot implicitly login. CgClientHttpError: If a transport error occurs, if the response content could not be decoded at all, if the status code is not 2xx, or if the decoded content is not a dict.
Source code in codingame_tools/client/common/raw_client.py
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 | |
service_request_to_list
async
¶
service_request_to_list(service_name, func_name, args=None, *, require_login=True)
Like service_request, but additionally requires (and type-checks) that the response is a JSON list.
See service_request for details on the request; see get_json_list_response for details on
the additional error condition.
Raises: CgAuthenticationError: If the session is not authenticated and cannot implicitly login. CgClientHttpError: If a transport error occurs, if the response content could not be decoded at all, if the status code is not 2xx, or if the decoded content is not a list.
Source code in codingame_tools/client/common/raw_client.py
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 | |
servlet_get_bytes
async
¶
servlet_get_bytes(base_url, servlet_name, params=None, *, require_login=True)
Make a GET request to a CodinGame servlet endpoint, returning its raw content bytes along with the response (for its headers).
Generates a GET request to {base_url}/{servlet_name}, with params (if any)
URL-encoded as a query string.
This is a low-level, content-shape-agnostic method--unlike service_request*, it does
not assume a JSON response, since servlets like fileservlet return arbitrary binary
content. Named *_bytes (rather than a general-purpose servlet_get) because the body
is read and returned directly as bytes: aiohttp releases the underlying connection
once the request's async with block exits, after which the response object's own
.read()/.text() can no longer be called (though its .headers/.status remain
readable)--a hypothetical future servlet_get_json (or similar) for a JSON-returning
GET servlet would need its own decode-before-return method, not a shared one returning
the raw response.
Parameters:
-
base_url(str) –The servlet's base URL, e.g.
CODINGAME_STATIC_SERVLET_URL. -
servlet_name(str) –The servlet's name, e.g. "fileservlet".
-
params(dict[str, str] | None, default:None) –Optional query string parameters.
-
require_login(bool, default:True) –If True (the default), the session must be logged in. If False, the request is made with whatever credentials (if any) are already attached to the session--some servlets are genuinely public.
Returns:
-
CgServletGetBytesResult–A CgServletGetBytesResult(content, response)--the response body as bytes, and the
-
CgServletGetBytesResult–aiohttp.ClientResponse (for reading headers such as Content-Type/Content-Disposition).
Raises:
-
CgAuthenticationError–If require_login is True and the session is not authenticated and cannot implicitly login.
-
CgClientHttpError–If a transport error occurs, or if the status code is not 2xx.
Source code in codingame_tools/client/common/raw_client.py
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 | |
servlet_post
async
¶
servlet_post(base_url, servlet_name, *, data=None, params=None, require_login=True)
Make a POST request to a CodinGame servlet endpoint, returning its JSON-decoded dict response.
Generates a POST request to {base_url}/{servlet_name} (with params, if any,
URL-encoded as a query string) with the given request body.
This is a low-level method that does not distinguish between normal responses and error responses, provided they are a valid JsonDict.
Parameters:
-
base_url(str) –The servlet's base URL, e.g.
CODINGAME_SERVLET_URL. -
servlet_name(str) –The servlet's name, e.g. "fileupload".
-
data(FormData | bytes | str | None, default:None) –The request body, e.g. an
aiohttp.FormDatafor a multipart request. -
params(dict[str, str] | None, default:None) –Optional query string parameters.
-
require_login(bool, default:True) –If True (the default), the session must be logged in.
Returns:
-
JsonDict–The JSON-decoded response as a dict. May be a successful response or an error
-
JsonDict–response, depending on the servlet.
Raises:
-
CgAuthenticationError–If require_login is True and the session is not authenticated and cannot implicitly login.
-
CgClientHttpError–If a transport error occurs, if the response content could not be decoded at all, if the status code is not 2xx, or if the decoded content is not a dict.
Source code in codingame_tools/client/common/raw_client.py
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 | |
CgServletError
¶
CgServletError(error_type, error_message, *, field_name='')
Bases: Exception
Base class for an embedded per-entry error returned by a servlet in an otherwise-successful
(200 OK) response--i.e. an application-level error signaled inside the JSON body rather
than via HTTP status, so it can't be caught as a CgClientHttpError.
This is not a claim that all servlets share one common error response shape--currently
only fileupload is known to work this way (see CgFileUploadError)--just the common
subset of fields (error_type, error_message, field_name) that make sense to factor
out if/when a second servlet turns out to follow the same pattern.
Source code in codingame_tools/client/common/raw_client.py
154 155 156 157 158 | |
error_type
instance-attribute
¶
error_type = error_type
The server's error type code, e.g. "UNSUPPORT_FILE_ERROR".
error_message
instance-attribute
¶
error_message = error_message
The server's human-readable error message, e.g. "Unsupported file: Format not supported".
field_name
instance-attribute
¶
field_name = field_name
The form field name the error applies to, if applicable. Defaults to "" when not applicable or not provided by the server.
CgServletGetBytesResult
¶
Bases: NamedTuple
The result of CgRawClient.servlet_get_bytes: a servlet GET response's raw content
bytes, paired with the aiohttp.ClientResponse (for its headers, e.g. Content-Type/
Content-Disposition). Only content and response.headers/.status remain usable--
aiohttp releases the underlying connection once the request's async with block exits, so
response.read()/.text() must not be called again.
CgUploadFileResult
¶
Bases: NamedTuple
The well-typed result of a successful file upload--parsed from the raw fileupload
servlet response, e.g. {"result": [{"fieldName": "file", "name": "cover.png",
"size": 250401, "id": 163935944975958}]}.
id
instance-attribute
¶
id
The globally unique ID assigned to the uploaded file. Used to download the file later
(see CgDownloadFileResult.id) or to reference it from other APIs that accept file IDs.
name
instance-attribute
¶
name
The filename as echoed back by the server; normally matches the filename provided at
upload time.
field_name
instance-attribute
¶
field_name
The multipart form field name the file was uploaded under. Always "file" in current usage.
from_dict
classmethod
¶
from_dict(d)
Parse a CgUploadFileResult from a successful entry of a raw fileupload servlet
response's "result" list. Assumes d is already known to be a successful entry
(not an embedded per-file error--see CgFileUploadError); callers must check for that
themselves before calling this.
Source code in codingame_tools/client/common/raw_client.py
119 120 121 122 123 124 125 126 127 128 129 130 | |
get_credentials
¶
get_credentials(*, profile_name=None, store=None, app_name=None)
Simplified function to get the current credentials for a profile, without any overrides.
If no credentials are available, returns an empty CgCredentials() object.
Parameters:
-
profile_name(str | None, default:None) –Optional profile name to use for fetching credentials. If None, the default profile is used.
-
store(CgCredentialsProfileStore | None, default:None) –Optional CgCredentialsProfileStore to use for fetching credentials. If None, the default persistent store singleton for the given app name is used.
-
app_name(str | None, default:None) –The app namespace to read credentials from when store is None; defaults to
CLIENT_APP_NAME.
Returns:
-
CgCredentials–Resolved
CgCredentialsobject. If no credentials are available, returns an emptyCgCredentials()object.
Source code in codingame_tools/credentials/cg_credentials.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
get_credentials_with_override
¶
get_credentials_with_override(*, profile_name=None, store=None, credentials=None, remember_me_token=None, cg_session_token=None, app_name=None)
Return the current credentials for an app, with environment variable overrides.
Resolution order
- If non-null
remember_me_token/cg_session_tokenare provided, use those values. If one is provided, both must be provided. - If
credentialsis provided with non-null tokens, use non-null token values from that object. Both must be non-null. - check the
REMEMBER_ME_TOKEN_ENV_VAR/CG_SESSION_TOKEN_ENV_VARenvironment variables for overrides. If one is provided, both must be provided. - If store is provided, check the store for the given profile name (or the default profile if None) and use those values if available.
- If store is None, use the default persistent store for the given app name (or the default app name if None) and check for the given profile name (or the default profile if None) and use those values if available.
- If none of the above are available, return an empty
CgCredentials()
Note that overrides, if any, are not persisted to store.
Parameters:
-
profile_name(str | None, default:None) –Optional profile name to use for fetching credentials. If None, the default profile is used.
-
store(CgCredentialsProfileStore | None, default:None) –Optional CgCredentialsProfileStore to use for fetching credentials. If None, the default persistent store singleton for the given app name is used.
-
credentials(CgCredentials | None, default:None) –Optional CgCredentials object to use as an override. Ignored if None or if either of the cookie values are None.
-
remember_me_token(str | None, default:None) –Optional override for the
rememberMecookie value. -
cg_session_token(str | None, default:None) –Optional override for the
cgSessioncookie value. -
app_name(str | None, default:None) –The app namespace to read credentials from when store is None; defaults to
CLIENT_APP_NAME.
Returns:
-
CgCredentials–Resolved
CgCredentialsobject, with parameter and environment variable overrides applied. If -
CgCredentials–no credentials are available, returns an empty
CgCredentials()object.
Source code in codingame_tools/credentials/cg_credentials.py
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | |
set_credentials
¶
set_credentials(credentials, *, profile_name=None, store=None, app_name=None)
Simplified function to set the current credentials for a profile in a credential store and immediately commit.
Parameters:
-
credentials(CgCredentials | None) –The CgCredentials object to set for the given profile. If None, the credentials become deleted/nonexistent.
-
profile_name(str | None, default:None) –Optional profile name to use for setting credentials. If None, the default profile is used.
-
store(CgCredentialsProfileStore | None, default:None) –Optional CgCredentialsProfileStore in which to set credentials. If None, the default persistent store singleton for the given app name is used.
-
app_name(str | None, default:None) –The app namespace to use when store is None; defaults to
CLIENT_APP_NAME.
Source code in codingame_tools/credentials/cg_credentials.py
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
compute_content_hash
¶
compute_content_hash(content)
Compute the SHA256 hash of the given content and return it as a hex string.
Source code in codingame_tools/client/common/raw_client.py
49 50 51 52 53 | |