Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Pizza #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
pizzapi
pizzapy
=======

Disclaimer
-----------
This is my fork of https://github.com/gamagori/pizzapi
It's heavily modified and not well documented, but i'm going to get to that. the below example should work though.

sorry! was kind of in a rush this morning.

Setup
-----

1. install python3
2. download this repository
3. install the requirements of the repository `pip install -r requirements.txt`
4. start a python3 interpreter inside of the folder called pizzapy
5. have fun


Description
-----------

Expand All @@ -16,22 +33,20 @@ First construct a ``Customer`` object and set the customer's address:
.. code-block:: python

customer = Customer('Barack', 'Obama', '[email protected]', '2024561111', '700 Pennsylvania Avenue NW, Washington, DC, 20408')
address = Address('700 Pennsylvania Avenue NW', 'Washington', 'DC', '20408')
# or Address(*customer.address.split(',')) if you're lazy

Then, find a store that will deliver to the address.

.. code-block:: python

store = address.closest_store()
my_local_dominos = StoreLocator.find_closest_store_to_customer(customer)

In order to add items to your order, you'll need the items' product codes.
To find the codes, get the menu from the store, then search for items you want to add.
You can do this by asking your ``Store`` object for its ``Menu``.

.. code-block:: python

menu = store.get_menu()
menu = my_local_dominos.get_menu()

Then search ``menu`` with ``menu.search``. For example, running this command:

Expand All @@ -53,7 +68,7 @@ After you've found your items' product codes, you can create an ``Order`` object

.. code-block:: python

order = Order(store, customer, address)
order = Order.begin_customer_order(customer, my_local_dominos)
order.add_item('P12IPAZA') # add a 12-inch pan pizza
order.add_item('MARINARA') # with an extra marinara cup
order.add_item('20BCOKE') # and a 20oz bottle of coke
Expand All @@ -64,20 +79,15 @@ You can remove items as well!

order.remove_item('20BCOKE')

Wrap your credit card information in a ``PaymentObject``:
Wrap your credit card information in a ``CreditCard``:

.. code-block:: python

card = PaymentObject('4100123422343234', '0115', '777', '90210')
card = CreditCard('4100123422343234', '0115', '777', '90210')

And that's it! Now you can place your order.

.. code-block:: python

order.place(card)

Or if you're just testing and don't want to actually order something, use ``.pay_with``.

.. code-block:: python

order.pay_with(card)
my_local_dominos.place_order(order, card)
14 changes: 7 additions & 7 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# pizzapi documentation build configuration file, created by
# pizzapy documentation build configuration file, created by
# sphinx-quickstart on Sun Feb 4 22:05:18 2018.
#
# This file is execfile()d with the current directory set to its
Expand Down Expand Up @@ -47,7 +47,7 @@
master_doc = 'index'

# General information about the project.
project = u'pizzapi'
project = u'pizzapy'
copyright = u'2018, Arie van Luttikhuizen, Grant Gordon'
author = u'Arie van Luttikhuizen, Grant Gordon'

Expand Down Expand Up @@ -113,7 +113,7 @@
# -- Options for HTMLHelp output ------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'pizzapidoc'
htmlhelp_basename = 'pizzapydoc'


# -- Options for LaTeX output ---------------------------------------------
Expand All @@ -140,7 +140,7 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'pizzapi.tex', u'pizzapi Documentation',
(master_doc, 'pizzapy.tex', u'pizzapy Documentation',
u'Arie van Luttikhuizen, Grant Gordon', 'manual'),
]

Expand All @@ -150,7 +150,7 @@
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'pizzapi', u'pizzapi Documentation',
(master_doc, 'pizzapy', u'pizzapy Documentation',
[author], 1)
]

Expand All @@ -161,8 +161,8 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'pizzapi', u'pizzapi Documentation',
author, 'pizzapi', 'One line description of project.',
(master_doc, 'pizzapy', u'pizzapy Documentation',
author, 'pizzapy', 'One line description of project.',
'Miscellaneous'),
]

Expand Down
13 changes: 0 additions & 13 deletions pizzapi/customer.py

This file was deleted.

25 changes: 0 additions & 25 deletions pizzapi/store.py

This file was deleted.

4 changes: 2 additions & 2 deletions pizzapi/__init__.py → pizzapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .customer import Customer
from .menu import Menu
from .order import Order
from .payment import PaymentObject
from .store import Store
from .payment import CreditCard
from .store import Store, StoreLocator
from .track import track_by_order, track_by_phone
from .utils import request_json, request_xml
3 changes: 3 additions & 0 deletions pizzapi/address.py → pizzapy/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def __init__(self, street, city, region='', zip='', country=COUNTRY_USA, *args):
self.urls = Urls(country)
self.country = country

def __repr__(self):
return ", ".join([self.street, self.city, self.region, self.zip])

