r/learnpython • u/bmw2621 • Sep 04 '19
Wrote my first API
I would love any feedback. The API simply returns homebrewing calculations. I know there still a lot to do. Brew Numbers API
10
u/Sebass13 Sep 04 '19
I'd personally recommend using URL parameters over URL routing to pass in the parameters to the endpoints; it's what they are there for, and it is better supported by libraries. If you used URL parameters, here's what a request to your URL would look like with requests:
requests.get("http://18.216.248.93/carbonation/howmuchsugar/",
params={"temp":65, "co2volume":33.0, "beervolume":10.0})That is a lot more intuitive than what it would look like with URL routing.
5
u/bmw2621 Sep 04 '19
In reading I saw
curlhas the same option. I need to figure out how to receive parameter. This would probably make error handling easier.Thanks for the feedback
2
u/gopherhole22 Sep 04 '19
I would second this. If somebody is using your api, the order of the numbers is not so intuitive. On the api end you should be able to access parameters passed as either req.body for POST and req.params or req.query for GET calls. Typically you can structure your api on what format you like. Either api.com/:id which should be passed api.com/id/233...... or as params in a url query string so api.com/id=123. I prefer the query string as I think it is more flexible to handle on the backend.
5
u/Scutterbum Sep 04 '19
Pretty cool. I'm just getting into the API world. How do you host this and how much does it cost?
5
u/bmw2621 Sep 04 '19
AWS EC2 server running Ubuntu. The t2.micro is free, but only has 1GB of memory and I think 8GB of storage, so it won't stand up to heavy usage. Buuuut AWS, as the good businessminded folks they are, can autoscale the server into something that is revenue generating for them.
I think AWS Beanstalk is intended for something like this, but I still have to figure out how to use it.
2
5
u/smartse Sep 04 '19
Nice work combining beer and python! On your last one though, the returned value is incorrectly labelled as the sugar to add not the co2 volume.
2
2
u/mrpbennett Sep 04 '19
Awesome job...do you have any tutorials etc that you used to learn how to create one...I want to create one myself as a project.
4
2
u/Tyler53121 Sep 04 '19
As a beginning coder and also homebrewer im trying to do similar things. Might take your project and expand on it if you don’t mind. Do you use temp control electronics?
1
u/bmw2621 Sep 04 '19
No, maybe one day. I would like to build my own version of a brewpi
1
u/Tyler53121 Sep 04 '19
Same! I started with fermentrack on raspberry pi and with tilt. The tilt required some coding to upload the outputs to cloud which brought me to coding in the first place! I want to find a way for my raspberry pi to do temp control for my anvil brew bucket and heat wrap hooked up to my inkbird 308c. Need to learn to build the API first though! I’m only partial introduced to python right now. Great work though!
2
u/themarxvolta Sep 04 '19
Nice! Why so many requirements though? For example you don't seem to be using Click at all
2
u/bmw2621 Sep 04 '19
I only explicitly installed Flask and flask-restful, all other requirements are their dependencies.
2
Sep 04 '19
This is kinda offtopic... where are you hosting your website?
2
u/bmw2621 Sep 04 '19
AWS EC2 server running Ubuntu. The t2.micro is free, but only has 1GB of memory and I think 8GB of storage, so it won't stand up to heavy usage. Buuuut AWS, as the good businessminded folks they are, can autoscale the server into something that is revenue generating for them.
I think AWS Beanstalk is intended for something like this, but I still have to figure out how to use it.
3
0
0
2
u/pspenguin Sep 04 '19
not OP, but it seems it's hosted in AWS - https://iplookup.flagfox.net/?ip=18.216.248.93&host=18.216.248.93
15
u/HanginToads Sep 04 '19
I gotta say man, I'm thoroughly impressed. Very detailed, very well structured code. Simple and effective.
Great job.
1
u/mrcaptncrunch Sep 04 '19
It took me a lot and I’m not 100% sure.
I discarded Nintendo homebrewing. Can you confirm it’s for homebrewing beer and not related to brew.sh?
2
u/bmw2621 Sep 04 '19
Definitely beer
1
u/mrcaptncrunch Sep 04 '19
Okay, I’m not crazy then!
I thought it was that, then thought it could be numbers on stats gathered by brew.sh or something.
3
u/theAnalyst6 Sep 04 '19
Maybe make the documentation swagger compliant?
Your documentation was brilliant anyway you might not need it.
1
1
1
u/thegreattriscuit Sep 05 '19
Great work.
To provide an excuse to refactor (and thus further justification for the tests /u/Doormatty suggested):
The way you accept parameters is a bit odd to me. Totally valid, but *I* would like to call the API with named parameters like this: http://18.216.248.93/all/no_correction?initialGrav=1.065&finalGrav=1.014
Also my entire enterprise and the livelihoods of all 3000 of my employees depends on a 30 million line COBOL application I paid some college kids to write last summer, and *that* application depends on your API as it's currently implemented, so you can't break it. So maybe you should implement some versioning system, so users can decide which version of the API to call?
1
u/forward_epochs Sep 17 '19
Howdy - and nice job! I'd love to contribute, any chance you'd be looking for some assistance? I stumbled across this post a while back and bookmarked it because it looked interesting. I'd call myself intermediate with Python - I'm looking for some projects outside of my own to which I can contribute. If you post some issues on the GitHub repo, maybe I can produce some pull requests you'd want to use. No hard feelings if not!
4
Sep 04 '19 edited Aug 30 '25
[removed] — view removed comment
2
u/bmw2621 Sep 04 '19
Your the second to mention Black formatter. I'll be researching it. How do you suggest fixing imports (I'm tracking importing * is bad juju, but I'm sticking with it for now as long as all my formulas fit in one file.
Numbers aren't magical they are published my Dr. Michael Hall in the article I cite at the top of the formulas.py, README.md, and the first paragraph of the documentation site linked above.
Thanks for the feedback, I'll fix my PEP8 violations.
2
u/Metaroxy Sep 04 '19
When /u/lanemik said that the numbers are "super magical" I'm sure the intention was not to imply that you came up with them. Rather that the code does not document what they are. So if you declare each number as a constant and then use that named constant in your formula, it will be clearer what it does and self-documenting.
73
u/Doormatty I get paid to write Python! Sep 04 '19
Fantastic job!
Took a quick look through the code, and it looks concise and self-explanatory!
I'd now recommend writing some tests, so you can be sure that when you refactor it (because you know you're going to want to), you can be sure it's still working correctly.
Again, awesome job!
Edit: Also love the website for it! I'm not a brewing person at all, but I could easily understand everything.