Skip to content

Commit

Permalink
added api call to initialize app
Browse files Browse the repository at this point in the history
  • Loading branch information
miltontony committed Feb 28, 2014
1 parent 8da581f commit 589e3e9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
27 changes: 18 additions & 9 deletions shortener/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ def create_url(self, request):
yield request.setResponseCode(http.CREATED)
returnValue({'short_url': short_url})

@handler('/<string:short_url>', methods=['GET'])
@handler('/api/init', methods=['GET'])
@inlineCallbacks
def resolve_url(self, request, short_url):
row = yield self.get_row_by_short_url(short_url)
if row and row['long_url']:
request.setResponseCode(http.MOVED_PERMANENTLY)
request.setHeader(b"location", row['long_url'].encode('utf-8'))
else:
request.setResponseCode(http.NOT_FOUND)
returnValue({})
def init_account(self, request):
'''
Initializes the account and creates the database tables
'''
result = yield self.create_tables()
returnValue(result)

@inlineCallbacks
def create_tables(self):
Expand All @@ -57,6 +55,17 @@ def create_tables(self):

returnValue({'created': not already_exists})

@handler('/<string:short_url>', methods=['GET'])
@inlineCallbacks
def resolve_url(self, request, short_url):
row = yield self.get_row_by_short_url(short_url)
if row and row['long_url']:
request.setResponseCode(http.MOVED_PERMANENTLY)
request.setHeader(b"location", row['long_url'].encode('utf-8'))
else:
request.setResponseCode(http.NOT_FOUND)
returnValue({})

@inlineCallbacks
def shorten_url(self, long_url, user_token=DEFAULT_USER_TOKEN):
if not user_token:
Expand Down
18 changes: 17 additions & 1 deletion shortener/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@ def setUp(self):
site = Site(self.service.app.resource())
self.listener = reactor.listenTCP(0, site, interface='localhost')
self.listener_port = self.listener.getHost().port
self._drop_tables()
self.conn = yield self.service.engine.connect()
self.addCleanup(self._drop_tables)
yield self.service.create_tables()
self.addCleanup(self.listener.loseConnection)
self.addCleanup(self.pool.closeCachedConnections)

@inlineCallbacks
def tearDown(self):
yield self.conn.close()
self._drop_tables()
yield self.listener.loseConnection()

def make_url(self, path):
return 'http://localhost:%s%s' % (self.listener_port, path)

Expand Down Expand Up @@ -152,3 +158,13 @@ def test_short_url_sequencing(self):

result = yield self.service.get_row_by_short_url('q3R')
self.assertEqual(result['long_url'], url + '55')

@inlineCallbacks
def test_account_init(self):
self._drop_tables()
resp = yield treq.get(
self.make_url('/api/init'),
allow_redirects=False,
pool=self.pool)
result = yield treq.json_content(resp)
self.assertFalse(result['created'])

0 comments on commit 589e3e9

Please sign in to comment.