A simple wrapper around the kroger api.
Provides utilities to: - Search for products - Search for locations - Add a product to a users card (requires customer authorization)
This project was developed on Python 3.7.5
A requirements.txt is provided with the projects dependencies.
In addition, to use the KrogerCustomerClient webscraper to automatically authorize your kroger account, you must have a chrome-driver installed and on your PATH for selenium to use.
You must create a developer account (https://developer.kroger.com/) and register an application.
To use the ServiceKrogerClient, 2 environment variables from your application configuration are required:
- KROGER_CLIENT_ID
- KROGER_CLIENT_SECRET
In addition, to use the CustomerKrogerClient, you must set 3 additional environment variables:
WARNING: This should only be used for testing. You should never request a user's username/password. In the future, this feature will be removed.
- KROGER_REDIRECT_URI
- KROGER_USERNAME
- KROGER_PASSWORD
The config module contains all the variables needed to intialize the client. In addition to the environment variables defined above, it includes a helper variable encoded_client_token, a base64 encoded combination of client_id and client_secret used in client authentication.
Note: Clients use TTL caching (as stored
access_token.cachepickle file) to prevent unnecessary calls to authenticate.
# Service Client
import config
client = KrogerServiceClient(config.encoded_client_token)# Customer Client
import config
client = KrogerServiceClient(config.encoded_client_token, config.redirect_uri, config.customer_username, config.customer_password)KrogerServiceClient has two methods:
search_products(term=None, location_id=None, product_id=None, brand=None, fulfillment='csp', limit=5)- returns a list of Productsget_locations(zipcode, within_miles=10, limit=5, chain='Kroger')- returns a list of Locations
KrogerCustomerClient contains the same methods as KrogerServiceClient and one additional method:
Products - Full api response not implemented. See full response returned by api here
Locations - Full api response not implemented. See full response returned by api here
add_items_to_cart(items)
import config
client = KrogerServiceClient(config.encoded_client_token, config.redirect_uri, config.customer_username, config.customer_password)
products = customer_client.search_products(term="milk", limit=1, location_id='02600845')
items = [{'upc': products[0].upc, 'quantity':3}]
customer_client.add_items_to_cart(items)There is a full example in example.py
- Remove crawler that authorizes customer client and replace it with and api around the standard authorization path (using the redirect_uri)
- Add functionality to refresh customer client token
- Add customer client function to get coupons
- Figure out a less clunky version of cacheing