Converting a Script into a Service

Hey everyone, I hope to get back into writing more on the regular as, been on a lazy slump that I just got off 2 hours ago. Anyways on with the blog…

If you followed a story a while ago I wrote a script that checked if Lowe’s had a heater: https://d-hanshew.medium.com/writing-code-to-check-if-lowes-has-a-heater-6e800a58959e

In this blog I detailed how I found Lowe’s API service and some of the tricks around using it to grab for my use case. The website in short didn’t easily show availability in a set of stores near a ZipCode.

So now I converted that script into a full functioning web service and wanted to go over some details.

As a Python developer I chose to use the FastAPI to quickly spin up a service and build out models. If you are unfamiliar with FastAPI the documentation is simple to follow. In this I’ll go over some additions/design choices I’ve made setting up the service.

Folder Structure

└── src
├── controllers
├── models
└── services
├── Dockerfile
├── Makefile
├── README.md
├── requirements.txt

So I do like using MVC setup to in Python as you do have access to classes and do object oriented programming (in a sense), but since I have no views I called it services which do the bulk of the work. Below is an explanation:

This setup isn’t the simplest, but since I am not persisting any data I decided to break it out into services for my third party calls.

Note: Makefiles are your friend, they simplify memorization of commands and are a good reference if you get into a new project to add a command. Use them when you can, don’t leave scripts around or chaining together large commands.

Locator Addition

So in my script I hardcoded my ZipCode and loaded up the data accordingly, however we want to search multiple nearby stores instead in the area instead of just one. Below is the snippet of code:

def __init__(self, zipcode, max_stores):
self.url = "https://www.lowes.com/store/nearbystoredetails"
self.headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41", # noqa
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Upgrade-Insecure-Requests": "1"
}
self.result = requests.get(
self.url,
headers=self.headers,
params={"maxResults": max_stores, "responseGroup": "large", "searchTerm": zipcode}
).json()

def get_store_ids(self):
store_ids = []
for store in self.result['suggestions']:
store_ids.append(store['store']['id'])
return store_ids

So the reason for using the constructor to call request is that all of the information in a request is available to the entire class. Afterwards the functions are used to be called and add what information it can find. Since this primarily a parsing for only data we need type project this setup is nice to have per class. The store lookup for product details is built similarly as well.

Future

I reached out to Lowe’s and got no response about this API if they wanted to have the code since I am using their services. However, I didn’t get a response back, but I had fun making it. If I do anything more I would attempt to add a couple features for displaying the data in a nicer format then JSON and create some sort of view. However, for now this project will sit on the sidelines for other ideas I have.

If you want to check out the entire project click the link below:
https://github.com/dhanshew72/store-lookup

Once again thanks for reading and I hope to blog more.

--

--

Senior Brogrammer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store