fix: align rest_proxy with schwab-py 1.5.1 enum API + pin dependency#1
Merged
Conversation
The /v1 endpoints were written against an older schwab-py enum API than the
unpinned build installed (schwab-py 1.5.1), causing live 502s:
- quotes: AttributeError "Quote has no attribute FIELD_QUOTE"
- pricehistory: ValueError "expected type Period, got type int"
Root cause: requirements.txt left schwab-py unpinned, so the API drifted.
Changes
- New app/schwab_data_proxy/enum_mapping.py: centralized, unit-testable param
-> schwab-py enum mappers. Accepts member name OR wire value; raises
UnknownEnumValue (surfaced as HTTP 400) on unmappable input instead of
passing raw strings/ints downstream.
- rest_proxy.py rewired for all four endpoints:
* quotes: Quote.FIELD_* -> Quote.Fields.*
* pricehistory: raw int period/frequency -> PriceHistory.Period/.Frequency;
period_type/frequency_type -> typed enums
* chains: Options.* enums; FIX kwarg range= -> strike_range= (the old
name was rejected by get_option_chain); entitlement now mapped
* markets: MarketHours.Market enums
* UnknownEnumValue -> 400 (bad query string), not 502 (upstream)
- requirements.txt: pin schwab-py==1.5.1 (with rationale comment) so the enum
API can't silently drift again.
- tests/: pytest regression suite (19 tests) covering the param->enum mapping
and handler-level call construction (mocked client, no network).
- CI: add pytest job; requirements-dev.txt + pyproject.toml pytest config.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
requirements.txtleft schwab-py unpinned, so the Docker build pulled a newer enum API thanrest_proxy.pywas written against. The installed version is schwab-py 1.5.1. Two live 502s resulted (greeksmith → proxy @ 127.0.0.1:8080):GET /v1/quotes?fields=quote→type object 'Quote' has no attribute 'FIELD_QUOTE'GET /v1/pricehistory?...frequency_type=daily&period=1→expected type "Period", got type "int"Audited all four
/v1endpoints; chains and markets were also wrong (chains used the droppedrange=kwarg + an unmappedentitlement; markets relied on name-only enum lookup).API corrections (old → new), validated against schwab-py 1.5.1
client.Quote.FIELD_QUOTE/FIELD_REFERENCE/FIELD_EXTENDED/FIELD_FUNDAMENTAL/FIELD_REGULARclient.Quote.Fields.QUOTE/.REFERENCE/.EXTENDED/.FUNDAMENTAL/.REGULARintforperiod,frequencyPriceHistory.Period(int),PriceHistory.Frequency(int)(typed enums; both int-valued)period_type/frequency_typebest-effort name lookupPeriodType/FrequencyType(by valueday/dailyor by name)kwargs["range"] = ...kwargs["strike_range"] = ...(get_option_chain renamed the kwarg;range=was rejected)entitlementpassed through as raw strOptions.EntitlementOptions.ContractType/Strategy/StrikeRange/ExpirationMonth/Typevia[NAME]onlyITM)MarketHours.Market[NAME]onlyequityor nameEQUITY)Fix design
app/schwab_data_proxy/enum_mapping.py: centralized, unit-testable mappers. Each accepts the live/mocked client (reads enum classes off it), accepts member name or wire value, and raisesUnknownEnumValueon bad input.rest_proxy.py: all four endpoints rewired to the mappers.UnknownEnumValuenow surfaces as HTTP 400 (bad query string) instead of an opaque 502.enforce_enumsat the schwab-py default (on) and passed correct typed enums — that's the intended usage for 1.5.1 and keeps bad params loud rather than silently shipping wrong types upstream.Dependency pin
requirements.txt:schwab-py==1.5.1(with rationale comment) so the enum API can't silently drift again.Tests / CI
tests/pytest suite (19 tests): param→enum mapping + handler-level call construction with a mocked client (no network/auth). Asserts correct enum types and kwarg names, and that bad params → 400.testjob (pytest) +requirements-dev.txt+pyproject.tomlpytest config.ruff check app/+ruff format --check app/clean.Deploy note
This is a code-only change. The running proxy container must be rebuilt/restarted by the operator for the fix to take effect (so greeksmith's data smoke can pass). No container was rebuilt or deployed in this PR. Do not merge until reviewed.