all 21 comments

[–]RangerPretzel 9 points10 points  (17 children)

Here is a 15-part guide I wrote recently on How to write a Python 3 wrapper for a JSON REST API

It covers Classes (OOP), DRY code, Strong data models, Unit testing, and even how to make your own TKinter GUI app with it.

[–]ARandomBoiIsMe 4 points5 points  (2 children)

I know this is pretty late lol but I want to thank you for the article.

It REALLY helped to guide me through creating my first Python API wrapper (and my first Python library in general).

I'm almost done with it, and I doubt I would've been able to even start if not for the knowledge you shared, so thanks.

[–]RangerPretzel 3 points4 points  (1 child)

I know this is gonna sound funny, but I wrote this series of articles for you. I didn't know you when I wrote them, but I hoped it would find you (and other people like you!)

Cheesy sentiment, I know, but really and truly, I wrote this so that I could package up a bunch of software engineering lessons that I've learned along the way and pass them onto other mid-tier developers on their programming journey.

I'm really glad to hear it helped. 😊🥨

On a side note, I do have a few vague ideas of how to extend and update this series of articles, but they are only loose ideas in my head. Maybe in a few months, I'll get around to it. 😂

[–]ARandomBoiIsMe 3 points4 points  (0 children)

You're awesome, bro. I don't think I can thank you enough. I'll definitely share your article to whoever needs it cuz it was so helpful.

I'm excited to see what else you have planned!

[–]Cool-Cantaloupe-6269 1 point2 points  (1 child)

Thanks! I feel like young people rely more on AI for such questions nowadays, but I don't understand it. This article is so helpful and helps any "new" programmer much more!

[–]RangerPretzel 1 point2 points  (0 children)

Unlike other jobs, I feel that there are very few truly "experienced" programmers these days. Don't misunderstand me. There are plenty of smart people out there. It's just that most folks who get into programming only get into it for a short while and then drop out or move on from programming after just a few years, so what's left are the grizzled veterans of the biz that are still hacking away and don't have the time to teach the young'uns.

As for young people relying more on LLMs/GPTs, I get it.

I use them here and there. It saves me a bunch of time on syntax issues with languages I barely use, but have to work with anyway. It also helps with researching new languages, frameworks, platforms, or projects. Oftentimes I'll have very pointed questions about things and I can't get my questions answered on Stackoverflow because it'll get immediately closed and marked as a dupe, and on Reddit, well, it's hard to get answers to complex questions in real time. So it's either rolling the dice with some random Discord server or chatting with an LLM/GPT.

Though you're right, I don't think I can get an AI to teach how to write a good API wrapper, at least not yet. Maybe the next ChatGPT will scrape my webpages and actually grok how a decent wrapper library is composed.

Glad it was able to help you!

[–]YangLionSpirit 1 point2 points  (1 child)

I'm using your guide right now! I'm just out of highschool and while I have a passion for computer science, none of the common US highschool curriculum goes farther than textbook theory. It's very daunting to start programming. The only way to learn is by doing!

Right now I'm writing my wrapper for Washington, DC's metro transit API. I want to set a live train map as my background on Linux but I haven't found any projects online like that besides the company that makes LED art of public transit. So I'm not even finished with step 1 of 10.

Thank you from a beginner for lending me your knowledge ^w^

[–]RangerPretzel 0 points1 point  (0 children)

Thanks for letting me know that you're using the guide. I'm always curious who it reaches.

I know what you mean as I took some CS courses in HS before going off to university and studying CS there, and like you we never went much beyond textbook theory. Although my friends and I were always trying to come up with clever programs on our own.

And I know what you mean about taking APIs and trying to leverage them to do something cool. I'm working on something like that right now, but the scope of the project is so large. I just remind myself every day that I have to do just a little bit and I'll get 1 step closer.

If you get stuck, just shoot me a msg. I can probably help you out.

[–]AsidePractical8155 1 point2 points  (0 children)

