Nearly two years ago I built a short URL generator that used AWS Lambda, API Gateway, S3 and CloudFront. It costs pence to run and it was a good excuse to properly get to grips with a load of serverless tech that I hadn’t had a chance to play with yet.
Until recently I'd been using curl as means of creating a short URL over the API:
curl -X POST \
-d '{"url": "https://www.jamesridgway.co.uk/build-your-own-custom-short-url-generator-using-aws"}' \
-H "x-api-key: XXXXX" \
http://jmsr.io/admin
I've been wanting to invoke the short URL API from a python script for a while, so I decided to write a python client that can act as both a CLI application and a library.
How does it work?
Its a very simple python application that uses the popular requests
library for the interactions with the API, and the built-in configparser
and argparse
packages.
I've used vcrpy
to help put in place comprehensive test coverage for the library and application. vcrpy
lets you mock out API requests and provide specific responses based on requests.
The elegance of vcrpy
is that it can be used to capture a real API interaction and replay it for the purpose of tests.
vcrpy
is a python implementation of the vcr
library for Ruby.
Usage
This is a very simple application, so I intend to keep the usage as simple and minimalist as possible.
Install:
pip install short-urls
Setup credentials:
short-urls config -d mydoma.in -a MyApiKey-l3tm31n
Off you go, create your short URLs!
short-urls create https://github.com/jamesridgway/short-urls-client python-client
Enjoy!
The python client along with the original short URLs project is all open source:
- aws-lambda-short-url
Repository for the Short URL generator that uses API Gateway, CloudFront, Lambda, Route 53 and S3 - Build your own custom Short URL generator using AWS
Blog post explaining the approach to the Short URL project - short-urls-client
Repository for the python client library for interacting with the Short URLs API - PyPi: short-urls-client
Short URLs client on PyPi.org