@property
def data(self):
return {'Street': self.street, 'City': self.city,
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions pizzapy/customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from .address import Address

class Customer:
"""The Customer who orders a pizza."""

def __init__(self, fname='', lname='', email='', phone='', address=None):
self.first_name = fname.strip()
self.last_name = lname.strip()
self.email = email.strip()
self.phone = str(phone).strip()
self.address = Address(*address.split(','))

def __repr__(self):
return "Name: {} {}\nEmail: {}\nPhone: {}\nAddress: {}".format(
self.first_name,
self.last_name,
self.email,
self.phone,
self.address,
)
8 changes: 4 additions & 4 deletions pizzapi/menu.py → pizzapy/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def search(self, **conditions):
if all(y in v.get(x, '') for x, y in conditions.items()):
print(v['Code'], end=' ')
print(v['Name'], end=' ')
print('$' + v['Price'], end=' ')
print(v['SizeCode'], end=' ')
print(v['ProductCode'], end=' ')
print(v['Toppings'])
print('$' + v['Price'])
#print(v['SizeCode'], end=' ')
#print(v['ProductCode'], end=' ')
#print(v['Toppings'])
18 changes: 13 additions & 5 deletions pizzapi/order.py → pizzapy/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class Order(object):
up all the logic for actually placing the order, after we've
determined what we want from the Menu.
"""
def __init__(self, store, customer, address, country=COUNTRY_USA):
def __init__(self, store, customer, country=COUNTRY_USA):
self.store = store
self.menu = Menu.from_store(store_id=store.id, country=country)
self.customer = customer
self.address = address
self.address = customer.address
self.urls = Urls(country)
self.data = {
'Address': {'Street': self.address.street,
Expand All @@ -35,6 +35,16 @@ def __init__(self, store, customer, address, country=COUNTRY_USA):
'PriceOrderTime': '', 'AmountsBreakdown': {}
}

@staticmethod
def begin_customer_order(customer, store, country=COUNTRY_USA):
return Order(store, customer, country=country)

def __repr__(self):
return "An order for {} with {} items in it\n".format(
self.customer.first_name,
len(self.data['Products']) if self.data['Products'] else 'no',
)

# TODO: Implement item options
# TODO: Add exception handling for KeyErrors
def add_item(self, code, qty=1, options=[]):
Expand Down Expand Up @@ -65,8 +75,6 @@ def _send(self, url, merge):
FirstName=self.customer.first_name,
LastName=self.customer.last_name,
Phone=self.customer.phone,
#Address=self.address.street

)

for key in ('Products', 'StoreID', 'Address'):
Expand Down Expand Up @@ -127,4 +135,4 @@ def pay_with(self, card=False):
}
]

return response
return response
7 changes: 5 additions & 2 deletions pizzapi/payment.py → pizzapy/payment.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import re


class PaymentObject(object):
"""A PaymentObject represents a credit card.
class CreditCard(object):
"""A CreditCard represents a credit card.

There's some sweet logic in here to make sure that the type of card
you passed is valid.
Expand All @@ -15,6 +15,9 @@ def __init__(self, number='', expiration='', cvv='', zip=''):
self.cvv = str(cvv).strip()
self.zip = str(zip).strip()

def __repr__(self):
return "Credit Card with last four #{}".format(self.number[-4:])

def validate(self):
is_valid = self.number and self.card_type and self.expiration
is_valid &= re.match(r'^[0-9]{3,4}$', self.cvv)
Expand Down
63 changes: 63 additions & 0 deletions pizzapy/store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from .menu import Menu
from .urls import Urls, COUNTRY_USA
from .utils import request_json



class Store(object):
"""The interface to the Store API

You can use this to find store information about stores near an
address, or to find the closest store to an address.
"""
def __init__(self, data={}, country=COUNTRY_USA):
self.id = str(data.get('StoreID', -1))
self.country = country
self.urls = Urls(country)
self.data = data

def __repr__(self):
return "Store #{}\nAddress: {}\n\nOpen Now: {}".format(
self.id,
self.data['AddressDescription'],
'Yes' if self.data.get('IsOpen', False) else 'No',
)

def get_details(self):
details = request_json(self.urls.info_url(), store_id=self.id)
return details

def place_order(self, order, card):
print('Order placed for {}'.format(order.customer.first_name))
return order.place(card=card)

def get_menu(self, lang='en'):
response = request_json(self.urls.menu_url(), store_id=self.id, lang=lang)
menu = Menu(response, self.country)
return menu


class StoreLocator(object):
@classmethod
def __repr__(self):
return 'I locate stores and nothing else'

@staticmethod
def nearby_stores(address, service='Delivery'):
"""Query the API to find nearby stores.

nearby_stores will filter the information we receive from the API
to exclude stores that are not currently online (!['IsOnlineNow']),
and stores that are not currently in service (!['ServiceIsOpen']).
"""
data = request_json(address.urls.find_url(), line1=address.line1, line2=address.line2, type=service)
return [Store(x, address.country) for x in data['Stores']
if x['IsOnlineNow'] and x['ServiceIsOpen'][service]]

@staticmethod
def find_closest_store_to_customer(customer, service='Delivery'):
stores = StoreLocator.nearby_stores(customer.address, service=service)
if not stores:
raise Exception('No local stores are currently open')
return stores[0]

File renamed without changes.
2 changes: 1 addition & 1 deletion pizzapi/urls.py → pizzapy/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Urls(object):
This initializes some dicts that contain country-unique information
on how to interact with the API, and some getter methods for getting
to that information. These are handy to pass as a first argument to
pizzapi.utils.request_[xml|json].
pizzapy.utils.request_[xml|json].
"""
def __init__(self, country=COUNTRY_USA):

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def run(self):

# Where the magic happens:
setup(
name='pizzapi',
name='pizzapy',

# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
Expand All @@ -73,7 +73,7 @@ def run(self):
long_description=long_description,

# The project's main homepage.
url='https://github.com/aluttik/pizzapi',
url='https://github.com/aluttik/pizzapy',

# Author details
author='aluttik',
Expand Down Expand Up @@ -121,7 +121,7 @@ def run(self):
# pip to create the appropriate form of executable for the target platform.
# entry_points={
# 'console_scripts': [
# 'pizzapi=pizzapi:main'
# 'pizzapy=pizzapy:main'
# ],
# },

Expand Down
Loading