[{"data":1,"prerenderedAt":3655},["ShallowReactive",2],{"page-\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fbuilding-an-api-client-cli-with-httpx\u002F":3,"content-directory":3108},{"id":4,"title":5,"body":6,"date":3095,"description":3096,"difficulty":3097,"draft":3098,"extension":3099,"meta":3100,"navigation":153,"path":3101,"seo":3102,"stem":3103,"tags":3104,"updated":3095,"__hash__":3107},"content\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fbuilding-an-api-client-cli-with-httpx\u002Findex.md","Building an API Client CLI with httpx",{"type":7,"value":8,"toc":3079},"minimark",[9,28,33,81,85,104,108,112,1358,1361,1385,1411,1429,1432,1436,2148,2166,2170,2173,2230,2234,2240,2961,2976,2980,2996,3000,3005,3012,3016,3019,3023,3030,3034,3041,3045,3075],[10,11,12,13,17,18,21,22,27],"p",{},"You want a command-line front end for a web API — your company's deployment service, an issue tracker, a metrics backend — that the team can use from a terminal and from scripts. The goal is not just \"it makes requests\" but a tool that stays pleasant as it grows: commands that read like the domain, errors that tell people what to do, output that is readable for humans and stable for ",[14,15,16],"code",{},"jq",", and tests that run offline in milliseconds. This guide builds that tool end to end with ",[14,19,20],{},"httpx"," and Typer, using a small projects API as the running example. It is the hands-on companion to ",[23,24,26],"a",{"href":25},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002F","calling HTTP APIs from Python CLIs",".",[29,30,32],"h2",{"id":31},"prerequisites","Prerequisites",[34,35,36,55,70],"ul",{},[37,38,39,40,42,43,46,47,50,51,54],"li",{},"Python 3.10+, ",[14,41,20],{}," 0.27 or newer, ",[14,44,45],{},"typer"," and ",[14,48,49],{},"rich"," (",[14,52,53],{},"uv add httpx typer rich",").",[37,56,57,58,61,62,65,66,69],{},"An API to talk to. The examples assume ",[14,59,60],{},"GET \u002Fprojects"," returns a JSON list of ",[14,63,64],{},"{\"name\", \"owner\", \"build_count\"}"," objects and ",[14,67,68],{},"GET \u002Fprojects\u002F{name}"," returns one.",[37,71,72,73,76,77,27],{},"An API token in the ",[14,74,75],{},"MYTOOL_TOKEN"," environment variable. Storing it properly is covered in ",[23,78,80],{"href":79},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Fstoring-tokens-with-keyring\u002F","storing tokens with keyring",[29,82,84],{"id":83},"the-shape-of-the-tool","The shape of the tool",[10,86,87,88,91,92,95,96,99,100,103],{},"Three modules, each with one job. ",[14,89,90],{},"api.py"," knows HTTP and the API's JSON shapes and returns typed objects. ",[14,93,94],{},"cli.py"," knows arguments and output formats. A small ",[14,97,98],{},"settings.py"," (folded into the CLI callback here) knows where the base URL and token come from. The command asks the API module for objects; the API module reuses a single ",[14,101,102],{},"httpx.Client"," for every request in the run.",[105,106],"inline-diagram",{"name":107},"http-request-sequence",[29,109,111],{"id":110},"the-recipe-the-api-module","The recipe: the API module",[113,114,119],"pre",{"className":115,"code":116,"language":117,"meta":118,"style":118},"language-python shiki shiki-themes github-light github-dark","# src\u002Fmytool\u002Fapi.py\nfrom __future__ import annotations\n\nfrom dataclasses import asdict, dataclass\nfrom typing import Any\nfrom urllib.parse import quote\n\nimport httpx\n\n__version__ = \"1.4.0\"\n\nEXIT_USAGE = 2\nEXIT_AUTH = 4\nEXIT_UNAVAILABLE = 69   # EX_UNAVAILABLE from sysexits.h\n\n\n@dataclass(frozen=True)\nclass Project:\n    name: str\n    owner: str\n    builds: int\n\n    def to_json(self) -> dict[str, Any]:\n        return asdict(self)\n\n\nclass ApiError(Exception):\n    def __init__(self, message: str, exit_code: int = 1) -> None:\n        super().__init__(message)\n        self.exit_code = exit_code\n\n\ndef make_client(base_url: str, token: str | None, *,\n                transport: httpx.BaseTransport | None = None) -> httpx.Client:\n    headers = {\"User-Agent\": f\"mytool\u002F{__version__}\", \"Accept\": \"application\u002Fjson\"}\n    if token:\n        headers[\"Authorization\"] = f\"Bearer {token}\"\n    return httpx.Client(\n        base_url=base_url,\n        headers=headers,\n        timeout=httpx.Timeout(30.0, connect=5.0),\n        transport=transport or httpx.HTTPTransport(retries=2),\n    )\n\n\nclass ProjectsApi:\n    def __init__(self, client: httpx.Client) -> None:\n        self._client = client\n\n    def _get(self, path: str, **params: Any) -> Any:\n        try:\n            response = self._client.get(path, params=params or None)\n        except httpx.TimeoutException:\n            raise ApiError(f\"{self._client.base_url.host} did not respond in time\",\n                           EXIT_UNAVAILABLE) from None\n        except httpx.TransportError as exc:\n            raise ApiError(f\"cannot reach {self._client.base_url.host}: {exc}\",\n                           EXIT_UNAVAILABLE) from None\n        return self._check(response)\n\n    @staticmethod\n    def _check(response: httpx.Response) -> Any:\n        if response.is_success:\n            return response.json()\n        code = response.status_code\n        detail = \"\"\n        if \"json\" in response.headers.get(\"content-type\", \"\"):\n            detail = response.json().get(\"message\", \"\")\n        if code in (401, 403):\n            raise ApiError(\"not authorised — set MYTOOL_TOKEN or run: mytool login\", EXIT_AUTH)\n        if code == 404:\n            raise ApiError(detail or \"not found\")\n        if code == 422:\n            raise ApiError(detail or \"the server rejected the request\", EXIT_USAGE)\n        if code >= 500:\n            raise ApiError(f\"the service returned {code}; try again shortly\", EXIT_UNAVAILABLE)\n        raise ApiError(f\"unexpected response {code}: {detail}\".rstrip(\": \"))\n\n    def list_projects(self) -> list[Project]:\n        return [Project(p[\"name\"], p[\"owner\"], p[\"build_count\"]) for p in self._get(\"\u002Fprojects\")]\n\n    def get_project(self, name: str) -> Project:\n        p = self._get(f\"\u002Fprojects\u002F{quote(name, safe='')}\")   # \"a\u002Fb\" must not become a path\n        return Project(p[\"name\"], p[\"owner\"], p[\"build_count\"])\n","python","",[14,120,121,130,148,155,169,182,195,200,208,213,226,231,242,253,267,272,277,300,312,321,329,338,343,361,375,380,385,401,433,448,462,467,472,506,524,566,575,607,616,627,638,665,692,698,703,708,718,732,745,750,771,779,807,816,842,856,870,901,912,922,927,936,947,956,965,976,987,1011,1031,1054,1070,1085,1100,1114,1132,1147,1174,1213,1218,1229,1273,1278,1294,1337],{"__ignoreMap":118},[122,123,126],"span",{"class":124,"line":125},"line",1,[122,127,129],{"class":128},"sJ8bj","# src\u002Fmytool\u002Fapi.py\n",[122,131,133,137,141,144],{"class":124,"line":132},2,[122,134,136],{"class":135},"szBVR","from",[122,138,140],{"class":139},"sj4cs"," __future__",[122,142,143],{"class":135}," import",[122,145,147],{"class":146},"sVt8B"," annotations\n",[122,149,151],{"class":124,"line":150},3,[122,152,154],{"emptyLinePlaceholder":153},true,"\n",[122,156,158,160,163,166],{"class":124,"line":157},4,[122,159,136],{"class":135},[122,161,162],{"class":146}," dataclasses ",[122,164,165],{"class":135},"import",[122,167,168],{"class":146}," asdict, dataclass\n",[122,170,172,174,177,179],{"class":124,"line":171},5,[122,173,136],{"class":135},[122,175,176],{"class":146}," typing ",[122,178,165],{"class":135},[122,180,181],{"class":146}," Any\n",[122,183,185,187,190,192],{"class":124,"line":184},6,[122,186,136],{"class":135},[122,188,189],{"class":146}," urllib.parse ",[122,191,165],{"class":135},[122,193,194],{"class":146}," quote\n",[122,196,198],{"class":124,"line":197},7,[122,199,154],{"emptyLinePlaceholder":153},[122,201,203,205],{"class":124,"line":202},8,[122,204,165],{"class":135},[122,206,207],{"class":146}," httpx\n",[122,209,211],{"class":124,"line":210},9,[122,212,154],{"emptyLinePlaceholder":153},[122,214,216,219,222],{"class":124,"line":215},10,[122,217,218],{"class":139},"__version__",[122,220,221],{"class":135}," =",[122,223,225],{"class":224},"sZZnC"," \"1.4.0\"\n",[122,227,229],{"class":124,"line":228},11,[122,230,154],{"emptyLinePlaceholder":153},[122,232,234,237,239],{"class":124,"line":233},12,[122,235,236],{"class":139},"EXIT_USAGE",[122,238,221],{"class":135},[122,240,241],{"class":139}," 2\n",[122,243,245,248,250],{"class":124,"line":244},13,[122,246,247],{"class":139},"EXIT_AUTH",[122,249,221],{"class":135},[122,251,252],{"class":139}," 4\n",[122,254,256,259,261,264],{"class":124,"line":255},14,[122,257,258],{"class":139},"EXIT_UNAVAILABLE",[122,260,221],{"class":135},[122,262,263],{"class":139}," 69",[122,265,266],{"class":128},"   # EX_UNAVAILABLE from sysexits.h\n",[122,268,270],{"class":124,"line":269},15,[122,271,154],{"emptyLinePlaceholder":153},[122,273,275],{"class":124,"line":274},16,[122,276,154],{"emptyLinePlaceholder":153},[122,278,280,284,287,291,294,297],{"class":124,"line":279},17,[122,281,283],{"class":282},"sScJk","@dataclass",[122,285,286],{"class":146},"(",[122,288,290],{"class":289},"s4XuR","frozen",[122,292,293],{"class":135},"=",[122,295,296],{"class":139},"True",[122,298,299],{"class":146},")\n",[122,301,303,306,309],{"class":124,"line":302},18,[122,304,305],{"class":135},"class",[122,307,308],{"class":282}," Project",[122,310,311],{"class":146},":\n",[122,313,315,318],{"class":124,"line":314},19,[122,316,317],{"class":146},"    name: ",[122,319,320],{"class":139},"str\n",[122,322,324,327],{"class":124,"line":323},20,[122,325,326],{"class":146},"    owner: ",[122,328,320],{"class":139},[122,330,332,335],{"class":124,"line":331},21,[122,333,334],{"class":146},"    builds: ",[122,336,337],{"class":139},"int\n",[122,339,341],{"class":124,"line":340},22,[122,342,154],{"emptyLinePlaceholder":153},[122,344,346,349,352,355,358],{"class":124,"line":345},23,[122,347,348],{"class":135},"    def",[122,350,351],{"class":282}," to_json",[122,353,354],{"class":146},"(self) -> dict[",[122,356,357],{"class":139},"str",[122,359,360],{"class":146},", Any]:\n",[122,362,364,367,370,373],{"class":124,"line":363},24,[122,365,366],{"class":135},"        return",[122,368,369],{"class":146}," asdict(",[122,371,372],{"class":139},"self",[122,374,299],{"class":146},[122,376,378],{"class":124,"line":377},25,[122,379,154],{"emptyLinePlaceholder":153},[122,381,383],{"class":124,"line":382},26,[122,384,154],{"emptyLinePlaceholder":153},[122,386,388,390,393,395,398],{"class":124,"line":387},27,[122,389,305],{"class":135},[122,391,392],{"class":282}," ApiError",[122,394,286],{"class":146},[122,396,397],{"class":139},"Exception",[122,399,400],{"class":146},"):\n",[122,402,404,406,409,412,414,417,420,422,425,428,431],{"class":124,"line":403},28,[122,405,348],{"class":135},[122,407,408],{"class":139}," __init__",[122,410,411],{"class":146},"(self, message: ",[122,413,357],{"class":139},[122,415,416],{"class":146},", exit_code: ",[122,418,419],{"class":139},"int",[122,421,221],{"class":135},[122,423,424],{"class":139}," 1",[122,426,427],{"class":146},") -> ",[122,429,430],{"class":139},"None",[122,432,311],{"class":146},[122,434,436,439,442,445],{"class":124,"line":435},29,[122,437,438],{"class":139},"        super",[122,440,441],{"class":146},"().",[122,443,444],{"class":139},"__init__",[122,446,447],{"class":146},"(message)\n",[122,449,451,454,457,459],{"class":124,"line":450},30,[122,452,453],{"class":139},"        self",[122,455,456],{"class":146},".exit_code ",[122,458,293],{"class":135},[122,460,461],{"class":146}," exit_code\n",[122,463,465],{"class":124,"line":464},31,[122,466,154],{"emptyLinePlaceholder":153},[122,468,470],{"class":124,"line":469},32,[122,471,154],{"emptyLinePlaceholder":153},[122,473,475,478,481,484,486,489,491,494,497,500,503],{"class":124,"line":474},33,[122,476,477],{"class":135},"def",[122,479,480],{"class":282}," make_client",[122,482,483],{"class":146},"(base_url: ",[122,485,357],{"class":139},[122,487,488],{"class":146},", token: ",[122,490,357],{"class":139},[122,492,493],{"class":135}," |",[122,495,496],{"class":139}," None",[122,498,499],{"class":146},", ",[122,501,502],{"class":135},"*",[122,504,505],{"class":146},",\n",[122,507,509,512,515,517,519,521],{"class":124,"line":508},34,[122,510,511],{"class":146},"                transport: httpx.BaseTransport ",[122,513,514],{"class":135},"|",[122,516,496],{"class":139},[122,518,221],{"class":135},[122,520,496],{"class":139},[122,522,523],{"class":146},") -> httpx.Client:\n",[122,525,527,530,532,535,538,541,544,547,550,553,555,558,560,563],{"class":124,"line":526},35,[122,528,529],{"class":146},"    headers ",[122,531,293],{"class":135},[122,533,534],{"class":146}," {",[122,536,537],{"class":224},"\"User-Agent\"",[122,539,540],{"class":146},": ",[122,542,543],{"class":135},"f",[122,545,546],{"class":224},"\"mytool\u002F",[122,548,549],{"class":139},"{__version__}",[122,551,552],{"class":224},"\"",[122,554,499],{"class":146},[122,556,557],{"class":224},"\"Accept\"",[122,559,540],{"class":146},[122,561,562],{"class":224},"\"application\u002Fjson\"",[122,564,565],{"class":146},"}\n",[122,567,569,572],{"class":124,"line":568},36,[122,570,571],{"class":135},"    if",[122,573,574],{"class":146}," token:\n",[122,576,578,581,584,587,589,592,595,598,601,604],{"class":124,"line":577},37,[122,579,580],{"class":146},"        headers[",[122,582,583],{"class":224},"\"Authorization\"",[122,585,586],{"class":146},"] ",[122,588,293],{"class":135},[122,590,591],{"class":135}," f",[122,593,594],{"class":224},"\"Bearer ",[122,596,597],{"class":139},"{",[122,599,600],{"class":146},"token",[122,602,603],{"class":139},"}",[122,605,606],{"class":224},"\"\n",[122,608,610,613],{"class":124,"line":609},38,[122,611,612],{"class":135},"    return",[122,614,615],{"class":146}," httpx.Client(\n",[122,617,619,622,624],{"class":124,"line":618},39,[122,620,621],{"class":289},"        base_url",[122,623,293],{"class":135},[122,625,626],{"class":146},"base_url,\n",[122,628,630,633,635],{"class":124,"line":629},40,[122,631,632],{"class":289},"        headers",[122,634,293],{"class":135},[122,636,637],{"class":146},"headers,\n",[122,639,641,644,646,649,652,654,657,659,662],{"class":124,"line":640},41,[122,642,643],{"class":289},"        timeout",[122,645,293],{"class":135},[122,647,648],{"class":146},"httpx.Timeout(",[122,650,651],{"class":139},"30.0",[122,653,499],{"class":146},[122,655,656],{"class":289},"connect",[122,658,293],{"class":135},[122,660,661],{"class":139},"5.0",[122,663,664],{"class":146},"),\n",[122,666,668,671,673,676,679,682,685,687,690],{"class":124,"line":667},42,[122,669,670],{"class":289},"        transport",[122,672,293],{"class":135},[122,674,675],{"class":146},"transport ",[122,677,678],{"class":135},"or",[122,680,681],{"class":146}," httpx.HTTPTransport(",[122,683,684],{"class":289},"retries",[122,686,293],{"class":135},[122,688,689],{"class":139},"2",[122,691,664],{"class":146},[122,693,695],{"class":124,"line":694},43,[122,696,697],{"class":146},"    )\n",[122,699,701],{"class":124,"line":700},44,[122,702,154],{"emptyLinePlaceholder":153},[122,704,706],{"class":124,"line":705},45,[122,707,154],{"emptyLinePlaceholder":153},[122,709,711,713,716],{"class":124,"line":710},46,[122,712,305],{"class":135},[122,714,715],{"class":282}," ProjectsApi",[122,717,311],{"class":146},[122,719,721,723,725,728,730],{"class":124,"line":720},47,[122,722,348],{"class":135},[122,724,408],{"class":139},[122,726,727],{"class":146},"(self, client: httpx.Client) -> ",[122,729,430],{"class":139},[122,731,311],{"class":146},[122,733,735,737,740,742],{"class":124,"line":734},48,[122,736,453],{"class":139},[122,738,739],{"class":146},"._client ",[122,741,293],{"class":135},[122,743,744],{"class":146}," client\n",[122,746,748],{"class":124,"line":747},49,[122,749,154],{"emptyLinePlaceholder":153},[122,751,753,755,758,761,763,765,768],{"class":124,"line":752},50,[122,754,348],{"class":135},[122,756,757],{"class":282}," _get",[122,759,760],{"class":146},"(self, path: ",[122,762,357],{"class":139},[122,764,499],{"class":146},[122,766,767],{"class":135},"**",[122,769,770],{"class":146},"params: Any) -> Any:\n",[122,772,774,777],{"class":124,"line":773},51,[122,775,776],{"class":135},"        try",[122,778,311],{"class":146},[122,780,782,785,787,790,793,796,798,801,803,805],{"class":124,"line":781},52,[122,783,784],{"class":146},"            response ",[122,786,293],{"class":135},[122,788,789],{"class":139}," self",[122,791,792],{"class":146},"._client.get(path, ",[122,794,795],{"class":289},"params",[122,797,293],{"class":135},[122,799,800],{"class":146},"params ",[122,802,678],{"class":135},[122,804,496],{"class":139},[122,806,299],{"class":146},[122,808,810,813],{"class":124,"line":809},53,[122,811,812],{"class":135},"        except",[122,814,815],{"class":146}," httpx.TimeoutException:\n",[122,817,819,822,825,827,829,832,835,837,840],{"class":124,"line":818},54,[122,820,821],{"class":135},"            raise",[122,823,824],{"class":146}," ApiError(",[122,826,543],{"class":135},[122,828,552],{"class":224},[122,830,831],{"class":139},"{self",[122,833,834],{"class":146},"._client.base_url.host",[122,836,603],{"class":139},[122,838,839],{"class":224}," did not respond in time\"",[122,841,505],{"class":146},[122,843,845,848,851,853],{"class":124,"line":844},55,[122,846,847],{"class":139},"                           EXIT_UNAVAILABLE",[122,849,850],{"class":146},") ",[122,852,136],{"class":135},[122,854,855],{"class":139}," None\n",[122,857,859,861,864,867],{"class":124,"line":858},56,[122,860,812],{"class":135},[122,862,863],{"class":146}," httpx.TransportError ",[122,865,866],{"class":135},"as",[122,868,869],{"class":146}," exc:\n",[122,871,873,875,877,879,882,884,886,888,890,892,895,897,899],{"class":124,"line":872},57,[122,874,821],{"class":135},[122,876,824],{"class":146},[122,878,543],{"class":135},[122,880,881],{"class":224},"\"cannot reach ",[122,883,831],{"class":139},[122,885,834],{"class":146},[122,887,603],{"class":139},[122,889,540],{"class":224},[122,891,597],{"class":139},[122,893,894],{"class":146},"exc",[122,896,603],{"class":139},[122,898,552],{"class":224},[122,900,505],{"class":146},[122,902,904,906,908,910],{"class":124,"line":903},58,[122,905,847],{"class":139},[122,907,850],{"class":146},[122,909,136],{"class":135},[122,911,855],{"class":139},[122,913,915,917,919],{"class":124,"line":914},59,[122,916,366],{"class":135},[122,918,789],{"class":139},[122,920,921],{"class":146},"._check(response)\n",[122,923,925],{"class":124,"line":924},60,[122,926,154],{"emptyLinePlaceholder":153},[122,928,930,933],{"class":124,"line":929},61,[122,931,932],{"class":282},"    @",[122,934,935],{"class":139},"staticmethod\n",[122,937,939,941,944],{"class":124,"line":938},62,[122,940,348],{"class":135},[122,942,943],{"class":282}," _check",[122,945,946],{"class":146},"(response: httpx.Response) -> Any:\n",[122,948,950,953],{"class":124,"line":949},63,[122,951,952],{"class":135},"        if",[122,954,955],{"class":146}," response.is_success:\n",[122,957,959,962],{"class":124,"line":958},64,[122,960,961],{"class":135},"            return",[122,963,964],{"class":146}," response.json()\n",[122,966,968,971,973],{"class":124,"line":967},65,[122,969,970],{"class":146},"        code ",[122,972,293],{"class":135},[122,974,975],{"class":146}," response.status_code\n",[122,977,979,982,984],{"class":124,"line":978},66,[122,980,981],{"class":146},"        detail ",[122,983,293],{"class":135},[122,985,986],{"class":224}," \"\"\n",[122,988,990,992,995,998,1001,1004,1006,1009],{"class":124,"line":989},67,[122,991,952],{"class":135},[122,993,994],{"class":224}," \"json\"",[122,996,997],{"class":135}," in",[122,999,1000],{"class":146}," response.headers.get(",[122,1002,1003],{"class":224},"\"content-type\"",[122,1005,499],{"class":146},[122,1007,1008],{"class":224},"\"\"",[122,1010,400],{"class":146},[122,1012,1014,1017,1019,1022,1025,1027,1029],{"class":124,"line":1013},68,[122,1015,1016],{"class":146},"            detail ",[122,1018,293],{"class":135},[122,1020,1021],{"class":146}," response.json().get(",[122,1023,1024],{"class":224},"\"message\"",[122,1026,499],{"class":146},[122,1028,1008],{"class":224},[122,1030,299],{"class":146},[122,1032,1034,1036,1039,1042,1044,1047,1049,1052],{"class":124,"line":1033},69,[122,1035,952],{"class":135},[122,1037,1038],{"class":146}," code ",[122,1040,1041],{"class":135},"in",[122,1043,50],{"class":146},[122,1045,1046],{"class":139},"401",[122,1048,499],{"class":146},[122,1050,1051],{"class":139},"403",[122,1053,400],{"class":146},[122,1055,1057,1059,1061,1064,1066,1068],{"class":124,"line":1056},70,[122,1058,821],{"class":135},[122,1060,824],{"class":146},[122,1062,1063],{"class":224},"\"not authorised — set MYTOOL_TOKEN or run: mytool login\"",[122,1065,499],{"class":146},[122,1067,247],{"class":139},[122,1069,299],{"class":146},[122,1071,1073,1075,1077,1080,1083],{"class":124,"line":1072},71,[122,1074,952],{"class":135},[122,1076,1038],{"class":146},[122,1078,1079],{"class":135},"==",[122,1081,1082],{"class":139}," 404",[122,1084,311],{"class":146},[122,1086,1088,1090,1093,1095,1098],{"class":124,"line":1087},72,[122,1089,821],{"class":135},[122,1091,1092],{"class":146}," ApiError(detail ",[122,1094,678],{"class":135},[122,1096,1097],{"class":224}," \"not found\"",[122,1099,299],{"class":146},[122,1101,1103,1105,1107,1109,1112],{"class":124,"line":1102},73,[122,1104,952],{"class":135},[122,1106,1038],{"class":146},[122,1108,1079],{"class":135},[122,1110,1111],{"class":139}," 422",[122,1113,311],{"class":146},[122,1115,1117,1119,1121,1123,1126,1128,1130],{"class":124,"line":1116},74,[122,1118,821],{"class":135},[122,1120,1092],{"class":146},[122,1122,678],{"class":135},[122,1124,1125],{"class":224}," \"the server rejected the request\"",[122,1127,499],{"class":146},[122,1129,236],{"class":139},[122,1131,299],{"class":146},[122,1133,1135,1137,1139,1142,1145],{"class":124,"line":1134},75,[122,1136,952],{"class":135},[122,1138,1038],{"class":146},[122,1140,1141],{"class":135},">=",[122,1143,1144],{"class":139}," 500",[122,1146,311],{"class":146},[122,1148,1150,1152,1154,1156,1159,1161,1163,1165,1168,1170,1172],{"class":124,"line":1149},76,[122,1151,821],{"class":135},[122,1153,824],{"class":146},[122,1155,543],{"class":135},[122,1157,1158],{"class":224},"\"the service returned ",[122,1160,597],{"class":139},[122,1162,14],{"class":146},[122,1164,603],{"class":139},[122,1166,1167],{"class":224},"; try again shortly\"",[122,1169,499],{"class":146},[122,1171,258],{"class":139},[122,1173,299],{"class":146},[122,1175,1177,1180,1182,1184,1187,1189,1191,1193,1195,1197,1200,1202,1204,1207,1210],{"class":124,"line":1176},77,[122,1178,1179],{"class":135},"        raise",[122,1181,824],{"class":146},[122,1183,543],{"class":135},[122,1185,1186],{"class":224},"\"unexpected response ",[122,1188,597],{"class":139},[122,1190,14],{"class":146},[122,1192,603],{"class":139},[122,1194,540],{"class":224},[122,1196,597],{"class":139},[122,1198,1199],{"class":146},"detail",[122,1201,603],{"class":139},[122,1203,552],{"class":224},[122,1205,1206],{"class":146},".rstrip(",[122,1208,1209],{"class":224},"\": \"",[122,1211,1212],{"class":146},"))\n",[122,1214,1216],{"class":124,"line":1215},78,[122,1217,154],{"emptyLinePlaceholder":153},[122,1219,1221,1223,1226],{"class":124,"line":1220},79,[122,1222,348],{"class":135},[122,1224,1225],{"class":282}," list_projects",[122,1227,1228],{"class":146},"(self) -> list[Project]:\n",[122,1230,1232,1234,1237,1240,1243,1246,1248,1251,1254,1257,1260,1262,1264,1267,1270],{"class":124,"line":1231},80,[122,1233,366],{"class":135},[122,1235,1236],{"class":146}," [Project(p[",[122,1238,1239],{"class":224},"\"name\"",[122,1241,1242],{"class":146},"], p[",[122,1244,1245],{"class":224},"\"owner\"",[122,1247,1242],{"class":146},[122,1249,1250],{"class":224},"\"build_count\"",[122,1252,1253],{"class":146},"]) ",[122,1255,1256],{"class":135},"for",[122,1258,1259],{"class":146}," p ",[122,1261,1041],{"class":135},[122,1263,789],{"class":139},[122,1265,1266],{"class":146},"._get(",[122,1268,1269],{"class":224},"\"\u002Fprojects\"",[122,1271,1272],{"class":146},")]\n",[122,1274,1276],{"class":124,"line":1275},81,[122,1277,154],{"emptyLinePlaceholder":153},[122,1279,1281,1283,1286,1289,1291],{"class":124,"line":1280},82,[122,1282,348],{"class":135},[122,1284,1285],{"class":282}," get_project",[122,1287,1288],{"class":146},"(self, name: ",[122,1290,357],{"class":139},[122,1292,1293],{"class":146},") -> Project:\n",[122,1295,1297,1300,1302,1304,1306,1308,1311,1313,1316,1319,1321,1324,1327,1329,1331,1334],{"class":124,"line":1296},83,[122,1298,1299],{"class":146},"        p ",[122,1301,293],{"class":135},[122,1303,789],{"class":139},[122,1305,1266],{"class":146},[122,1307,543],{"class":135},[122,1309,1310],{"class":224},"\"\u002Fprojects\u002F",[122,1312,597],{"class":139},[122,1314,1315],{"class":146},"quote(name, ",[122,1317,1318],{"class":289},"safe",[122,1320,293],{"class":135},[122,1322,1323],{"class":224},"''",[122,1325,1326],{"class":146},")",[122,1328,603],{"class":139},[122,1330,552],{"class":224},[122,1332,1333],{"class":146},")   ",[122,1335,1336],{"class":128},"# \"a\u002Fb\" must not become a path\n",[122,1338,1340,1342,1345,1347,1349,1351,1353,1355],{"class":124,"line":1339},84,[122,1341,366],{"class":135},[122,1343,1344],{"class":146}," Project(p[",[122,1346,1239],{"class":224},[122,1348,1242],{"class":146},[122,1350,1245],{"class":224},[122,1352,1242],{"class":146},[122,1354,1250],{"class":224},[122,1356,1357],{"class":146},"])\n",[10,1359,1360],{},"Three decisions are doing most of the work.",[10,1362,1363,1367,1368,1371,1372,1375,1376,1380,1381,1384],{},[1364,1365,1366],"strong",{},"One error type with an exit code."," Every failure — network, timeout, HTTP status — becomes ",[14,1369,1370],{},"ApiError",", carrying the message the user should see and the exit code the process should use. The command layer then needs exactly one ",[14,1373,1374],{},"except"," clause. The codes follow the conventions in ",[23,1377,1379],{"href":1378},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002Fchoosing-exit-codes-for-cli-tools\u002F","choosing exit codes for CLI tools",": 2 for bad input, 69 (",[14,1382,1383],{},"EX_UNAVAILABLE",") when the service cannot be reached, and a distinct code for authentication so scripts can detect \"needs login\".",[10,1386,1387,1390,1391,1394,1395,1398,1399,1401,1402,1405,1406,1410],{},[1364,1388,1389],{},"Typed objects out."," Commands receive ",[14,1392,1393],{},"Project"," instances, never response dictionaries. When the API renames ",[14,1396,1397],{},"build_count",", one line in ",[14,1400,90],{}," changes and every command keeps working. It also means your ",[14,1403,1404],{},"--json"," output is ",[1407,1408,1409],"em",{},"your"," schema, not a pass-through of the server's, so you control its stability.",[10,1412,1413,1416,1417,1420,1421,1424,1425,1428],{},[1364,1414,1415],{},"An injectable transport."," ",[14,1418,1419],{},"make_client"," accepts an optional ",[14,1422,1423],{},"transport",". Production code passes nothing and gets real HTTP with connection retries; tests pass ",[14,1426,1427],{},"httpx.MockTransport"," and get no network at all.",[105,1430],{"name":1431},"http-error-map",[29,1433,1435],{"id":1434},"the-recipe-the-command-layer","The recipe: the command layer",[113,1437,1439],{"className":115,"code":1438,"language":117,"meta":118,"style":118},"# src\u002Fmytool\u002Fcli.py\nfrom __future__ import annotations\n\nimport json\nimport os\nfrom collections.abc import Callable\n\nimport httpx\nimport typer\nfrom rich.console import Console\nfrom rich.table import Table\n\nfrom mytool.api import ApiError, ProjectsApi, make_client\n\napp = typer.Typer(no_args_is_help=True)\nerr = Console(stderr=True)\n\n# Tests replace this factory to inject a mock transport.\nclient_factory: Callable[[str, str | None], httpx.Client] = make_client\n\n\n@app.callback()\ndef main(\n    ctx: typer.Context,\n    api_url: str = typer.Option(\"https:\u002F\u002Fapi.example.com\u002Fv1\", envvar=\"MYTOOL_API_URL\"),\n) -> None:\n    \"\"\"Command-line access to the projects API.\"\"\"\n    client = client_factory(api_url, os.environ.get(\"MYTOOL_TOKEN\"))\n    ctx.call_on_close(client.close)\n    ctx.obj = ProjectsApi(client)\n\n\ndef fail(exc: ApiError) -> None:\n    err.print(f\"[red]error:[\u002Fred] {exc}\")\n    raise typer.Exit(exc.exit_code)\n\n\n@app.command()\ndef projects(ctx: typer.Context, as_json: bool = typer.Option(False, \"--json\")) -> None:\n    \"\"\"List projects.\"\"\"\n    try:\n        items = ctx.obj.list_projects()\n    except ApiError as exc:\n        fail(exc)\n    if as_json:\n        typer.echo(json.dumps([p.to_json() for p in items], indent=2))\n        return\n    table = Table(\"NAME\", \"OWNER\", \"BUILDS\", box=None, header_style=\"bold\")\n    for p in sorted(items, key=lambda p: p.name):\n        table.add_row(p.name, p.owner, str(p.builds))\n    Console().print(table)\n\n\n@app.command()\ndef show(ctx: typer.Context, name: str, as_json: bool = typer.Option(False, \"--json\")) -> None:\n    \"\"\"Show one project.\"\"\"\n    try:\n        p = ctx.obj.get_project(name)\n    except ApiError as exc:\n        fail(exc)\n    if as_json:\n        typer.echo(json.dumps(p.to_json(), indent=2))\n    else:\n        typer.echo(f\"{p.name}  owner={p.owner}  builds={p.builds}\")\n\n\nif __name__ == \"__main__\":\n    app()\n",[14,1440,1441,1446,1456,1460,1467,1474,1486,1490,1496,1503,1515,1527,1531,1543,1547,1566,1585,1589,1594,1617,1621,1625,1633,1643,1648,1675,1683,1688,1703,1708,1718,1722,1726,1740,1760,1768,1772,1776,1783,1815,1820,1827,1837,1849,1854,1861,1884,1889,1933,1957,1967,1972,1976,1980,1986,2019,2024,2030,2039,2049,2053,2059,2072,2079,2119,2123,2127,2143],{"__ignoreMap":118},[122,1442,1443],{"class":124,"line":125},[122,1444,1445],{"class":128},"# src\u002Fmytool\u002Fcli.py\n",[122,1447,1448,1450,1452,1454],{"class":124,"line":132},[122,1449,136],{"class":135},[122,1451,140],{"class":139},[122,1453,143],{"class":135},[122,1455,147],{"class":146},[122,1457,1458],{"class":124,"line":150},[122,1459,154],{"emptyLinePlaceholder":153},[122,1461,1462,1464],{"class":124,"line":157},[122,1463,165],{"class":135},[122,1465,1466],{"class":146}," json\n",[122,1468,1469,1471],{"class":124,"line":171},[122,1470,165],{"class":135},[122,1472,1473],{"class":146}," os\n",[122,1475,1476,1478,1481,1483],{"class":124,"line":184},[122,1477,136],{"class":135},[122,1479,1480],{"class":146}," collections.abc ",[122,1482,165],{"class":135},[122,1484,1485],{"class":146}," Callable\n",[122,1487,1488],{"class":124,"line":197},[122,1489,154],{"emptyLinePlaceholder":153},[122,1491,1492,1494],{"class":124,"line":202},[122,1493,165],{"class":135},[122,1495,207],{"class":146},[122,1497,1498,1500],{"class":124,"line":210},[122,1499,165],{"class":135},[122,1501,1502],{"class":146}," typer\n",[122,1504,1505,1507,1510,1512],{"class":124,"line":215},[122,1506,136],{"class":135},[122,1508,1509],{"class":146}," rich.console ",[122,1511,165],{"class":135},[122,1513,1514],{"class":146}," Console\n",[122,1516,1517,1519,1522,1524],{"class":124,"line":228},[122,1518,136],{"class":135},[122,1520,1521],{"class":146}," rich.table ",[122,1523,165],{"class":135},[122,1525,1526],{"class":146}," Table\n",[122,1528,1529],{"class":124,"line":233},[122,1530,154],{"emptyLinePlaceholder":153},[122,1532,1533,1535,1538,1540],{"class":124,"line":244},[122,1534,136],{"class":135},[122,1536,1537],{"class":146}," mytool.api ",[122,1539,165],{"class":135},[122,1541,1542],{"class":146}," ApiError, ProjectsApi, make_client\n",[122,1544,1545],{"class":124,"line":255},[122,1546,154],{"emptyLinePlaceholder":153},[122,1548,1549,1552,1554,1557,1560,1562,1564],{"class":124,"line":269},[122,1550,1551],{"class":146},"app ",[122,1553,293],{"class":135},[122,1555,1556],{"class":146}," typer.Typer(",[122,1558,1559],{"class":289},"no_args_is_help",[122,1561,293],{"class":135},[122,1563,296],{"class":139},[122,1565,299],{"class":146},[122,1567,1568,1571,1573,1576,1579,1581,1583],{"class":124,"line":274},[122,1569,1570],{"class":146},"err ",[122,1572,293],{"class":135},[122,1574,1575],{"class":146}," Console(",[122,1577,1578],{"class":289},"stderr",[122,1580,293],{"class":135},[122,1582,296],{"class":139},[122,1584,299],{"class":146},[122,1586,1587],{"class":124,"line":279},[122,1588,154],{"emptyLinePlaceholder":153},[122,1590,1591],{"class":124,"line":302},[122,1592,1593],{"class":128},"# Tests replace this factory to inject a mock transport.\n",[122,1595,1596,1599,1601,1603,1605,1607,1609,1612,1614],{"class":124,"line":314},[122,1597,1598],{"class":146},"client_factory: Callable[[",[122,1600,357],{"class":139},[122,1602,499],{"class":146},[122,1604,357],{"class":139},[122,1606,493],{"class":135},[122,1608,496],{"class":139},[122,1610,1611],{"class":146},"], httpx.Client] ",[122,1613,293],{"class":135},[122,1615,1616],{"class":146}," make_client\n",[122,1618,1619],{"class":124,"line":323},[122,1620,154],{"emptyLinePlaceholder":153},[122,1622,1623],{"class":124,"line":331},[122,1624,154],{"emptyLinePlaceholder":153},[122,1626,1627,1630],{"class":124,"line":340},[122,1628,1629],{"class":282},"@app.callback",[122,1631,1632],{"class":146},"()\n",[122,1634,1635,1637,1640],{"class":124,"line":345},[122,1636,477],{"class":135},[122,1638,1639],{"class":282}," main",[122,1641,1642],{"class":146},"(\n",[122,1644,1645],{"class":124,"line":363},[122,1646,1647],{"class":146},"    ctx: typer.Context,\n",[122,1649,1650,1653,1655,1657,1660,1663,1665,1668,1670,1673],{"class":124,"line":377},[122,1651,1652],{"class":146},"    api_url: ",[122,1654,357],{"class":139},[122,1656,221],{"class":135},[122,1658,1659],{"class":146}," typer.Option(",[122,1661,1662],{"class":224},"\"https:\u002F\u002Fapi.example.com\u002Fv1\"",[122,1664,499],{"class":146},[122,1666,1667],{"class":289},"envvar",[122,1669,293],{"class":135},[122,1671,1672],{"class":224},"\"MYTOOL_API_URL\"",[122,1674,664],{"class":146},[122,1676,1677,1679,1681],{"class":124,"line":382},[122,1678,427],{"class":146},[122,1680,430],{"class":139},[122,1682,311],{"class":146},[122,1684,1685],{"class":124,"line":387},[122,1686,1687],{"class":224},"    \"\"\"Command-line access to the projects API.\"\"\"\n",[122,1689,1690,1693,1695,1698,1701],{"class":124,"line":403},[122,1691,1692],{"class":146},"    client ",[122,1694,293],{"class":135},[122,1696,1697],{"class":146}," client_factory(api_url, os.environ.get(",[122,1699,1700],{"class":224},"\"MYTOOL_TOKEN\"",[122,1702,1212],{"class":146},[122,1704,1705],{"class":124,"line":435},[122,1706,1707],{"class":146},"    ctx.call_on_close(client.close)\n",[122,1709,1710,1713,1715],{"class":124,"line":450},[122,1711,1712],{"class":146},"    ctx.obj ",[122,1714,293],{"class":135},[122,1716,1717],{"class":146}," ProjectsApi(client)\n",[122,1719,1720],{"class":124,"line":464},[122,1721,154],{"emptyLinePlaceholder":153},[122,1723,1724],{"class":124,"line":469},[122,1725,154],{"emptyLinePlaceholder":153},[122,1727,1728,1730,1733,1736,1738],{"class":124,"line":474},[122,1729,477],{"class":135},[122,1731,1732],{"class":282}," fail",[122,1734,1735],{"class":146},"(exc: ApiError) -> ",[122,1737,430],{"class":139},[122,1739,311],{"class":146},[122,1741,1742,1745,1747,1750,1752,1754,1756,1758],{"class":124,"line":508},[122,1743,1744],{"class":146},"    err.print(",[122,1746,543],{"class":135},[122,1748,1749],{"class":224},"\"[red]error:[\u002Fred] ",[122,1751,597],{"class":139},[122,1753,894],{"class":146},[122,1755,603],{"class":139},[122,1757,552],{"class":224},[122,1759,299],{"class":146},[122,1761,1762,1765],{"class":124,"line":526},[122,1763,1764],{"class":135},"    raise",[122,1766,1767],{"class":146}," typer.Exit(exc.exit_code)\n",[122,1769,1770],{"class":124,"line":568},[122,1771,154],{"emptyLinePlaceholder":153},[122,1773,1774],{"class":124,"line":577},[122,1775,154],{"emptyLinePlaceholder":153},[122,1777,1778,1781],{"class":124,"line":609},[122,1779,1780],{"class":282},"@app.command",[122,1782,1632],{"class":146},[122,1784,1785,1787,1790,1793,1796,1798,1800,1803,1805,1808,1811,1813],{"class":124,"line":618},[122,1786,477],{"class":135},[122,1788,1789],{"class":282}," projects",[122,1791,1792],{"class":146},"(ctx: typer.Context, as_json: ",[122,1794,1795],{"class":139},"bool",[122,1797,221],{"class":135},[122,1799,1659],{"class":146},[122,1801,1802],{"class":139},"False",[122,1804,499],{"class":146},[122,1806,1807],{"class":224},"\"--json\"",[122,1809,1810],{"class":146},")) -> ",[122,1812,430],{"class":139},[122,1814,311],{"class":146},[122,1816,1817],{"class":124,"line":629},[122,1818,1819],{"class":224},"    \"\"\"List projects.\"\"\"\n",[122,1821,1822,1825],{"class":124,"line":640},[122,1823,1824],{"class":135},"    try",[122,1826,311],{"class":146},[122,1828,1829,1832,1834],{"class":124,"line":667},[122,1830,1831],{"class":146},"        items ",[122,1833,293],{"class":135},[122,1835,1836],{"class":146}," ctx.obj.list_projects()\n",[122,1838,1839,1842,1845,1847],{"class":124,"line":694},[122,1840,1841],{"class":135},"    except",[122,1843,1844],{"class":146}," ApiError ",[122,1846,866],{"class":135},[122,1848,869],{"class":146},[122,1850,1851],{"class":124,"line":700},[122,1852,1853],{"class":146},"        fail(exc)\n",[122,1855,1856,1858],{"class":124,"line":705},[122,1857,571],{"class":135},[122,1859,1860],{"class":146}," as_json:\n",[122,1862,1863,1866,1868,1870,1872,1875,1878,1880,1882],{"class":124,"line":710},[122,1864,1865],{"class":146},"        typer.echo(json.dumps([p.to_json() ",[122,1867,1256],{"class":135},[122,1869,1259],{"class":146},[122,1871,1041],{"class":135},[122,1873,1874],{"class":146}," items], ",[122,1876,1877],{"class":289},"indent",[122,1879,293],{"class":135},[122,1881,689],{"class":139},[122,1883,1212],{"class":146},[122,1885,1886],{"class":124,"line":720},[122,1887,1888],{"class":135},"        return\n",[122,1890,1891,1894,1896,1899,1902,1904,1907,1909,1912,1914,1917,1919,1921,1923,1926,1928,1931],{"class":124,"line":734},[122,1892,1893],{"class":146},"    table ",[122,1895,293],{"class":135},[122,1897,1898],{"class":146}," Table(",[122,1900,1901],{"class":224},"\"NAME\"",[122,1903,499],{"class":146},[122,1905,1906],{"class":224},"\"OWNER\"",[122,1908,499],{"class":146},[122,1910,1911],{"class":224},"\"BUILDS\"",[122,1913,499],{"class":146},[122,1915,1916],{"class":289},"box",[122,1918,293],{"class":135},[122,1920,430],{"class":139},[122,1922,499],{"class":146},[122,1924,1925],{"class":289},"header_style",[122,1927,293],{"class":135},[122,1929,1930],{"class":224},"\"bold\"",[122,1932,299],{"class":146},[122,1934,1935,1938,1940,1942,1945,1948,1951,1954],{"class":124,"line":747},[122,1936,1937],{"class":135},"    for",[122,1939,1259],{"class":146},[122,1941,1041],{"class":135},[122,1943,1944],{"class":139}," sorted",[122,1946,1947],{"class":146},"(items, ",[122,1949,1950],{"class":289},"key",[122,1952,1953],{"class":135},"=lambda",[122,1955,1956],{"class":146}," p: p.name):\n",[122,1958,1959,1962,1964],{"class":124,"line":752},[122,1960,1961],{"class":146},"        table.add_row(p.name, p.owner, ",[122,1963,357],{"class":139},[122,1965,1966],{"class":146},"(p.builds))\n",[122,1968,1969],{"class":124,"line":773},[122,1970,1971],{"class":146},"    Console().print(table)\n",[122,1973,1974],{"class":124,"line":781},[122,1975,154],{"emptyLinePlaceholder":153},[122,1977,1978],{"class":124,"line":809},[122,1979,154],{"emptyLinePlaceholder":153},[122,1981,1982,1984],{"class":124,"line":818},[122,1983,1780],{"class":282},[122,1985,1632],{"class":146},[122,1987,1988,1990,1993,1996,1998,2001,2003,2005,2007,2009,2011,2013,2015,2017],{"class":124,"line":844},[122,1989,477],{"class":135},[122,1991,1992],{"class":282}," show",[122,1994,1995],{"class":146},"(ctx: typer.Context, name: ",[122,1997,357],{"class":139},[122,1999,2000],{"class":146},", as_json: ",[122,2002,1795],{"class":139},[122,2004,221],{"class":135},[122,2006,1659],{"class":146},[122,2008,1802],{"class":139},[122,2010,499],{"class":146},[122,2012,1807],{"class":224},[122,2014,1810],{"class":146},[122,2016,430],{"class":139},[122,2018,311],{"class":146},[122,2020,2021],{"class":124,"line":858},[122,2022,2023],{"class":224},"    \"\"\"Show one project.\"\"\"\n",[122,2025,2026,2028],{"class":124,"line":872},[122,2027,1824],{"class":135},[122,2029,311],{"class":146},[122,2031,2032,2034,2036],{"class":124,"line":903},[122,2033,1299],{"class":146},[122,2035,293],{"class":135},[122,2037,2038],{"class":146}," ctx.obj.get_project(name)\n",[122,2040,2041,2043,2045,2047],{"class":124,"line":914},[122,2042,1841],{"class":135},[122,2044,1844],{"class":146},[122,2046,866],{"class":135},[122,2048,869],{"class":146},[122,2050,2051],{"class":124,"line":924},[122,2052,1853],{"class":146},[122,2054,2055,2057],{"class":124,"line":929},[122,2056,571],{"class":135},[122,2058,1860],{"class":146},[122,2060,2061,2064,2066,2068,2070],{"class":124,"line":938},[122,2062,2063],{"class":146},"        typer.echo(json.dumps(p.to_json(), ",[122,2065,1877],{"class":289},[122,2067,293],{"class":135},[122,2069,689],{"class":139},[122,2071,1212],{"class":146},[122,2073,2074,2077],{"class":124,"line":949},[122,2075,2076],{"class":135},"    else",[122,2078,311],{"class":146},[122,2080,2081,2084,2086,2088,2090,2093,2095,2098,2100,2103,2105,2108,2110,2113,2115,2117],{"class":124,"line":958},[122,2082,2083],{"class":146},"        typer.echo(",[122,2085,543],{"class":135},[122,2087,552],{"class":224},[122,2089,597],{"class":139},[122,2091,2092],{"class":146},"p.name",[122,2094,603],{"class":139},[122,2096,2097],{"class":224},"  owner=",[122,2099,597],{"class":139},[122,2101,2102],{"class":146},"p.owner",[122,2104,603],{"class":139},[122,2106,2107],{"class":224},"  builds=",[122,2109,597],{"class":139},[122,2111,2112],{"class":146},"p.builds",[122,2114,603],{"class":139},[122,2116,552],{"class":224},[122,2118,299],{"class":146},[122,2120,2121],{"class":124,"line":967},[122,2122,154],{"emptyLinePlaceholder":153},[122,2124,2125],{"class":124,"line":978},[122,2126,154],{"emptyLinePlaceholder":153},[122,2128,2129,2132,2135,2138,2141],{"class":124,"line":989},[122,2130,2131],{"class":135},"if",[122,2133,2134],{"class":139}," __name__",[122,2136,2137],{"class":135}," ==",[122,2139,2140],{"class":224}," \"__main__\"",[122,2142,311],{"class":146},[122,2144,2145],{"class":124,"line":1013},[122,2146,2147],{"class":146},"    app()\n",[10,2149,2150,2153,2154,2157,2158,2161,2162,27],{},[14,2151,2152],{},"ctx.call_on_close(client.close)"," closes the connection pool when the command finishes, whether it succeeded or raised — the Click context's cleanup hook, which is more reliable than remembering a ",[14,2155,2156],{},"finally"," in every command. The client and API object travel on ",[14,2159,2160],{},"ctx.obj",", the shared-state mechanism described in ",[23,2163,2165],{"href":2164},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fsharing-state-with-click-context-objects\u002F","sharing state with Click context objects",[29,2167,2169],{"id":2168},"ux-considerations","UX considerations",[105,2171],{"name":2172},"http-json-or-table",[34,2174,2175,2194,2200,2210,2224],{},[37,2176,2177,2180,2181,2183,2184,2186,2187,2189,2190,27],{},[1364,2178,2179],{},"Human by default, machine on request."," A table with sensible column order and no borders for people; ",[14,2182,1404],{}," with a stable, documented schema for scripts. Keep diagnostics on stderr so ",[14,2185,1404],{}," output can be piped straight into ",[14,2188,16],{},". The contract is spelled out in ",[23,2191,2193],{"href":2192},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Femitting-json-output-for-scripting\u002F","emitting JSON output for scripting",[37,2195,2196,2199],{},[1364,2197,2198],{},"Errors that end in an action."," \"not authorised — set MYTOOL_TOKEN or run: mytool login\" is worth ten \"401 Unauthorized\"s. Prefer the server's own validation message for 422 responses; it usually names the offending field.",[37,2201,2202,2205,2206,2209],{},[1364,2203,2204],{},"Show which server you are talking to."," A ",[14,2207,2208],{},"--verbose"," line naming the base URL saves a lot of confusion between staging and production.",[37,2211,2212,2215,2216,2219,2220,27],{},[1364,2213,2214],{},"Do not hide slow calls."," For requests that can take more than a second, a Rich ",[14,2217,2218],{},"console.status(\"Fetching projects...\")"," spinner on stderr tells the user the tool is working, as covered in ",[23,2221,2223],{"href":2222},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Fadding-progress-bars-and-spinners-to-python-clis\u002F","adding progress bars and spinners to Python CLIs",[37,2225,2226,2229],{},[1364,2227,2228],{},"Sort client-side when order matters."," APIs often return items in insertion or arbitrary order. Sorting by name makes output diffable between runs.",[29,2231,2233],{"id":2232},"testing-the-behaviour","Testing the behaviour",[10,2235,2236,2237,2239],{},"Replace the client factory with one that builds a client on ",[14,2238,1427],{},". The handler is an ordinary function that inspects the request and returns a response, so each test states exactly what the server does:",[113,2241,2243],{"className":115,"code":2242,"language":117,"meta":118,"style":118},"# tests\u002Ftest_cli.py\nimport json\n\nimport httpx\nimport pytest\nfrom typer.testing import CliRunner\n\nfrom mytool import cli\nfrom mytool.api import make_client\n\nrunner = CliRunner()\n\n\n@pytest.fixture\ndef server(monkeypatch):\n    routes: dict[str, httpx.Response] = {}\n\n    def handler(request: httpx.Request) -> httpx.Response:\n        assert request.headers[\"User-Agent\"].startswith(\"mytool\u002F\")\n        return routes.get(request.url.path, httpx.Response(404, json={\"message\": \"no such thing\"}))\n\n    monkeypatch.setattr(cli, \"client_factory\",\n                        lambda url, token: make_client(url, token, transport=httpx.MockTransport(handler)))\n    return routes\n\n\ndef test_projects_table(server):\n    server[\"\u002Fv1\u002Fprojects\"] = httpx.Response(200, json=[\n        {\"name\": \"web\", \"owner\": \"ana\", \"build_count\": 214},\n        {\"name\": \"billing\", \"owner\": \"ops\", \"build_count\": 87},\n    ])\n    result = runner.invoke(cli.app, [\"projects\"])\n    assert result.exit_code == 0\n    assert result.output.index(\"billing\") \u003C result.output.index(\"web\")\n\n\ndef test_projects_json_is_our_schema(server):\n    server[\"\u002Fv1\u002Fprojects\"] = httpx.Response(200, json=[{\"name\": \"web\", \"owner\": \"ana\", \"build_count\": 1}])\n    result = runner.invoke(cli.app, [\"projects\", \"--json\"])\n    assert json.loads(result.output) == [{\"name\": \"web\", \"owner\": \"ana\", \"builds\": 1}]\n\n\ndef test_auth_failure_exit_code(server):\n    server[\"\u002Fv1\u002Fprojects\"] = httpx.Response(401)\n    result = runner.invoke(cli.app, [\"projects\"])\n    assert result.exit_code == 4\n    assert \"mytool login\" in result.output\n\n\ndef test_not_found_uses_server_message(server):\n    result = runner.invoke(cli.app, [\"show\", \"nope\"])\n    assert result.exit_code == 1\n    assert \"no such thing\" in result.output\n\n\ndef test_network_failure(monkeypatch):\n    def boom(request):\n        raise httpx.ConnectError(\"connection refused\", request=request)\n\n    monkeypatch.setattr(cli, \"client_factory\",\n                        lambda url, token: make_client(url, token, transport=httpx.MockTransport(boom)))\n    result = runner.invoke(cli.app, [\"projects\"])\n    assert result.exit_code == 69\n",[14,2244,2245,2250,2256,2260,2266,2273,2285,2289,2301,2311,2315,2325,2329,2333,2338,2348,2363,2367,2377,2395,2424,2428,2438,2453,2460,2464,2468,2478,2505,2538,2569,2574,2589,2602,2622,2626,2630,2639,2688,2704,2742,2746,2750,2759,2775,2787,2797,2809,2813,2817,2826,2844,2855,2866,2870,2874,2883,2893,2913,2917,2925,2938,2950],{"__ignoreMap":118},[122,2246,2247],{"class":124,"line":125},[122,2248,2249],{"class":128},"# tests\u002Ftest_cli.py\n",[122,2251,2252,2254],{"class":124,"line":132},[122,2253,165],{"class":135},[122,2255,1466],{"class":146},[122,2257,2258],{"class":124,"line":150},[122,2259,154],{"emptyLinePlaceholder":153},[122,2261,2262,2264],{"class":124,"line":157},[122,2263,165],{"class":135},[122,2265,207],{"class":146},[122,2267,2268,2270],{"class":124,"line":171},[122,2269,165],{"class":135},[122,2271,2272],{"class":146}," pytest\n",[122,2274,2275,2277,2280,2282],{"class":124,"line":184},[122,2276,136],{"class":135},[122,2278,2279],{"class":146}," typer.testing ",[122,2281,165],{"class":135},[122,2283,2284],{"class":146}," CliRunner\n",[122,2286,2287],{"class":124,"line":197},[122,2288,154],{"emptyLinePlaceholder":153},[122,2290,2291,2293,2296,2298],{"class":124,"line":202},[122,2292,136],{"class":135},[122,2294,2295],{"class":146}," mytool ",[122,2297,165],{"class":135},[122,2299,2300],{"class":146}," cli\n",[122,2302,2303,2305,2307,2309],{"class":124,"line":210},[122,2304,136],{"class":135},[122,2306,1537],{"class":146},[122,2308,165],{"class":135},[122,2310,1616],{"class":146},[122,2312,2313],{"class":124,"line":215},[122,2314,154],{"emptyLinePlaceholder":153},[122,2316,2317,2320,2322],{"class":124,"line":228},[122,2318,2319],{"class":146},"runner ",[122,2321,293],{"class":135},[122,2323,2324],{"class":146}," CliRunner()\n",[122,2326,2327],{"class":124,"line":233},[122,2328,154],{"emptyLinePlaceholder":153},[122,2330,2331],{"class":124,"line":244},[122,2332,154],{"emptyLinePlaceholder":153},[122,2334,2335],{"class":124,"line":255},[122,2336,2337],{"class":282},"@pytest.fixture\n",[122,2339,2340,2342,2345],{"class":124,"line":269},[122,2341,477],{"class":135},[122,2343,2344],{"class":282}," server",[122,2346,2347],{"class":146},"(monkeypatch):\n",[122,2349,2350,2353,2355,2358,2360],{"class":124,"line":274},[122,2351,2352],{"class":146},"    routes: dict[",[122,2354,357],{"class":139},[122,2356,2357],{"class":146},", httpx.Response] ",[122,2359,293],{"class":135},[122,2361,2362],{"class":146}," {}\n",[122,2364,2365],{"class":124,"line":279},[122,2366,154],{"emptyLinePlaceholder":153},[122,2368,2369,2371,2374],{"class":124,"line":302},[122,2370,348],{"class":135},[122,2372,2373],{"class":282}," handler",[122,2375,2376],{"class":146},"(request: httpx.Request) -> httpx.Response:\n",[122,2378,2379,2382,2385,2387,2390,2393],{"class":124,"line":314},[122,2380,2381],{"class":135},"        assert",[122,2383,2384],{"class":146}," request.headers[",[122,2386,537],{"class":224},[122,2388,2389],{"class":146},"].startswith(",[122,2391,2392],{"class":224},"\"mytool\u002F\"",[122,2394,299],{"class":146},[122,2396,2397,2399,2402,2405,2407,2410,2412,2414,2416,2418,2421],{"class":124,"line":323},[122,2398,366],{"class":135},[122,2400,2401],{"class":146}," routes.get(request.url.path, httpx.Response(",[122,2403,2404],{"class":139},"404",[122,2406,499],{"class":146},[122,2408,2409],{"class":289},"json",[122,2411,293],{"class":135},[122,2413,597],{"class":146},[122,2415,1024],{"class":224},[122,2417,540],{"class":146},[122,2419,2420],{"class":224},"\"no such thing\"",[122,2422,2423],{"class":146},"}))\n",[122,2425,2426],{"class":124,"line":331},[122,2427,154],{"emptyLinePlaceholder":153},[122,2429,2430,2433,2436],{"class":124,"line":340},[122,2431,2432],{"class":146},"    monkeypatch.setattr(cli, ",[122,2434,2435],{"class":224},"\"client_factory\"",[122,2437,505],{"class":146},[122,2439,2440,2443,2446,2448,2450],{"class":124,"line":345},[122,2441,2442],{"class":135},"                        lambda",[122,2444,2445],{"class":146}," url, token: make_client(url, token, ",[122,2447,1423],{"class":289},[122,2449,293],{"class":135},[122,2451,2452],{"class":146},"httpx.MockTransport(handler)))\n",[122,2454,2455,2457],{"class":124,"line":363},[122,2456,612],{"class":135},[122,2458,2459],{"class":146}," routes\n",[122,2461,2462],{"class":124,"line":377},[122,2463,154],{"emptyLinePlaceholder":153},[122,2465,2466],{"class":124,"line":382},[122,2467,154],{"emptyLinePlaceholder":153},[122,2469,2470,2472,2475],{"class":124,"line":387},[122,2471,477],{"class":135},[122,2473,2474],{"class":282}," test_projects_table",[122,2476,2477],{"class":146},"(server):\n",[122,2479,2480,2483,2486,2488,2490,2493,2496,2498,2500,2502],{"class":124,"line":403},[122,2481,2482],{"class":146},"    server[",[122,2484,2485],{"class":224},"\"\u002Fv1\u002Fprojects\"",[122,2487,586],{"class":146},[122,2489,293],{"class":135},[122,2491,2492],{"class":146}," httpx.Response(",[122,2494,2495],{"class":139},"200",[122,2497,499],{"class":146},[122,2499,2409],{"class":289},[122,2501,293],{"class":135},[122,2503,2504],{"class":146},"[\n",[122,2506,2507,2510,2512,2514,2517,2519,2521,2523,2526,2528,2530,2532,2535],{"class":124,"line":435},[122,2508,2509],{"class":146},"        {",[122,2511,1239],{"class":224},[122,2513,540],{"class":146},[122,2515,2516],{"class":224},"\"web\"",[122,2518,499],{"class":146},[122,2520,1245],{"class":224},[122,2522,540],{"class":146},[122,2524,2525],{"class":224},"\"ana\"",[122,2527,499],{"class":146},[122,2529,1250],{"class":224},[122,2531,540],{"class":146},[122,2533,2534],{"class":139},"214",[122,2536,2537],{"class":146},"},\n",[122,2539,2540,2542,2544,2546,2549,2551,2553,2555,2558,2560,2562,2564,2567],{"class":124,"line":450},[122,2541,2509],{"class":146},[122,2543,1239],{"class":224},[122,2545,540],{"class":146},[122,2547,2548],{"class":224},"\"billing\"",[122,2550,499],{"class":146},[122,2552,1245],{"class":224},[122,2554,540],{"class":146},[122,2556,2557],{"class":224},"\"ops\"",[122,2559,499],{"class":146},[122,2561,1250],{"class":224},[122,2563,540],{"class":146},[122,2565,2566],{"class":139},"87",[122,2568,2537],{"class":146},[122,2570,2571],{"class":124,"line":464},[122,2572,2573],{"class":146},"    ])\n",[122,2575,2576,2579,2581,2584,2587],{"class":124,"line":469},[122,2577,2578],{"class":146},"    result ",[122,2580,293],{"class":135},[122,2582,2583],{"class":146}," runner.invoke(cli.app, [",[122,2585,2586],{"class":224},"\"projects\"",[122,2588,1357],{"class":146},[122,2590,2591,2594,2597,2599],{"class":124,"line":474},[122,2592,2593],{"class":135},"    assert",[122,2595,2596],{"class":146}," result.exit_code ",[122,2598,1079],{"class":135},[122,2600,2601],{"class":139}," 0\n",[122,2603,2604,2606,2609,2611,2613,2616,2618,2620],{"class":124,"line":508},[122,2605,2593],{"class":135},[122,2607,2608],{"class":146}," result.output.index(",[122,2610,2548],{"class":224},[122,2612,850],{"class":146},[122,2614,2615],{"class":135},"\u003C",[122,2617,2608],{"class":146},[122,2619,2516],{"class":224},[122,2621,299],{"class":146},[122,2623,2624],{"class":124,"line":526},[122,2625,154],{"emptyLinePlaceholder":153},[122,2627,2628],{"class":124,"line":568},[122,2629,154],{"emptyLinePlaceholder":153},[122,2631,2632,2634,2637],{"class":124,"line":577},[122,2633,477],{"class":135},[122,2635,2636],{"class":282}," test_projects_json_is_our_schema",[122,2638,2477],{"class":146},[122,2640,2641,2643,2645,2647,2649,2651,2653,2655,2657,2659,2662,2664,2666,2668,2670,2672,2674,2676,2678,2680,2682,2685],{"class":124,"line":609},[122,2642,2482],{"class":146},[122,2644,2485],{"class":224},[122,2646,586],{"class":146},[122,2648,293],{"class":135},[122,2650,2492],{"class":146},[122,2652,2495],{"class":139},[122,2654,499],{"class":146},[122,2656,2409],{"class":289},[122,2658,293],{"class":135},[122,2660,2661],{"class":146},"[{",[122,2663,1239],{"class":224},[122,2665,540],{"class":146},[122,2667,2516],{"class":224},[122,2669,499],{"class":146},[122,2671,1245],{"class":224},[122,2673,540],{"class":146},[122,2675,2525],{"class":224},[122,2677,499],{"class":146},[122,2679,1250],{"class":224},[122,2681,540],{"class":146},[122,2683,2684],{"class":139},"1",[122,2686,2687],{"class":146},"}])\n",[122,2689,2690,2692,2694,2696,2698,2700,2702],{"class":124,"line":618},[122,2691,2578],{"class":146},[122,2693,293],{"class":135},[122,2695,2583],{"class":146},[122,2697,2586],{"class":224},[122,2699,499],{"class":146},[122,2701,1807],{"class":224},[122,2703,1357],{"class":146},[122,2705,2706,2708,2711,2713,2716,2718,2720,2722,2724,2726,2728,2730,2732,2735,2737,2739],{"class":124,"line":629},[122,2707,2593],{"class":135},[122,2709,2710],{"class":146}," json.loads(result.output) ",[122,2712,1079],{"class":135},[122,2714,2715],{"class":146}," [{",[122,2717,1239],{"class":224},[122,2719,540],{"class":146},[122,2721,2516],{"class":224},[122,2723,499],{"class":146},[122,2725,1245],{"class":224},[122,2727,540],{"class":146},[122,2729,2525],{"class":224},[122,2731,499],{"class":146},[122,2733,2734],{"class":224},"\"builds\"",[122,2736,540],{"class":146},[122,2738,2684],{"class":139},[122,2740,2741],{"class":146},"}]\n",[122,2743,2744],{"class":124,"line":640},[122,2745,154],{"emptyLinePlaceholder":153},[122,2747,2748],{"class":124,"line":667},[122,2749,154],{"emptyLinePlaceholder":153},[122,2751,2752,2754,2757],{"class":124,"line":694},[122,2753,477],{"class":135},[122,2755,2756],{"class":282}," test_auth_failure_exit_code",[122,2758,2477],{"class":146},[122,2760,2761,2763,2765,2767,2769,2771,2773],{"class":124,"line":700},[122,2762,2482],{"class":146},[122,2764,2485],{"class":224},[122,2766,586],{"class":146},[122,2768,293],{"class":135},[122,2770,2492],{"class":146},[122,2772,1046],{"class":139},[122,2774,299],{"class":146},[122,2776,2777,2779,2781,2783,2785],{"class":124,"line":705},[122,2778,2578],{"class":146},[122,2780,293],{"class":135},[122,2782,2583],{"class":146},[122,2784,2586],{"class":224},[122,2786,1357],{"class":146},[122,2788,2789,2791,2793,2795],{"class":124,"line":710},[122,2790,2593],{"class":135},[122,2792,2596],{"class":146},[122,2794,1079],{"class":135},[122,2796,252],{"class":139},[122,2798,2799,2801,2804,2806],{"class":124,"line":720},[122,2800,2593],{"class":135},[122,2802,2803],{"class":224}," \"mytool login\"",[122,2805,997],{"class":135},[122,2807,2808],{"class":146}," result.output\n",[122,2810,2811],{"class":124,"line":734},[122,2812,154],{"emptyLinePlaceholder":153},[122,2814,2815],{"class":124,"line":747},[122,2816,154],{"emptyLinePlaceholder":153},[122,2818,2819,2821,2824],{"class":124,"line":752},[122,2820,477],{"class":135},[122,2822,2823],{"class":282}," test_not_found_uses_server_message",[122,2825,2477],{"class":146},[122,2827,2828,2830,2832,2834,2837,2839,2842],{"class":124,"line":773},[122,2829,2578],{"class":146},[122,2831,293],{"class":135},[122,2833,2583],{"class":146},[122,2835,2836],{"class":224},"\"show\"",[122,2838,499],{"class":146},[122,2840,2841],{"class":224},"\"nope\"",[122,2843,1357],{"class":146},[122,2845,2846,2848,2850,2852],{"class":124,"line":781},[122,2847,2593],{"class":135},[122,2849,2596],{"class":146},[122,2851,1079],{"class":135},[122,2853,2854],{"class":139}," 1\n",[122,2856,2857,2859,2862,2864],{"class":124,"line":809},[122,2858,2593],{"class":135},[122,2860,2861],{"class":224}," \"no such thing\"",[122,2863,997],{"class":135},[122,2865,2808],{"class":146},[122,2867,2868],{"class":124,"line":818},[122,2869,154],{"emptyLinePlaceholder":153},[122,2871,2872],{"class":124,"line":844},[122,2873,154],{"emptyLinePlaceholder":153},[122,2875,2876,2878,2881],{"class":124,"line":858},[122,2877,477],{"class":135},[122,2879,2880],{"class":282}," test_network_failure",[122,2882,2347],{"class":146},[122,2884,2885,2887,2890],{"class":124,"line":872},[122,2886,348],{"class":135},[122,2888,2889],{"class":282}," boom",[122,2891,2892],{"class":146},"(request):\n",[122,2894,2895,2897,2900,2903,2905,2908,2910],{"class":124,"line":903},[122,2896,1179],{"class":135},[122,2898,2899],{"class":146}," httpx.ConnectError(",[122,2901,2902],{"class":224},"\"connection refused\"",[122,2904,499],{"class":146},[122,2906,2907],{"class":289},"request",[122,2909,293],{"class":135},[122,2911,2912],{"class":146},"request)\n",[122,2914,2915],{"class":124,"line":914},[122,2916,154],{"emptyLinePlaceholder":153},[122,2918,2919,2921,2923],{"class":124,"line":924},[122,2920,2432],{"class":146},[122,2922,2435],{"class":224},[122,2924,505],{"class":146},[122,2926,2927,2929,2931,2933,2935],{"class":124,"line":929},[122,2928,2442],{"class":135},[122,2930,2445],{"class":146},[122,2932,1423],{"class":289},[122,2934,293],{"class":135},[122,2936,2937],{"class":146},"httpx.MockTransport(boom)))\n",[122,2939,2940,2942,2944,2946,2948],{"class":124,"line":938},[122,2941,2578],{"class":146},[122,2943,293],{"class":135},[122,2945,2583],{"class":146},[122,2947,2586],{"class":224},[122,2949,1357],{"class":146},[122,2951,2952,2954,2956,2958],{"class":124,"line":949},[122,2953,2593],{"class":135},[122,2955,2596],{"class":146},[122,2957,1079],{"class":135},[122,2959,2960],{"class":139}," 69\n",[10,2962,2963,2964,2967,2968,2971,2972,27],{},"Note the JSON test asserts the ",[1364,2965,2966],{},"renamed"," key ",[14,2969,2970],{},"builds",", proving that the output schema is decoupled from the server's. For more on keeping CLI tests fast and offline, see ",[23,2973,2975],{"href":2974},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Fmocking-filesystem-and-network-in-cli-tests\u002F","mocking filesystem and network in CLI tests",[29,2977,2979],{"id":2978},"conclusion","Conclusion",[10,2981,2982,2983,2985,2986,2990,2991,2995],{},"An API client CLI is three small, separate things: a configured ",[14,2984,102],{},", an API module that converts HTTP into typed objects and one error type, and a command layer that renders those objects for people or for scripts. Keep the boundaries firm, inject the transport so tests never touch the network, and give every failure a message that ends in something the user can do. From here, add ",[23,2987,2989],{"href":2988},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fretries-and-backoff-for-cli-http-calls\u002F","retries and backoff"," for flaky services and ",[23,2992,2994],{"href":2993},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fpaginating-api-results-in-a-cli\u002F","pagination"," for lists that outgrow one response.",[29,2997,2999],{"id":2998},"frequently-asked-questions","Frequently asked questions",[3001,3002,3004],"h3",{"id":3003},"should-i-generate-the-client-from-an-openapi-spec","Should I generate the client from an OpenAPI spec?",[10,3006,3007,3008,3011],{},"Generators such as ",[14,3009,3010],{},"openapi-python-client"," save typing for large APIs and keep models in sync with the spec. They also produce a lot of code and a dependency on the generator's style. For a CLI that uses a dozen endpoints, a hand-written module is usually smaller and easier to read; for one that wraps hundreds, generate the models and keep a hand-written layer on top.",[3001,3013,3015],{"id":3014},"pydantic-or-dataclasses-for-the-response-models","Pydantic or dataclasses for the response models?",[10,3017,3018],{},"Dataclasses are enough when you control the parsing and want zero import cost. Pydantic earns its place when responses are deeply nested or you want validation errors that name the bad field. If startup time matters, measure: Pydantic adds noticeable import time.",[3001,3020,3022],{"id":3021},"where-should-the-base-url-and-token-come-from","Where should the base URL and token come from?",[10,3024,3025,3026,27],{},"A flag, then an environment variable, then a config file, then a default — the standard precedence. Tokens should prefer the keychain over config files. Both are covered in ",[23,3027,3029],{"href":3028},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Fsupporting-multiple-profiles-and-accounts\u002F","supporting multiple profiles and accounts",[3001,3031,3033],{"id":3032},"how-do-i-avoid-creating-the-client-for-commands-that-do-not-need-it","How do I avoid creating the client for commands that do not need it?",[10,3035,3036,3037,3040],{},"Create it lazily: store a factory on the context and build the client the first time a command asks for it. That keeps ",[14,3038,3039],{},"--help"," and purely local commands free of network setup.",[29,3042,3044],{"id":3043},"related","Related",[34,3046,3047,3053,3058,3063,3069],{},[37,3048,3049,3050],{},"Up: ",[23,3051,3052],{"href":25},"Calling HTTP APIs from Python CLIs",[37,3054,3055],{},[23,3056,3057],{"href":2988},"Retries and backoff for CLI HTTP calls",[37,3059,3060],{},[23,3061,3062],{"href":2993},"Paginating API results in a CLI",[37,3064,3065],{},[23,3066,3068],{"href":3067},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Frendering-tables-and-json-with-rich\u002F","Rendering tables and JSON with Rich",[37,3070,3071],{},[23,3072,3074],{"href":3073},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fdependency-injection-patterns-for-cli-commands\u002F","Dependency injection patterns for CLI commands",[3076,3077,3078],"style",{},"html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":118,"searchDepth":132,"depth":132,"links":3080},[3081,3082,3083,3084,3085,3086,3087,3088,3094],{"id":31,"depth":132,"text":32},{"id":83,"depth":132,"text":84},{"id":110,"depth":132,"text":111},{"id":1434,"depth":132,"text":1435},{"id":2168,"depth":132,"text":2169},{"id":2232,"depth":132,"text":2233},{"id":2978,"depth":132,"text":2979},{"id":2998,"depth":132,"text":2999,"children":3089},[3090,3091,3092,3093],{"id":3003,"depth":150,"text":3004},{"id":3014,"depth":150,"text":3015},{"id":3021,"depth":150,"text":3022},{"id":3032,"depth":150,"text":3033},{"id":3043,"depth":132,"text":3044},"2026-09-18","Build a Typer CLI over a REST API with httpx: one configured client, typed results, error mapping to exit codes, table and JSON output, and MockTransport tests.","intermediate",false,"md",{},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fbuilding-an-api-client-cli-with-httpx",{"title":5,"description":3096},"cli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fbuilding-an-api-client-cli-with-httpx\u002Findex",[20,3105,45,3106],"api","testing","qBl8WeNngbxzAo_tn_VpWdhPsscF6kP6FAOhYxdTjGQ",[3109,3112,3115,3118,3121,3124,3127,3130,3133,3136,3139,3142,3145,3148,3151,3154,3157,3160,3163,3166,3169,3172,3175,3178,3181,3184,3187,3190,3193,3196,3199,3202,3205,3208,3211,3214,3217,3220,3223,3226,3229,3232,3235,3238,3241,3244,3247,3250,3253,3256,3259,3262,3265,3268,3271,3274,3275,3278,3280,3283,3286,3289,3292,3295,3298,3301,3304,3307,3310,3313,3316,3319,3322,3325,3328,3331,3334,3337,3340,3343,3346,3349,3352,3355,3358,3361,3364,3367,3370,3373,3376,3379,3382,3385,3388,3391,3394,3397,3400,3403,3406,3409,3412,3415,3418,3421,3424,3427,3430,3433,3436,3439,3442,3445,3448,3451,3454,3457,3460,3463,3466,3469,3472,3475,3478,3481,3484,3487,3490,3493,3496,3499,3502,3505,3508,3511,3514,3517,3520,3523,3526,3529,3532,3535,3538,3541,3544,3547,3550,3553,3556,3559,3562,3565,3568,3571,3574,3577,3580,3583,3586,3589,3592,3595,3598,3601,3604,3607,3610,3613,3616,3619,3622,3625,3628,3631,3634,3637,3640,3643,3646,3649,3652],{"path":3110,"title":3111},"\u002Fabout","About Python CLI Toolcraft",{"path":3113,"title":3114},"\u002Fadvanced-input-parsing-user-experience\u002Fadvanced-argument-validation-strategies","Advanced Argument Validation Strategies",{"path":3116,"title":3117},"\u002Fadvanced-input-parsing-user-experience\u002Fadvanced-argument-validation-strategies\u002Fparsing-nested-json-arguments-in-python-clis","Parsing Nested JSON Args in Python CLIs",{"path":3119,"title":3120},"\u002Fadvanced-input-parsing-user-experience\u002Fadvanced-argument-validation-strategies\u002Fvalidating-dependent-and-conflicting-options","Validating Dependent and Conflicting CLI Options",{"path":3122,"title":3123},"\u002Fadvanced-input-parsing-user-experience\u002Fadvanced-argument-validation-strategies\u002Fvalidating-file-and-directory-paths-in-clis","Validating File and Directory Paths in CLIs",{"path":3125,"title":3126},"\u002Fadvanced-input-parsing-user-experience\u002Fadvanced-argument-validation-strategies\u002Fwriting-custom-click-parameter-types","Writing Custom Click Parameter Types",{"path":3128,"title":3129},"\u002Fadvanced-input-parsing-user-experience\u002Fbuilding-terminal-uis-with-textual\u002Fbuilding-your-first-textual-app","Building Your First Textual App for a Python CLI",{"path":3131,"title":3132},"\u002Fadvanced-input-parsing-user-experience\u002Fbuilding-terminal-uis-with-textual\u002Fchoosing-between-a-cli-a-prompt-flow-and-a-tui","Choosing Between a CLI, a Prompt Flow and a TUI",{"path":3134,"title":3135},"\u002Fadvanced-input-parsing-user-experience\u002Fbuilding-terminal-uis-with-textual","Building Terminal UIs with Textual for Python CLIs",{"path":3137,"title":3138},"\u002Fadvanced-input-parsing-user-experience\u002Fbuilding-terminal-uis-with-textual\u002Ftesting-textual-apps-with-pilot","Testing Textual Apps with Pilot",{"path":3140,"title":3141},"\u002Fadvanced-input-parsing-user-experience\u002Fcli-help-output-and-documentation\u002Fadding-examples-and-epilogs-to-help-output","Adding Examples and Epilogs to Help Output",{"path":3143,"title":3144},"\u002Fadvanced-input-parsing-user-experience\u002Fcli-help-output-and-documentation\u002Fgenerating-man-pages-and-docs-from-a-cli","Generating Man Pages and Docs from a CLI",{"path":3146,"title":3147},"\u002Fadvanced-input-parsing-user-experience\u002Fcli-help-output-and-documentation","CLI Help Output and Documentation",{"path":3149,"title":3150},"\u002Fadvanced-input-parsing-user-experience\u002Fcli-help-output-and-documentation\u002Fversioning-and-deprecating-cli-flags","Versioning and Deprecating CLI Flags",{"path":3152,"title":3153},"\u002Fadvanced-input-parsing-user-experience\u002Fcli-help-output-and-documentation\u002Fwriting-help-text-users-actually-read","Writing Help Text Users Actually Read",{"path":3155,"title":3156},"\u002Fadvanced-input-parsing-user-experience\u002Fcross-platform-terminal-compatibility\u002Fadapting-output-to-terminal-width","Adapting Python CLI Output to Terminal Width",{"path":3158,"title":3159},"\u002Fadvanced-input-parsing-user-experience\u002Fcross-platform-terminal-compatibility\u002Fdetecting-ci-environments-and-non-interactive-shells","Detecting CI Environments and Non-Interactive Shells",{"path":3161,"title":3162},"\u002Fadvanced-input-parsing-user-experience\u002Fcross-platform-terminal-compatibility\u002Ffixing-unicode-and-encoding-errors-on-windows","Fixing Unicode and Encoding Errors on Windows in Python CLIs",{"path":3164,"title":3165},"\u002Fadvanced-input-parsing-user-experience\u002Fcross-platform-terminal-compatibility","Cross-Platform Terminal Compatibility for Python CLIs",{"path":3167,"title":3168},"\u002Fadvanced-input-parsing-user-experience\u002Fcross-platform-terminal-compatibility\u002Frespecting-no-color-and-force-color","Respecting NO_COLOR and FORCE_COLOR in Python CLIs",{"path":3170,"title":3171},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002Fchoosing-exit-codes-for-cli-tools","Choosing Exit Codes for CLI Tools",{"path":3173,"title":3174},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002Fdesigning-an-exception-hierarchy-for-a-cli","Designing an Exception Hierarchy for a Python CLI",{"path":3176,"title":3177},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002Ffriendly-error-messages-and-tracebacks","Friendly Error Messages and Tracebacks",{"path":3179,"title":3180},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002Fhandling-keyboard-interrupt-cleanly","Handling Keyboard Interrupt Cleanly",{"path":3182,"title":3183},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes","Error Handling and Exit Codes for CLIs",{"path":3185,"title":3186},"\u002Fadvanced-input-parsing-user-experience\u002Ferror-handling-and-exit-codes\u002Freporting-machine-readable-errors-in-json-mode","Reporting Machine-Readable Errors in JSON Mode",{"path":3188,"title":3189},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars\u002Fconfig-precedence-flags-env-files-defaults","Config Precedence: Flags, Env, Files, Defaults",{"path":3191,"title":3192},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars\u002Fdiscovering-project-config-files-by-walking-up-directories","Discovering Project Config Files by Walking Up Directories",{"path":3194,"title":3195},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars","Handling Config Files and Env Vars in CLIs",{"path":3197,"title":3198},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars\u002Floading-yaml-configs-safely-in-cli-apps","Loading YAML configs safely in CLI apps",{"path":3200,"title":3201},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars\u002Freading-toml-config-with-tomllib","Reading TOML Config with tomllib in Python CLIs",{"path":3203,"title":3204},"\u002Fadvanced-input-parsing-user-experience\u002Fhandling-configuration-files-env-vars\u002Ftyped-settings-with-pydantic-settings","Typed Settings with pydantic-settings in Python CLIs",{"path":3206,"title":3207},"\u002Fadvanced-input-parsing-user-experience","Advanced Input Parsing for Python CLIs",{"path":3209,"title":3210},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Fadding-progress-bars-and-spinners-to-python-clis","Progress Bars and Spinners for Python CLIs",{"path":3212,"title":3213},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Fbuilding-interactive-prompts-and-menus","Building Interactive Prompts and Menus in Python CLIs",{"path":3215,"title":3216},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich","Interactive Terminal UI with Rich",{"path":3218,"title":3219},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Flive-dashboards-with-rich-live","Live Dashboards with Rich Live in Python CLIs",{"path":3221,"title":3222},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Frendering-tables-and-json-with-rich","Rendering Tables and JSON with Rich",{"path":3224,"title":3225},"\u002Fadvanced-input-parsing-user-experience\u002Finteractive-terminal-ui-with-rich\u002Ftheming-rich-output-consistently","Theming Rich Output Consistently in Python CLIs",{"path":3227,"title":3228},"\u002Fadvanced-input-parsing-user-experience\u002Fshell-completion-for-python-clis\u002Fdynamic-completion-values-from-apis-and-files","Dynamic Completion Values from APIs and Files",{"path":3230,"title":3231},"\u002Fadvanced-input-parsing-user-experience\u002Fshell-completion-for-python-clis\u002Fenabling-tab-completion-in-click-and-typer","Enabling Tab Completion in Click and Typer",{"path":3233,"title":3234},"\u002Fadvanced-input-parsing-user-experience\u002Fshell-completion-for-python-clis","Shell Completion for Python CLIs",{"path":3236,"title":3237},"\u002Fadvanced-input-parsing-user-experience\u002Fshell-completion-for-python-clis\u002Finstalling-shell-completion-for-bash-zsh-fish","Installing Shell Completion for bash, zsh, fish",{"path":3239,"title":3240},"\u002Fadvanced-input-parsing-user-experience\u002Fshell-completion-for-python-clis\u002Ftesting-shell-completion-in-python-clis","Testing Shell Completion in Python CLIs",{"path":3242,"title":3243},"\u002Fadvanced-input-parsing-user-experience\u002Fstructured-logging-for-cli-apps\u002Fadding-trace-ids-and-context-to-cli-logs","Adding Trace IDs and Context to Python CLI Logs",{"path":3245,"title":3246},"\u002Fadvanced-input-parsing-user-experience\u002Fstructured-logging-for-cli-apps\u002Fadding-verbose-and-quiet-logging-flags","Adding Verbose and Quiet Logging Flags",{"path":3248,"title":3249},"\u002Fadvanced-input-parsing-user-experience\u002Fstructured-logging-for-cli-apps","Structured Logging for CLI Apps",{"path":3251,"title":3252},"\u002Fadvanced-input-parsing-user-experience\u002Fstructured-logging-for-cli-apps\u002Fstructured-json-logging-in-python-clis","Structured JSON Logging in Python CLIs",{"path":3254,"title":3255},"\u002Fadvanced-input-parsing-user-experience\u002Fstructured-logging-for-cli-apps\u002Fwriting-rotating-log-files-from-a-cli","Writing Rotating Log Files from a Python CLI",{"path":3257,"title":3258},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Fdetecting-tty-and-adapting-output","Detecting a TTY and Adapting Output",{"path":3260,"title":3261},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Femitting-json-output-for-scripting","Emitting JSON Output for Scripting",{"path":3263,"title":3264},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Fhandling-broken-pipe-and-sigpipe","Handling Broken Pipe and SIGPIPE",{"path":3266,"title":3267},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes","Working with stdin, stdout and Pipes",{"path":3269,"title":3270},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Fprocessing-large-files-and-ndjson-streams","Processing Large Files and NDJSON Streams in Python CLIs",{"path":3272,"title":3273},"\u002Fadvanced-input-parsing-user-experience\u002Fworking-with-stdin-stdout-and-pipes\u002Freading-piped-input-in-python-clis","Reading Piped Input in Python CLIs",{"path":3101,"title":5},{"path":3276,"title":3277},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fdownloading-files-with-progress-in-python","Downloading Files with Progress in Python CLIs",{"path":3279,"title":3052},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis",{"path":3281,"title":3282},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Foauth-device-flow-login-for-clis","OAuth Device Flow Login for Python CLIs",{"path":3284,"title":3285},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fpaginating-api-results-in-a-cli","Paginating API Results in a Python CLI",{"path":3287,"title":3288},"\u002Fcli-runtime-systems-integration\u002Fcalling-http-apis-from-python-clis\u002Fretries-and-backoff-for-cli-http-calls","Retries and Backoff for CLI HTTP Calls",{"path":3290,"title":3291},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002Fcancelling-async-tasks-on-ctrl-c","Cancelling Async Tasks on Ctrl+C in Python CLIs",{"path":3293,"title":3294},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis","Concurrency and Async in Python CLIs",{"path":3296,"title":3297},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002Fmultiprocessing-for-cpu-bound-cli-tasks","Multiprocessing for CPU-Bound CLI Tasks",{"path":3299,"title":3300},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002Fparallelising-cli-work-with-thread-pools","Parallelising CLI Work with Thread Pools",{"path":3302,"title":3303},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002Frate-limiting-concurrent-requests-in-clis","Rate-Limiting Concurrent Requests in Python CLIs",{"path":3305,"title":3306},"\u002Fcli-runtime-systems-integration\u002Fconcurrency-and-async-in-python-clis\u002Frunning-async-code-in-typer-and-click","Running Async Code in Typer and Click",{"path":3308,"title":3309},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Fcross-platform-paths-with-pathlib","Cross-Platform Paths with pathlib in CLIs",{"path":3311,"title":3312},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Ffile-locking-for-concurrent-cli-runs","File Locking for Concurrent CLI Runs in Python",{"path":3314,"title":3315},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes","Filesystem Paths and Atomic Writes for CLIs",{"path":3317,"title":3318},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Fsafe-temporary-files-and-directories","Safe Temporary Files and Directories in CLIs",{"path":3320,"title":3321},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Fstoring-app-data-with-platformdirs","Storing CLI App Data with platformdirs",{"path":3323,"title":3324},"\u002Fcli-runtime-systems-integration\u002Ffilesystem-paths-and-atomic-writes\u002Fwriting-files-atomically-in-python-clis","Writing Files Atomically in Python CLIs",{"path":3326,"title":3327},"\u002Fcli-runtime-systems-integration","CLI Runtime & Systems Integration for Python",{"path":3329,"title":3330},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Fbuilding-a-watch-mode-with-watchfiles","Building a Watch Mode with watchfiles in Python",{"path":3332,"title":3333},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Fhandling-sigterm-and-graceful-shutdown","Handling SIGTERM and Graceful Shutdown in CLIs",{"path":3335,"title":3336},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Fhealth-checks-and-heartbeats-for-long-running-clis","Health Checks and Heartbeats for Long-Running CLIs",{"path":3338,"title":3339},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis","Long-Running and Watch-Mode Python CLIs",{"path":3341,"title":3342},"\u002Fcli-runtime-systems-integration\u002Flong-running-and-watch-mode-clis\u002Frunning-a-cli-on-a-schedule-with-cron-and-systemd","Running a Python CLI on a Schedule with cron and systemd",{"path":3344,"title":3345},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis\u002Favoiding-shell-injection-in-python-clis","Avoiding Shell Injection in Python CLIs",{"path":3347,"title":3348},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis\u002Fcalling-external-commands-safely-with-subprocess","Calling External Commands Safely with subprocess",{"path":3350,"title":3351},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis\u002Fhandling-subprocess-timeouts-and-exit-codes","Handling Subprocess Timeouts and Exit Codes",{"path":3353,"title":3354},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis","Running Subprocesses from Python CLIs",{"path":3356,"title":3357},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis\u002Fstreaming-subprocess-output-in-real-time","Streaming Subprocess Output in Real Time",{"path":3359,"title":3360},"\u002Fcli-runtime-systems-integration\u002Frunning-subprocesses-from-python-clis\u002Fwrapping-git-and-other-tools-from-a-python-cli","Wrapping git and Other Tools from a Python CLI",{"path":3362,"title":3363},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis","Secrets and Credentials in Python CLIs",{"path":3365,"title":3366},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Fprompting-for-passwords-securely","Prompting for Passwords Securely in Python CLIs",{"path":3368,"title":3369},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Freading-secrets-from-env-and-files","Reading Secrets from Env Vars and Files in CLIs",{"path":3371,"title":3372},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Fredacting-secrets-from-cli-output-and-logs","Redacting Secrets from CLI Output and Logs",{"path":3374,"title":3375},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Fstoring-tokens-with-keyring","Storing CLI Tokens Securely with keyring",{"path":3377,"title":3378},"\u002Fcli-runtime-systems-integration\u002Fsecrets-and-credentials-in-python-clis\u002Fsupporting-multiple-profiles-and-accounts","Supporting Multiple Profiles and Accounts in CLIs",{"path":3380,"title":3381},"\u002F","Python CLI Toolcraft",{"path":3383,"title":3384},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcli-startup-performance-and-lazy-loading\u002Fcaching-expensive-work-between-cli-runs","Caching Expensive Work Between Python CLI Runs",{"path":3386,"title":3387},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcli-startup-performance-and-lazy-loading","CLI Startup Performance and Lazy Loading",{"path":3389,"title":3390},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcli-startup-performance-and-lazy-loading\u002Flazy-loading-subcommands-for-faster-startup","Lazy Loading Subcommands for Faster Startup",{"path":3392,"title":3393},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcli-startup-performance-and-lazy-loading\u002Fprofiling-python-cli-startup-time","Profiling Python CLI Startup Time",{"path":3395,"title":3396},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcli-startup-performance-and-lazy-loading\u002Freducing-cli-dependency-weight","Reducing CLI Dependency Weight",{"path":3398,"title":3399},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse\u002Fargparse-subparsers-for-subcommands","argparse Subparsers for Subcommands",{"path":3401,"title":3402},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse\u002Fargparse-vs-click-vs-typer-comparison","argparse vs Click vs Typer Compared",{"path":3404,"title":3405},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse","Command-Line Parsing with argparse",{"path":3407,"title":3408},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse\u002Fmigrating-from-argparse-to-typer","Migrating from argparse to Typer",{"path":3410,"title":3411},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse\u002Fmutually-exclusive-options-in-argparse","Mutually Exclusive Options in argparse",{"path":3413,"title":3414},"\u002Fmodern-python-cli-frameworks-architecture\u002Fcommand-line-parsing-with-argparse\u002Fwriting-custom-argparse-actions","Writing Custom argparse Actions in Python",{"path":3416,"title":3417},"\u002Fmodern-python-cli-frameworks-architecture\u002Fdesigning-cli-interfaces-and-conventions\u002Fadding-dry-run-and-confirmation-to-destructive-commands","Adding Dry-Run and Confirmation to Destructive Commands",{"path":3419,"title":3420},"\u002Fmodern-python-cli-frameworks-architecture\u002Fdesigning-cli-interfaces-and-conventions\u002Ffollowing-posix-and-gnu-argument-conventions","Following POSIX and GNU Argument Conventions in Python",{"path":3422,"title":3423},"\u002Fmodern-python-cli-frameworks-architecture\u002Fdesigning-cli-interfaces-and-conventions\u002Fglobal-options-vs-per-command-options","Global Options vs Per-Command Options in Python CLIs",{"path":3425,"title":3426},"\u002Fmodern-python-cli-frameworks-architecture\u002Fdesigning-cli-interfaces-and-conventions","Designing CLI Interfaces and Conventions in Python",{"path":3428,"title":3429},"\u002Fmodern-python-cli-frameworks-architecture\u002Fdesigning-cli-interfaces-and-conventions\u002Fnaming-commands-and-flags-consistently","Naming Commands and Flags Consistently in Python CLIs",{"path":3431,"title":3432},"\u002Fmodern-python-cli-frameworks-architecture","Python CLI Frameworks and Architecture",{"path":3434,"title":3435},"\u002Fmodern-python-cli-frameworks-architecture\u002Fplugin-architectures-for-extensible-clis\u002Fdiscovering-plugins-with-entry-points","Discovering Plugins with Entry Points in Python CLIs",{"path":3437,"title":3438},"\u002Fmodern-python-cli-frameworks-architecture\u002Fplugin-architectures-for-extensible-clis\u002Fhook-based-plugins-with-pluggy","Hook-Based Plugins for Python CLIs with pluggy",{"path":3440,"title":3441},"\u002Fmodern-python-cli-frameworks-architecture\u002Fplugin-architectures-for-extensible-clis","Plugin Architectures for Extensible CLIs",{"path":3443,"title":3444},"\u002Fmodern-python-cli-frameworks-architecture\u002Fplugin-architectures-for-extensible-clis\u002Fversioning-a-plugin-api","Versioning a Plugin API for a Python CLI",{"path":3446,"title":3447},"\u002Fmodern-python-cli-frameworks-architecture\u002Fplugin-architectures-for-extensible-clis\u002Fwriting-a-plugin-for-an-existing-cli","Writing a Plugin for an Existing CLI",{"path":3449,"title":3450},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fbest-practices-for-python-cli-entry-points","Best practices for Python CLI entry points",{"path":3452,"title":3453},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fdependency-injection-patterns-for-cli-commands","Dependency Injection Patterns for CLI Commands",{"path":3455,"title":3456},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fhow-to-structure-a-large-python-cli-project","Structuring a Large Python CLI Project",{"path":3458,"title":3459},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis","Structuring Multi-Command Python CLIs",{"path":3461,"title":3462},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fsharing-common-options-across-commands","Sharing Common Options Across Python CLI Commands",{"path":3464,"title":3465},"\u002Fmodern-python-cli-frameworks-architecture\u002Fstructuring-multi-command-python-clis\u002Fsharing-state-with-click-context-objects","Sharing State with Click Context Objects",{"path":3467,"title":3468},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Fend-to-end-testing-an-installed-cli","End-to-End Testing an Installed Python CLI",{"path":3470,"title":3471},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications","Testing Python CLI Applications",{"path":3473,"title":3474},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Fmeasuring-cli-test-coverage","Measuring CLI Test Coverage",{"path":3476,"title":3477},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Fmocking-filesystem-and-network-in-cli-tests","Mocking the Filesystem and Network in CLI Tests",{"path":3479,"title":3480},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Fproperty-based-testing-cli-arguments-with-hypothesis","Property-Based Testing CLI Arguments with Hypothesis",{"path":3482,"title":3483},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Fsnapshot-testing-cli-output","Snapshot Testing CLI Output",{"path":3485,"title":3486},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Ftesting-click-commands-with-clirunner","Testing Click Commands with CliRunner",{"path":3488,"title":3489},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftesting-python-cli-applications\u002Ftesting-interactive-prompts-and-stdin","Testing Interactive Prompts and stdin",{"path":3491,"title":3492},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each\u002Fbuilding-a-cli-with-subcommands-in-click","Building a CLI with subcommands in Click",{"path":3494,"title":3495},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each\u002Fbuilding-dynamic-commands-in-click","Building Dynamic Commands in Click",{"path":3497,"title":3498},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each\u002Fconverting-a-click-app-to-typer","Converting a Click App to Typer",{"path":3500,"title":3501},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each","Typer vs Click: When to Use Each",{"path":3503,"title":3504},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each\u002Ftyper-callback-functions-explained","Typer callback functions explained",{"path":3506,"title":3507},"\u002Fmodern-python-cli-frameworks-architecture\u002Ftyper-vs-click-when-to-use-each\u002Fusing-annotated-options-in-typer","Using Annotated Options in Typer",{"path":3509,"title":3510},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis\u002Fautomating-releases-from-git-tags","Automating Python CLI Releases from Git Tags",{"path":3512,"title":3513},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis\u002Fcaching-uv-dependencies-in-ci","Caching uv Dependencies in CI for Python CLIs",{"path":3515,"title":3516},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis","CI\u002FCD Pipelines for Python CLIs",{"path":3518,"title":3519},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis\u002Fpublishing-to-pypi-with-trusted-publishing","Publishing a CLI to PyPI with Trusted Publishing",{"path":3521,"title":3522},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis\u002Fsmoke-testing-the-built-wheel-in-ci","Smoke-Testing the Built Wheel of a Python CLI in CI",{"path":3524,"title":3525},"\u002Fproject-setup-dependency-management\u002Fci-cd-pipelines-for-python-clis\u002Ftesting-a-cli-across-python-versions-with-github-actions","Testing a CLI Across Python Versions in GitHub Actions",{"path":3527,"title":3528},"\u002Fproject-setup-dependency-management\u002Fcli-project-scaffolding-with-cookiecutter\u002Fbuilding-a-cookiecutter-template-for-typer-clis","Building a Cookiecutter Template for Typer CLIs",{"path":3530,"title":3531},"\u002Fproject-setup-dependency-management\u002Fcli-project-scaffolding-with-cookiecutter\u002Fcopier-vs-cookiecutter-for-cli-templates","Copier vs Cookiecutter for CLI Templates",{"path":3533,"title":3534},"\u002Fproject-setup-dependency-management\u002Fcli-project-scaffolding-with-cookiecutter","CLI Project Scaffolding with Cookiecutter",{"path":3536,"title":3537},"\u002Fproject-setup-dependency-management\u002Fcli-project-scaffolding-with-cookiecutter\u002Fpost-generation-hooks-in-cli-templates","Post-Generation Hooks in Python CLI Templates",{"path":3539,"title":3540},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries\u002Fbuilding-cross-platform-release-binaries-in-ci","Building Cross-Platform Release Binaries in CI",{"path":3542,"title":3543},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries\u002Fbundling-a-python-cli-with-pyinstaller","Bundling a Python CLI with PyInstaller",{"path":3545,"title":3546},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries\u002Fhomebrew-and-scoop-packaging-for-python-clis","Homebrew and Scoop Packaging for Python CLIs",{"path":3548,"title":3549},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries","Distributing CLIs as Standalone Binaries",{"path":3551,"title":3552},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries\u002Fnuitka-vs-pyinstaller-for-python-clis","Nuitka vs PyInstaller for Python CLI Binaries",{"path":3554,"title":3555},"\u002Fproject-setup-dependency-management\u002Fdistributing-clis-as-standalone-binaries\u002Fshipping-a-cli-as-a-zipapp-with-shiv","Shipping a CLI as a Zipapp with shiv",{"path":3557,"title":3558},"\u002Fproject-setup-dependency-management","Project Setup & Dependency Management",{"path":3560,"title":3561},"\u002Fproject-setup-dependency-management\u002Flinting-and-type-checking-cli-code\u002Fconfiguring-ruff-for-a-cli-project","Configuring Ruff for a Python CLI Project",{"path":3563,"title":3564},"\u002Fproject-setup-dependency-management\u002Flinting-and-type-checking-cli-code\u002Fenforcing-import-boundaries-in-a-cli-codebase","Enforcing Import Boundaries in a Python CLI Codebase",{"path":3566,"title":3567},"\u002Fproject-setup-dependency-management\u002Flinting-and-type-checking-cli-code","Linting and Type-Checking Python CLI Code",{"path":3569,"title":3570},"\u002Fproject-setup-dependency-management\u002Flinting-and-type-checking-cli-code\u002Ftype-checking-click-and-typer-code-with-mypy","Type-Checking Click and Typer Code with mypy",{"path":3572,"title":3573},"\u002Fproject-setup-dependency-management\u002Fmanaging-cli-versioning-changelogs\u002Fautomating-changelogs-with-conventional-commits","Automating Changelogs with Conventional Commits",{"path":3575,"title":3576},"\u002Fproject-setup-dependency-management\u002Fmanaging-cli-versioning-changelogs\u002Fderiving-versions-from-git-tags-with-hatch-vcs","Deriving CLI Versions from Git Tags with hatch-vcs",{"path":3578,"title":3579},"\u002Fproject-setup-dependency-management\u002Fmanaging-cli-versioning-changelogs\u002Fexposing-version-info-and-build-metadata","Exposing Version Info and Build Metadata",{"path":3581,"title":3582},"\u002Fproject-setup-dependency-management\u002Fmanaging-cli-versioning-changelogs","Managing CLI Versioning & Changelogs",{"path":3584,"title":3585},"\u002Fproject-setup-dependency-management\u002Fmanaging-cli-versioning-changelogs\u002Fsemantic-versioning-policy-for-cli-tools","A Semantic Versioning Policy for CLI Tools",{"path":3587,"title":3588},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution\u002Fbuilding-wheels-and-sdists-for-python-clis","Building Wheels and sdists for Python CLIs",{"path":3590,"title":3591},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution\u002Fbundling-data-files-with-importlib-resources","Bundling Data Files with importlib.resources in CLIs",{"path":3593,"title":3594},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution","Packaging Python CLIs for Distribution",{"path":3596,"title":3597},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution\u002Finstalling-and-distributing-clis-with-pipx","Installing and Distributing CLIs with pipx",{"path":3599,"title":3600},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution\u002Fpublishing-a-python-cli-to-pypi","Publishing a Python CLI to PyPI",{"path":3602,"title":3603},"\u002Fproject-setup-dependency-management\u002Fpackaging-python-clis-for-distribution\u002Fwriting-pyproject-toml-metadata-for-a-cli","Writing pyproject.toml Metadata for a Python CLI",{"path":3605,"title":3606},"\u002Fproject-setup-dependency-management\u002Fpoetry-workflows-for-cli-development","Poetry Workflows for CLI Development",{"path":3608,"title":3609},"\u002Fproject-setup-dependency-management\u002Fpoetry-workflows-for-cli-development\u002Fmigrating-a-cli-from-poetry-to-uv","Migrating a Python CLI from Poetry to uv",{"path":3611,"title":3612},"\u002Fproject-setup-dependency-management\u002Fpoetry-workflows-for-cli-development\u002Fpoetry-dependency-groups-for-cli-tooling","Poetry Dependency Groups for CLI Tooling",{"path":3614,"title":3615},"\u002Fproject-setup-dependency-management\u002Fpoetry-workflows-for-cli-development\u002Fpoetry-entry-points-and-scripts-for-clis","Poetry Entry Points and Scripts for CLIs",{"path":3617,"title":3618},"\u002Fproject-setup-dependency-management\u002Fpre-commit-hooks-for-cli-projects","Pre-commit Hooks for CLI Projects",{"path":3620,"title":3621},"\u002Fproject-setup-dependency-management\u002Fpre-commit-hooks-for-cli-projects\u002Fsetting-up-pre-commit-for-python-cli-repos","Setting up pre-commit for Python CLI repos",{"path":3623,"title":3624},"\u002Fproject-setup-dependency-management\u002Fpre-commit-hooks-for-cli-projects\u002Fshipping-your-cli-as-a-pre-commit-hook","Shipping Your Python CLI as a pre-commit Hook",{"path":3626,"title":3627},"\u002Fproject-setup-dependency-management\u002Fpre-commit-hooks-for-cli-projects\u002Fwriting-local-pre-commit-hooks-in-python","Writing Local pre-commit Hooks in Python",{"path":3629,"title":3630},"\u002Fproject-setup-dependency-management\u002Fuv-for-python-cli-dependency-management","uv for Python CLI Dependency Management",{"path":3632,"title":3633},"\u002Fproject-setup-dependency-management\u002Fuv-for-python-cli-dependency-management\u002Frunning-one-off-cli-scripts-with-uv-run","Running One-Off CLI Scripts with uv run and PEP 723",{"path":3635,"title":3636},"\u002Fproject-setup-dependency-management\u002Fuv-for-python-cli-dependency-management\u002Fuv-init-vs-poetry-init-for-cli-tools","uv init vs poetry init for CLI tools",{"path":3638,"title":3639},"\u002Fproject-setup-dependency-management\u002Fuv-for-python-cli-dependency-management\u002Fuv-tool-install-vs-pipx-for-clis","uv tool install vs pipx for CLIs",{"path":3641,"title":3642},"\u002Fproject-setup-dependency-management\u002Fuv-for-python-cli-dependency-management\u002Fuv-workspaces-for-multi-package-clis","uv Workspaces for Multi-Package Python CLIs",{"path":3644,"title":3645},"\u002Fproject-setup-dependency-management\u002Fvirtual-environments-isolation-best-practices","Python CLI Env Isolation Best Practices",{"path":3647,"title":3648},"\u002Fproject-setup-dependency-management\u002Fvirtual-environments-isolation-best-practices\u002Fmanaging-virtual-environments-for-cross-platform-clis","Managing Python CLI Virtual Environments",{"path":3650,"title":3651},"\u002Fproject-setup-dependency-management\u002Fvirtual-environments-isolation-best-practices\u002Fpinning-the-python-version-for-a-cli","Pinning the Python Version for a CLI",{"path":3653,"title":3654},"\u002Fproject-setup-dependency-management\u002Fvirtual-environments-isolation-best-practices\u002Fsupporting-multiple-python-versions-with-nox","Supporting Multiple Python Versions with nox",1789736905048]