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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 | |
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
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
__aenter__
async
¶
__aenter__() -> CocApi
Async context manager entry - enables async mode automatically
Source code in cocapi/client.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
__aexit__
async
¶
__aexit__(exc_type: Any, exc_val: Any, exc_tb: Any) -> None
Async context manager exit
Source code in cocapi/client.py
277 278 279 280 281 | |
test
¶
test() -> dict[str, Any]
Test API connection
Source code in cocapi/client.py
572 573 574 575 576 577 578 579 580 581 582 583 584 | |
clan_current_war_auto
¶
clan_current_war_auto(
clan_tag: str,
) -> dict[str, Any] | Awaitable[dict[str, Any]]
Get the clan's current war, falling back to its Clan War League war.
Tries /currentwar first. If the clan is not in a regular war (or the
war log is private), looks up the CWL group and returns the most relevant
CWL war involving the clan. CWL results include "is_cwl": True and
"war_tag". Private war logs return an error with
error_kind == "private_war_log" when no CWL war is available either.
Source code in cocapi/client.py
847 848 849 850 851 852 853 854 855 856 857 858 859 860 | |
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
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 929 | |
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
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 | |
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
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 | |
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
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 | |
get_base_url
¶
get_base_url() -> str
Get current base URL
Source code in cocapi/client.py
1087 1088 1089 | |
reset_base_url
¶
reset_base_url() -> None
Reset to official SuperCell API URL
Source code in cocapi/client.py
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 | |
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
1110 1111 1112 1113 1114 1115 1116 1117 1118 | |
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
1120 1121 1122 1123 1124 | |
get_metrics
¶
get_metrics() -> dict[str, Any]
Get API metrics summary
Source code in cocapi/client.py
1127 1128 1129 | |
clear_metrics
¶
clear_metrics() -> None
Clear stored metrics
Source code in cocapi/client.py
1131 1132 1133 | |
clear_cache
¶
clear_cache() -> int
Clear API response cache
Source code in cocapi/client.py
1136 1137 1138 | |
get_cache_stats
¶
get_cache_stats() -> dict[str, Any]
Get cache statistics
Source code in cocapi/client.py
1140 1141 1142 | |