Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build SMS applications with Flask and Pindo

Remy Muhire
December 08, 2018

Build SMS applications with Flask and Pindo

A simple example using Flask and Pindo API to send an SMS.

Remy Muhire

December 08, 2018
Tweet

More Decks by Remy Muhire

Other Decks in Programming

Transcript

  1. Flask is a micro web framework written in Python. It

    is classified as a microframework because it does not require particular tools or libraries
  2. Let’s first have a taste of pindo-cli. We will use

    pindo-cli to generate an API-KEY https://github.com/pindo-io/pindo-cli
  3. Objectives By the end of this session you should be

    able to….. • Write a simple api route for sending SMS using Flask. • Use the requests to fetch external APIs. • Build a Docker image for your application.
  4. Required tools • Install the Postman Client for testing APIs

    https://getpostman.com/apps • Install Docker https://docs.docker.com/install/ • Install httpie https://httpie.org/#installation for those who prefer cli application instead of Postman
  5. Project setup First we will create a simple project called

    pindo-flask You can find the full example on Github https://github.com/kenessajr/flask-pindo mkdir pindo-flask && cd pindo-flask
  6. from flask import Flask, jsonify app = Flask(__name__) @app.route('/') def

    ping(): return jsonify( { 'status': 200, 'message': 'saccess', 'payload': 'pong' } ) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0') Then create app.py where you add the following code touch app.py or manually
  7. Flask==1.0.2 Let’s create a requirements.txt to keep our dependencies, and

    add the following lines touch requirements.txt or manually
  8. FROM python:3.6-alpine as base COPY . /app WORKDIR /app RUN

    pip install -r requirements.txt ENTRYPOINT ["python"] CMD ["app.py"] Let’s create a Dockerfile and add the following code touch Dockerfile or manually
  9. Run our app.... Starting by building our Dockerfile image Run

    with docker Yay! Your app is now running on port 5000! Now, if go to localhost:5000… docker build -t pindo-flask:latest . docker run -d -p 5000:5000 pindo-flask
  10. api: build: . ports: - "5000:5000" volumes: - .:/api Create

    the docker-compose.yml file touch docker-compose.yml or manually
  11. from flask import Flask, jsonify app = Flask(__name__) # new

    class Config: BASE_URL = 'http://178.128.165.87' @app.route('/') def ping(): return jsonify( { 'status': 200, 'message': 'success', 'payload': 'ping' } ) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0') We’re back to our app.py, add the following code to app.py
  12. from flask import Flask, jsonify, request import requests from requests.auth

    import HTTPBasicAuth app = Flask(__name__) class Config: BASE_URL = 'http://178.128.165.87' class Send(Config): def __init__(self, token, to, text, sender='Pindo'): self.token = token self.to = to self.text = text self.sender = sender self.url = '{}/sms/'.format(Config.BASE_URL) def sms(self): payload = { 'to': self.to, 'text': self.text, 'sender': self.sender } r = requests.post( self.url, auth=HTTPBasicAuth(self.token, ''), json=payload ) return '{}'.format(r.json()) Now let’s add our Class Send method to app.py,
  13. @app.route('/') def ping(): return jsonify( { 'status': 200, 'message': 'success',

    'payload': 'ping' } ) # new from . import Send @app.route('/send', methods=['POST']) def sms(): data = request.get_json() send = Send(data['token'], data['to'], data['text'], data['sender']).sms() return jsonify(send) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0') We’re already there, let’s now build our sending route api. Add the following code to app.py
  14. Now, run docker-compose down && docker-compose up --build . Your

    application is running on ….. http://localhost:5000/. Let’s test on Postman!
  15. First register and request a token through the pindo-cli. Check

    instruction on https://github.com/pindo-io/pindo-cli