diff --git a/src/02-shipping/docroot/plainHttpService.php b/src/02-shipping/docroot/plainHttpService.php deleted file mode 100644 index 2a20ebf..0000000 --- a/src/02-shipping/docroot/plainHttpService.php +++ /dev/null @@ -1,11 +0,0 @@ - $price) { - $result[] = "$service: $price USD"; - } - print implode("\n", $result); -?> diff --git a/src/02-shipping/docroot/shipping.php b/src/02-shipping/docroot/shipping.php deleted file mode 100644 index 797df90..0000000 --- a/src/02-shipping/docroot/shipping.php +++ /dev/null @@ -1,12 +0,0 @@ - $baseCost * 4, - "Two Day Air" => $baseCost * 2, - "Saver Ground" => $baseCost); - return $options; - } - -?> diff --git a/src/02-shipping/docroot/xmlHttpService.php b/src/02-shipping/docroot/xmlHttpService.php deleted file mode 100644 index 59a594b..0000000 --- a/src/02-shipping/docroot/xmlHttpService.php +++ /dev/null @@ -1,15 +0,0 @@ -"; - foreach ($options as $service => $price) { - $results[] = ""; - } - $results[] = ""; - print implode("\n", $results); -?> diff --git a/src/02-shipping/shipping.py b/src/02-shipping/shipping.py index 98a37d5..63279bb 100644 --- a/src/02-shipping/shipping.py +++ b/src/02-shipping/shipping.py @@ -1,32 +1,94 @@ from __future__ import print_function import sys +import cgi from wsgiref.simple_server import make_server def main(sysargs=sys.argv[:]): - app = ShippingApp() port = 18080 - server = make_server('0.0.0.0', port, app) - print('serving {0.__class__.__name__} on port {1}'.format(app, port)) + server = make_server('0.0.0.0', port, shipping_app) + print('serving {0.__name__} on port {1}'.format(shipping_app, port)) server.serve_forever() return 0 -class ShippingApp(object): +def shipping_app(environ, start_response): + path_info = environ.get('PATH_INFO', '').strip('/') - def __call__(self, environ, start_response): - start_response('200 OK', [('content-type', 'text/plain')]) - return ['oker doke'] + handler = { + 'text': plaintext_handler, + 'xml': xml_handler, + }.get(path_info) - @classmethod - def get_shipping_options(cls, zipcode, pounds): - base_cost = (float(zipcode) / 10000.0) + (pounds * 5.0) - return { - "Next Day": int(base_cost * 4), - "Two Day Air": int(base_cost * 2), - "Saver Ground": int(base_cost) - } + if handler: + try: + return handler(environ, start_response) + except Exception: + start_response('500 Internal Server Error', [ + ('content-type', 'text/plain'), + ]) + return ['OUCH'] + else: + ret = 'nothin at {0!r}'.format(path_info) + start_response('404 Not Found', [ + ('content-type', 'text/plain'), + ('content-length', str(len(ret))) + ]) + return [ret] + + +def get_params(environ): + params = cgi.parse(fp=environ.get('wsgi.input'), environ=environ) + for key, value in params.iteritems(): + if len(value) == 1: + params[key] = value[0] + return params + + +def xml_handler(environ, start_response): + params = get_params(environ) + zipcode = int(params.get('zipcode', 0)) + pounds = int(params.get('pounds', 0)) + + ret = [''] + for service, price in get_shipping_options(zipcode, pounds).iteritems(): + ret.append(''.format(**locals())) + ret.append('') + body = '\n'.join(ret) + + start_response('200 OK', [ + ('content-type', 'text/xml'), + ('content-length', str(len(body))) + ]) + return [body] + + +def plaintext_handler(environ, start_response): + params = get_params(environ) + zipcode = int(params.get('zipcode', 0)) + pounds = int(params.get('pounds', 0)) + + ret = [] + for service, price in get_shipping_options(zipcode, pounds).iteritems(): + ret.append('{service}: {price} USD'.format(**locals())) + body = '\n'.join(ret) + + start_response('200 OK', [ + ('content-type', 'text/plain'), + ('content-length', str(len(body))) + ]) + return [body] + + +def get_shipping_options(zipcode, pounds): + base_cost = (float(zipcode) / 10000.0) + (pounds * 5.0) + return { + "Next Day": int(base_cost * 4), + "Two Day Air": int(base_cost * 2), + "Saver Ground": int(base_cost) + } if __name__ == '__main__':