Python3 GitHub CLI tool as a refresher
Tags: tech, python, cli
Published: 2023-02-01 02:38:35

---
title: Python3 GitHub CLI tool as a refresher
published: true
date: 2021-11-04 05:29:00 UTC
tags: Tech,python,cli
canonical_url: https://tyrel.dev/blog/2021/11/python3-github-cli-tool-as-a-refresher.html
---

It's no lie that I love terminals. I wish I could live on a terminal and never really need to see a GUI application again.

Last night I migrated a lot of my old code from one GitLab account to another ([tyrelsouza](https://gitlab.com/tyrelsouza) to [tyrel](https://gitlab.com/tyrel)) in an effort to clean up some of my usernames spread across the world. While doing that I noticed my [django-dbfilestorage](https://gitlab.com/tyrel/django-dbfilestorage) Python module that has been sitting and rotting for three years. I played around a little bit in order to port it to Python 3.9, but I ran into some base64 errors. I tried a little bit but it was late and I couldn't figure it out. My resolve is that I have been away from Python for too long so the little things - that I knew and love - had fallen away. I mentioned this to my friend Alex and he said _"make a barebones github cli (readonly?) with issue viewer, and stats display"_. I've embarked on a journey to refresh my Python well enough to repair DBFS.

 ![Me: "okay python friends, what should I make as a quick refresher into the Python world?" alex: "maybe: barebonx github cli (reasdonly?) with issue viewer and stats display"](https://tyrel.dev/blog/images/2021/11/github_cli-alex_prompt.png)

I knew I wanted to use <tt>httpx</tt> as my network client library, it's new, fast, and I have a couple friends who work on it. I started with a barebones <tt>requirements.in</tt> file, tossed in <tt>invoke</tt>, <tt>pytes</tt>t, and <tt>black</tt>. From there I used <tt><span>pip-compile</span></tt> to generate my <tt>requirements.txt</tt> - (a tip I picked up recently while adding Pip-Compile support to the [Tidelift CLI](https://tidelift.com/cli)) and I was good to go.

The [docs for the GitHub API](https://docs.github.com/en/rest/overview/resources-in-the-rest-api#schema) are pretty easy to read, so I knew all I really needed to do was set my <tt>Accept</tt> header to be Version3 and I could view the schema. With the schema saved to a <tt>.json</tt> file I then wrote a <tt>GHub</tt> class to pull this data down using <tt>httpx.client.Client.get</tt>, super simple! The only two endpoints I care about right now are the user and repos endpoints, so I made two <tt>get_</tt> functions for each. After a little bit of work - which I won't bore you with the super intricate details - I have a functional cli.py file. For now, the only interaction is a propmt from <tt>rich</tt> for a username, and then you get a fancy table (also from <tt>rich</tt>) of the first page of results of repos, stargazer/watchers/forks counts, and a description.

 ![Prompting for the username and showing my table of repositories.](https://tyrel.dev/blog/images/2021/11/github_cli-prompting_and_table.png)

Prompting for the username and showing my table of repositories.

It was a fun evening of learning what's changed in Python3 since I last touched it, especially as I've spent the majority of my career in Python2.7. Type annotations are super awesome. I'll probably pick it up again once I get some more free time later in the week. It's also nice blog fodder! I already have a million things I want to do next - pagination, caching, some more interaction.

 ![Showing py.test running](https://tyrel.dev/blog/images/2021/11/github_cli-pytest_running.png)

Showing py.test running

I know the tool I'm writing is nothing special, especially with their own [cli](https://github.com/cli/cli) now, but I'm not looking at reinventing the wheel!

Check out the code so far on my [GitLab](https://gitlab.com/tyrel/ghub) (_heh_, ironic it's there).

Dependencies: [httpx](https://www.python-httpx.org/), [pip-tools](https://github.com/jazzband/pip-tools), [black](https://github.com/psf/black), [invoke](https://www.pyinvoke.org/), [pytest](https://docs.pytest.org/en/6.2.x/), [pytest-httpx](https://pypi.org/project/pytest-httpx/), [rich](https://github.com/willmcgugan/rich).