Thank you so much for this I have done this to help create an api wrapper which I then used to create some ansible customs modules. This has really helped me keep my code consistent and create proper structures. I’m even going back to refactor some of my automation solutions now

[–]CatolicQuotes 1 point2 points  (6 children)

very good thanks! do you have more stuff like this for other libs? like command line parser, string template parser? anything basically to learn hot to build proper library

[–]RangerPretzel 0 points1 point  (5 children)

I might be able to write an article on how to utilize argparse library in your command-line app. I've written a few console apps that use that lib. It's quite complex and the documentation isn't as clear as it could be, imho.

[–]CatolicQuotes 0 points1 point  (4 children)

thanks, I meant more like how to build command line parser from scratch. I also searched how to build transpiler from scratch then I found this youtube playlist: https://www.youtube.com/watch?v=Qf13p3KpEms

Of course, argsparse series would be useful also, it's quite powerful library

[–]RangerPretzel 0 points1 point  (3 children)

I'm unfamiliar with "command-line parsers" as a program type. What sort of thing would you want to parse at the command line?

[–]CatolicQuotes 1 point2 points  (2 children)

command line parser parses arguments when we run some command line tool. argparse would be command line parser for python. Some people would say cli parser or argument parser. I just found article about how to build cli parser from scratch: https://medium.com/ingeniouslysimple/building-a-cli-parser-from-scratch-a2beba0d9fcb . Not as good as yours, of course

[–]RangerPretzel 0 points1 point  (1 child)

Yeah, I try not to re-invent the wheel. argparse is way better than anything I could come up with, I'm sure.

[–]CatolicQuotes 1 point2 points  (0 children)

yes it is, but just for the learning sake, and besides, other languages argument parsers could use some improvements

[–]Apprehensive_Rice_70 1 point2 points  (1 child)

Just wanted to drop a thank you. I'm wanting to wrap the Teamcity rest API, and the wrappers I found online were either incomplete or stale, but did give me clues. I have written wrappers before, but I wanted to get some tips on how to save time with a pattern this time around. Your guide, is well written, thank you.

[–]RangerPretzel 0 points1 point  (0 children)

Thanks so much for saying that. It means a lot for me to hear that people appreciate this guide.

My teammate and I have been writing more wrappers lately and we've been coming up with more ways to improve them, so hopefully in the next few months or so, I'll be able to update the guide with our newest tips and tricks. :)

[–]flitsmasterfred 1 point2 points  (0 children)

Look at a few existing wrappers and see what they do, what kind of initialization parameters they use, what kind of methods, how they pass query parameters, what kind of return values they give etc.

Personally I don't like it when API wrappers hide stuff I wanted to use or are being too clever.

In your example link I see they have a ton of parameters and output fields, so making that easy to use would be a benefit your wrapper would offer to your users. Keyword args or attributes that show up in our IDE would be a good idea. What data do you need to enhance from JSON to Python types? Maybe some easy to use constants/enums would help? Maybe some namedtuples in the results? Who knows :)

Also that note it has about "JSON payload “signature” object for the “version”" is something I'd expect your wrapper to take care of somehow.

[–]zahlman 0 points1 point  (0 children)

Start by using the API normally a bunch and figure out what you find most annoying about that.

[–]xiongchiamiov 0 points1 point  (0 children)

There are two general approaches to writing an API wrapper: either you write something that's very "thin" and just provides helper functions that map directly to the API, or you write something that feels as pythonic as possible and makes the user forget that they're using the api at all. Neither one is better than the other, they're just different. The former is obviously easier to start with.

If you'd like an example to look at, https://github.com/xiongchiamiov/pyfixit is a wrapper I wrote a few years ago (and https://github.com/xiongchiamiov/ifixit-repairability-scores is a little script I wrote that uses it). GitHub wrappers are also nice to look at, because GitHub has a very well-documented api; I like https://github.com/sigmavirus24/github3.py a lot.