Migration Guide¶
This guide covers the changes needed when upgrading from swgoh_comlink v1.x (the requests-based release)
to the new version that introduces async support, StatCalc, and httpx.
1. Update dependencies¶
The HTTP library changed from requests to httpx.
# Remove the old dependency
pip uninstall requests
# Install the new version
pip install --upgrade swgoh_comlink
If your project pins dependencies, update your requirements.txt or pyproject.toml:
httpx is installed automatically as a dependency of swgoh_comlink, so in most cases
you only need to upgrade the package itself.
2. Update exception handling¶
v1.x let requests.RequestException escape on network failures. The client now
catches the transport errors raised by httpx (httpx.RequestError: connection
refused, timeouts, DNS failures) and re-raises them as SwgohComlinkException, so
code that caught the requests exception must catch SwgohComlinkException
instead. Do not switch it to httpx.RequestError; that exception never reaches
your code.
from swgoh_comlink import SwgohComlink
- import requests
+ from swgoh_comlink.exceptions import SwgohComlinkException
comlink = SwgohComlink()
try:
player = comlink.get_player(allycode=245866537)
- except requests.RequestException as e:
+ except SwgohComlinkException as e:
print(f"Network error: {e}")
To tell argument errors apart from service and network errors, catch the subclasses first:
from swgoh_comlink import SwgohComlink
from swgoh_comlink.exceptions import (
SwgohComlinkException,
SwgohComlinkValueError,
SwgohComlinkTypeError,
)
comlink = SwgohComlink()
try:
player = comlink.get_player(allycode=245866537)
except SwgohComlinkValueError as e:
print(f"Invalid argument value: {e}")
except SwgohComlinkTypeError as e:
print(f"Invalid argument type: {e}")
except SwgohComlinkException as e:
print(f"Comlink error: {e}")
The exception hierarchy is:
SwgohComlinkException— base for all library errorsSwgohComlinkValueError(also inheritsValueError) — invalid argument valuesSwgohComlinkTypeError(also inheritsTypeError) — invalid argument typesSwgohComlinkHTTPError— comlink answered with an HTTP error status (v2.3+)SwgohComlinkRateLimitError— HTTP 429 or the game'sRate exceeded!SwgohComlinkUnavailableError— HTTP 503SwgohComlinkClientError— any other 4xx
See Exceptions for details.
3. Update logging configuration¶
The get_logger() helper no longer accepts a log_level parameter and no longer
attaches handlers or sets log levels automatically. The library now follows Python best
practice by attaching only a NullHandler to the package root logger.
Before:
from swgoh_comlink.globals import get_logger
logger = get_logger(__name__, log_level="DEBUG") # auto-configured handler + level
After:
Or target only the swgoh_comlink namespace:
import logging
comlink_logger = logging.getLogger("swgoh_comlink")
comlink_logger.setLevel(logging.DEBUG)
comlink_logger.addHandler(logging.StreamHandler())
See Logging for more examples including the built-in LoggingFormatter
and rotating file handlers.
4. Close the client when done¶
The new version uses persistent httpx connection pools for better performance.
This means the client should be closed when you are finished with it.
Context manager (recommended):
with SwgohComlink() as comlink:
player = comlink.get_player(allycode=245866537)
# connections are closed automatically
Manual close:
comlink = SwgohComlink()
try:
player = comlink.get_player(allycode=245866537)
finally:
comlink.close()
Existing code that does not call close() will still work — connections are cleaned up
on garbage collection — but explicitly closing is preferred to avoid resource warnings.
5. Helpers module refactoring¶
The monolithic helpers.py file has been refactored into a helpers/ subpackage.
All existing import paths continue to work — a backward-compatible re-export shim
ensures no breaking changes.
What moved¶
| Before | After (internal location) |
|---|---|
helpers.py (single file) |
helpers/ subpackage with focused modules |
Constants.STAT_ENUMS |
helpers/_stat_data.STAT_ENUMS (re-exported) |
Constants.STATS |
helpers/_stat_data.STATS (re-exported) |
StatCalc.STATS_NAME_MAP |
Now imports from helpers/_stat_data.STATS |
Constants.get() changes¶
Constants.get() still works for all legacy PascalCase names (e.g., "UnitDefinitions")
as well as the new UPPER_SNAKE_CASE DataItems names (e.g., "UNITS"):
from swgoh_comlink.helpers import Constants
# All three forms still work:
Constants.get("UnitDefinitions") # -> '137438953472' (legacy name)
Constants.get("UNITS") # -> '137438953472' (DataItems name)
Constants.get("Segment1") # -> '2097151' (class attribute)
Recommendation: Prefer using DataItems enum values directly for type safety, request
only the collections you need, and combine members with | — see
GameDataItems server alignment below:
from swgoh_comlink.helpers import DataItems
items = DataItems.SKILL | DataItems.EQUIPMENT
data = comlink.get_game_data(items=items)
Migration checker CLI¶
A CLI tool is included to scan your codebase for patterns that may need updating:
# Scan a project directory
python -m swgoh_comlink.migrate /path/to/your/project
# Or use the console script
swgoh-migrate /path/to/your/project
# Filter by severity and exclude directories
swgoh-migrate . --severity=WARNING --exclude .venv dist
The tool reports deprecated imports, removed APIs, and suggests replacements.
6. Subclass and internal API changes¶
These only matter if you subclass SwgohComlink or call private methods directly.
Constructor inheritance¶
SwgohComlink and SwgohComlinkAsync now inherit from SwgohComlinkBase. The
constructor parameters are unchanged, but the subclass signature uses **kwargs:
# Both still work:
comlink = SwgohComlink(url="http://myhost:3000")
comlink = SwgohComlink(host="myhost", port=3000)
_request() signature¶
def _request(
self,
method: str = "POST",
- url_base: str | None = None,
endpoint: str | None = None,
payload: dict | list | None = None,
+ stats: bool = False,
+ timeout: float | None = None,
) -> dict | list:
url_basewas removed — the method now selects betweenself.url_baseandself.stats_url_basebased on thestatsflag.timeoutallows per-request timeout overrides.
_post() signature¶
Same changes as _request() — url_base replaced by stats and timeout.
7. Removed sentinels¶
All sentinel objects (OPTIONAL, NotSet, REQUIRED, MISSING, GIVEN,
MutualExclusiveRequired) have been removed from swgoh_comlink.helpers.
Remove any sentinel imports from your code.
- from swgoh_comlink.helpers import (
- REQUIRED,
- MISSING,
- GIVEN,
- OPTIONAL,
- NotSet,
- MutualExclusiveRequired,
- )
Functions that previously used sentinel defaults now use standard Python
patterns: required parameters have no default, and optional parameters
default to None.
8. GAC bracket helpers¶
get_gac_brackets() and async_get_gac_brackets() have two changes:
-
The
limitparameter changed from a sentinel default tointwith default0(where0means "no limit"): -
Bracket boundary discovery now uses exponential probing with binary search, reducing HTTP requests from O(n) to O(log n). The async variant also fetches brackets in parallel batches via
asyncio.gather. No code changes are needed on your side — the return format is identical.
9. GameDataItems server alignment¶
DataItems and Constants were re-synced against the live GameDataItemsEnum that
get_enums() now exposes. Two practical impacts:
Segment2 and Segment4 aggregate values changed. If you hardcoded the integers
in your own code (rather than referencing the constants by name), update them:
| Constant | Old value | New value |
|---|---|---|
DataItems.SEGMENT2 / Constants.Segment2 |
68717379584 |
1125968624222208 |
DataItems.SEGMENT4 / Constants.Segment4 |
281200098803712 |
7881024469991424 |
SEGMENT1 (2097151) and SEGMENT3 (206158430208) are unchanged.
Code that references the constant by name (DataItems.SEGMENT2, Constants.Segment2,
Constants.get("Segment2")) picks up the new values automatically.
get_game_data(items=...) accepts any in-range bitmask. Comlink accepts any
items value from 1 to 253 − 1, or -1 (DataItems.ALL; the client sends
-1 for it even on Python 3.15+, where int(DataItems.ALL) is 253 − 1) for everything,
so single collections (e.g. DataItems.UNITS), custom combinations and the
SEGMENT1–SEGMENT4 aggregates all work. Out-of-range values are rejected with an
HTTP 400. Earlier versions of this guide advised against single-collection values;
the failures behind that advice were most likely caused by a since-fixed Comlink bug
in how CDN URLs were built, so upgrade Comlink if you still see them.
Combine members with | rather than +. Aliased members (e.g. CONQUEST_DEFINITION
and ARTIFACT_DEFINITION, which are both the CONQUEST bit) set a different bit when
added together.
New members added (from the live GameDataItemsEnum): ABILITY_DECISION_TREE,
ERA_DEFINITION, UBS_UPDATE. New legacy-name aliases: AbilityDecisionTrees,
EraDefinitions, UBSUpdate, EpisodeDefinitions (plural), AccountLinking.
10. v2.9 changes¶
Upgrading from v2.8 to v2.9.
get_unit_stats(language=)must be a language code. A value that doesn't matchxx_yy/xxx_yy(letters only, e.g.eng_us) now raisesSwgohComlinkValueErrorinstead of being passed through into the query string. A well-formed code that isn't inConstants.LANGUAGESlogs a warning and is still sent. Case is unchanged.- HMAC keys move to
COMLINK_ACCESS_KEY/COMLINK_SECRET_KEY. Rename theACCESS_KEY/SECRET_KEYenvironment variables. The old names still work until v3 with aDeprecationWarning, and are read only when neither new name is set. Constructor arguments still win. Setting only one key now logs a warning. - Plain-HTTP HMAC warning. A client with HMAC keys whose
urlorstats_urlishttp://to a non-loopback host logs one warning. Requests are still sent; switch tohttpsor a private network, or filter theswgoh_comlink._baselogger. - Optional response size limits. New keyword-only
max_response_bytesandmax_game_data_bytes(for/data) arguments make the clients raiseSwgohComlinkValueErrorfor a larger response, instead of reading it all into memory. Both default toNone, so nothing changes unless you set them. See Limiting Response Size. SwgohComlinkAsynccloses idle connections after 5 s. It no longer keeps idle pooled connections open forever; both clients now use the httpx default pool limits. A new keyword-onlylimits=httpx.Limits(...)argument on both clients sets other limits, e.g.limits=httpx.Limits(keepalive_expiry=None)restores the old async behaviour. Request timeouts are unchanged.- HTTP error messages are truncated.
str(exc)forSwgohComlinkHTTPErrorcuts a body longer than 512 characters and appends… (N bytes total). Read the full body fromexc.response.text;exc.code,exc.detailand the exception class still come from the full body. - Bad league or division names raise
SwgohComlinkValueError.get_leaderboardraised a bareKeyErrorfor an unknownleagueordivisionname; it now raisesSwgohComlinkValueError(aValueError, not aKeyError) listing the valid names. CatchSwgohComlinkValueErrorinstead ofKeyError. StatCalc.set_game_datavalidates its tables. A missingunitData,gearData,modSetData,crTables,gpTablesorrelicDatatable, or one set toNone, raisesSwgohComlinkValueErrornaming it, instead ofKeyErrororAssertionError. The previously loaded data is kept.- StatCalc game data download is capped and uses httpx.
StatCalc()fetches throughhttpx(certifi CA bundle, not the system store) and follows redirects in both the sync and async paths. A body over 64 MiB raisesSwgohComlinkValueError. New keyword-onlygame_data_urlandexpected_sha256onStatCalc()andStatCalcAsync.create()pick the source and verify its SHA-256; the default stays the latestmainof swgoh-utils/gamedata. StatCalcAsynccan calculate off the event loop. Newacalc_char_stats,acalc_ship_stats,acalc_roster_statsandacalc_player_statsrun the matchingcalc_*method in a worker thread. The synccalc_*methods are unchanged but block the loop; in async code, replacecalc.calc_player_stats(p)withawait calc.acalc_player_stats(p).- StatCalc reads game data tables by
strkey.set_game_datarebuilds any table holding non-strkeys (such asintkeys from a hand-built dict) withstrkeys, so it now gives the same results as JSON game data; your dict is not modified. Such a rebuilt table is a copy: callset_game_dataagain after changing it. The same applies to a unit'sskillslist, whichset_game_dataindexes by skill id, and togpTables["modRarityLevelTierGP"], which it indexes by pips, level and tier. DataItems.OVERCHARGEand a widerSEGMENT4need Comlink 4.6.0 or later. Game client 0.41 added theOverchargebit (252).DataItems.OVERCHARGErequestsoverchargeDefinition,overchargeStatGroupandoverchargePointMissionLookup(each also available under its own alias, e.g.DataItems.OVERCHARGE_STAT_GROUP), andConstants.get("Overcharge")resolves it. The game folded the bit into segment 4, soDataItems.SEGMENT4/Constants.Segment4changes from3377424842620928to7881024469991424, anditemsaccepts 1 to 253 − 1. A Comlink release older than 4.6.0 rejects both values with an HTTP 400.
Summary of changes¶
| Area | Before (v1.x) | After |
|---|---|---|
| HTTP library | requests |
httpx |
| Transport exception | requests.RequestException |
SwgohComlinkException (wraps httpx.RequestError) |
| Exception hierarchy | SwgohComlinkException, SwgohComlinkValueError |
Added SwgohComlinkTypeError |
get_logger(log_level=) |
Accepted log_level, auto-configured handlers |
No log_level param, no auto-configuration |
| Default log output | Console output at INFO level | Silent (NullHandler only) |
| Client lifecycle | No close needed | Use with or call close() |
| Async support | Not available | SwgohComlinkAsync |
| Local stat calc | Not available | StatCalc (sync) / StatCalcAsync (async) |
_request(url_base=) |
url_base parameter |
stats bool + timeout |
helpers.py |
Single 1,970-line file | helpers/ subpackage (all imports preserved) |
| All sentinels | Exported from helpers | Removed — use None or required positional args |
get_gac_brackets(limit=) |
Sentinel default | int default 0 (0 = no limit) |
| GAC bracket scanning | Linear O(n) | Exponential probe + binary search O(log n) |
| Migration checker | Not available | swgoh-migrate CLI / python -m swgoh_comlink.migrate |
DataItems.SEGMENT2 value |
68717379584 |
1125968624222208 (server-aligned) |
DataItems.SEGMENT4 value |
281200098803712 |
7881024469991424 (server-aligned) |
get_game_data(items=) values |
Any bitmask | Any bitmask from 1 to 253 − 1, or -1 |