client
client
¶
Main CocApi client - simplified and modular version
CocApi
¶
CocApi(
token: str,
timeout: int = 20,
status_code: bool = False,
config: ApiConfig | None = None,
async_mode: bool = False,
)
Bases: ApiMethods
Clash of Clans API Wrapper with enhanced v3.0.0 features
Provides both sync and async interfaces with caching, metrics, middleware, and dynamic model generation capabilities.
Initialize CocApi with enhanced features
| PARAMETER | DESCRIPTION |
|---|---|
token
|
API token from developer.clashofclans.com
TYPE:
|
timeout
|
Request timeout in seconds (backward compatibility)
TYPE:
|
status_code
|
Include status code in responses (backward compatibility)
TYPE:
|
config
|
Optional ApiConfig for advanced settings
TYPE:
|
async_mode
|
Enable async mode (default: False for backward compatibility)
TYPE:
|
Source code in cocapi/client.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
from_credentials
classmethod
¶
from_credentials(
email: str,
password: str,
timeout: int = 20,
status_code: bool = False,
config: ApiConfig | None = None,
) -> CocApi
Create a CocApi instance using SuperCell developer portal credentials.
Instead of providing a raw API token, provide your developer portal email and password. Keys are automatically created and managed based on your current public IP.
| PARAMETER | DESCRIPTION |
|---|---|
email
|
SuperCell developer portal email
TYPE:
|
password
|
SuperCell developer portal password
TYPE:
|
timeout
|
Request timeout in seconds
TYPE:
|
status_code
|
Include status code in responses
TYPE:
|
config
|
Optional ApiConfig for advanced settings
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CocApi
|
Configured CocApi instance with a managed API token |
Example::
api = CocApi.from_credentials("email@example.com", "password")
clan = api.clan_tag("#CLAN_TAG")
Source code in cocapi/client.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
__aenter__
async
¶
__aenter__() -> CocApi
Async context manager entry - enables async mode automatically
Source code in cocapi/client.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
__aexit__
async
¶
__aexit__(exc_type: Any, exc_val: Any, exc_tb: Any) -> None
Async context manager exit
Source code in cocapi/client.py
228 229 230 231 232 | |
test
¶
test() -> dict[str, Any]
Test API connection
Source code in cocapi/client.py
510 511 512 513 514 515 516 517 518 519 520 521 522 | |
paginate
¶
paginate(
method: Callable[..., Any], *args: Any, limit: int = 100
) -> Any
Auto-paginate through all results from a list endpoint.
Yields individual items from each page, automatically following
the after cursor until all pages are exhausted.
| PARAMETER | DESCRIPTION |
|---|---|
method
|
An API method that returns paginated results
(e.g.
TYPE:
|
*args
|
Positional arguments for the method excluding the
TYPE:
|
limit
|
Items per page (default 100).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
Generator (sync) or async generator (async) of individual items. |
Example::
for member in api.paginate(api.clan_members, "#TAG"):
print(member["name"])
Source code in cocapi/client.py
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 | |
batch
¶
batch(
method: Callable[..., Any],
args_list: list[Any],
max_concurrent: int | None = None,
) -> list[dict[str, Any]] | Awaitable[list[dict[str, Any]]]
Fetch multiple resources in one call.
| PARAMETER | DESCRIPTION |
|---|---|
method
|
An API method (e.g.
TYPE:
|
args_list
|
List of arguments — each element is passed to method. Use tuples for methods that take multiple positional args.
TYPE:
|
max_concurrent
|
Limit concurrent requests in async mode (ignored in sync mode).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[dict[str, Any]] | Awaitable[list[dict[str, Any]]]
|
List of response dicts (sync) or awaitable list (async). |
list[dict[str, Any]] | Awaitable[list[dict[str, Any]]]
|
Failed calls return their error dict in-place. |
Example::
results = api.batch(api.players, ["#TAG1", "#TAG2", "#TAG3"])
Source code in cocapi/client.py
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 | |
custom_endpoint
¶
custom_endpoint(
endpoint_path: str,
params: dict[str, Any] | None = None,
use_dynamic_model: bool = False,
) -> dict[str, Any] | Awaitable[dict[str, Any]]
Call a custom API endpoint (future-proofing for new SuperCell endpoints)
| PARAMETER | DESCRIPTION |
|---|---|
endpoint_path
|
The endpoint path (e.g., "/clans/new-feature")
TYPE:
|
params
|
Optional query parameters
TYPE:
|
use_dynamic_model
|
Whether to generate dynamic Pydantic models
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | Awaitable[dict[str, Any]]
|
API response as dict or Pydantic model |
Source code in cocapi/client.py
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 | |
set_base_url
¶
set_base_url(
new_base_url: str, force: bool = False
) -> None
Change the base API URL with safety warnings
| PARAMETER | DESCRIPTION |
|---|---|
new_base_url
|
New base URL to use
TYPE:
|
force
|
Skip safety warnings (use with caution)
TYPE:
|
Source code in cocapi/client.py
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 | |
get_base_url
¶
get_base_url() -> str
Get current base URL
Source code in cocapi/client.py
964 965 966 | |
reset_base_url
¶
reset_base_url() -> None
Reset to official SuperCell API URL
Source code in cocapi/client.py
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 | |
add_request_middleware
¶
add_request_middleware(
middleware: Callable[
[str, dict[str, str], dict[str, Any]],
tuple[str, dict[str, str], dict[str, Any]],
],
) -> None
Add request middleware - delegates to middleware manager
Source code in cocapi/client.py
987 988 989 990 991 992 993 994 995 | |
add_response_middleware
¶
add_response_middleware(
middleware: Callable[[dict[str, Any]], dict[str, Any]],
) -> None
Add response middleware - delegates to middleware manager
Source code in cocapi/client.py
997 998 999 1000 1001 | |
get_metrics
¶
get_metrics() -> dict[str, Any]
Get API metrics summary
Source code in cocapi/client.py
1004 1005 1006 | |
clear_metrics
¶
clear_metrics() -> None
Clear stored metrics
Source code in cocapi/client.py
1008 1009 1010 | |
clear_cache
¶
clear_cache() -> int
Clear API response cache
Source code in cocapi/client.py
1013 1014 1015 | |
get_cache_stats
¶
get_cache_stats() -> dict[str, Any]
Get cache statistics
Source code in cocapi/client.py
1017 1018 1019 | |