Skip to content

jtbricker/python-kroger-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-kroger-client

A simple wrapper around the kroger api.

Table of Contents

Features

Provides utilities to: - Search for products - Search for locations - Add a product to a users card (requires customer authorization)

Setup

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.

Usage

Set environment variables

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:

  1. KROGER_CLIENT_ID
  2. 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.

  1. KROGER_REDIRECT_URI
  2. KROGER_USERNAME
  3. KROGER_PASSWORD

Import the config module

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.

Initialize the client

Note: Clients use TTL caching (as stored access_token.cache pickle 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)

Methods

KrogerServiceClient has two methods:

  • search_products(term=None, location_id=None, product_id=None, brand=None, fulfillment='csp', limit=5) - returns a list of Products
  • get_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:

Models

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)

Example

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

TODOS

  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages