Skip to content

Latest commit

 

History

History
102 lines (91 loc) · 5.92 KB

File metadata and controls

102 lines (91 loc) · 5.92 KB

Developing a number-crunching application

Generating a Swagger API documentation

  1. Open the username-python-microservice repository in Visual Studio Code.
  2. Install the latest Flask-RESTPlus by
    • Adding flask-restplus to the bottom of requirements.txt.
    • Adding flask-restplus = "*" to the bottom of Pipfile.
  3. Open the server/__init__.py file, perform the following modifications and save your work.
    • Add from flask_restplus import Api to the import list at the top of the file.
    • Add the following line below the definition of the app object in line 6.
      api = Api(app, title='My first Python API', version='1.0', doc='/apidocs/', description='A number-crunching API')
  4. Create a new math.py file in server/routes/ with the code shown below.
    from server import api
    from flask import jsonify
    from flask_restplus import Resource
    
    @api.route('/double/<int:number>')
    @api.doc(params={'number': 'Number to be doubled.'}, description='This method doubles the input.')
    class DoubleNumber(Resource):
        def get(self, number):
            return jsonify(result=2 * number)
  5. Commit your changes to the requirements.txt, Pipfile, server/__init__.py and math.py files.
  6. Sync your commits with the GitHub repository.
  7. Track the deployment progress in the Continuous Delivery Pipeline.

Exploring the Swagger API documentation

  1. Go to http://username-python-microservice.mybluemix.net/apidocs/.
  2. Observe how the header in the top reflects the line recently added to server/__init__.py.
  3. Click on the Default namespace and the /double/{number} endpoint.
  4. Observe how the blue card reflects the contents of server/routes/math.py.
  5. Try to guess from the documentation the purpose and data input format of this method.
  6. Click Try it out, enter an input in the text box and hit Execute.
  7. Try several values for the required field and observe the response.
    • A string: word
    • A symbol: +
    • A floating point number: 1.5
    • An integer number: 42
  8. Go to http://username-python-microservice.mybluemix.net/double/42 and observe the response.

Creating number-crunching services

  1. Open the username-python-microservice repository in Visual Studio Code.
  2. Install the latest NumPy by
    • Adding numpy to the bottom of requirements.txt.
    • Adding numpy = "*" to the bottom of Pipfile.
  3. Create a new file inside server/services/ and name it math_services.py.
  4. Start math_services.py by importing the Python module you have just added to requirements.txt.
    import numpy as np
  5. Implement a service that converts a string with comma-separated integer values to a Python list of integers.
  6. Implement a service that converts a list of integer values to an integer NumPy array.
  7. Implement a service that calculates the mean of a list of integer values.
  8. Commit your changes to the requirements.txt, Pipfile and math_services.py files.
  9. Sync your commits with the GitHub repository.
  10. Track the deployment progress in the Continuous Delivery Pipeline.

Configuring Python deployment environment

  1. Open the username-python-microservice repository in Visual Studio Code.
  2. Create a new file in the main repository directory named runtime.txt by clicking the New File button.
  3. Check the latest Python buildpack version available at IBM Cloud.
  4. Add python-X.Y.Z to runtime.txt reflecting the latest available version.
    • Example: python-3.6.4
  5. Commit your changes to the runtime.txt file.
  6. Sync your commits with the GitHub repository.
  7. Track the deployment progress in the Continuous Delivery Pipeline.

Linking number-crunching services to API resources

  1. Open the username-python-microservice repository in Visual Studio Code.
  2. Open math.py and remove the @api.route('/double/<int:number>') code block.
  3. Add from server.services import math_services as ms to the import list at the top of the file.
  4. Add below my_list = list() to create an empty Python list.
  5. Create an API method that prints the content of the list.
  6. Create an API method that resets the content of the list.
  7. Create an API method that reverses the content of the list.
  8. Create an API method that sorts the content of the list.
  9. Create an API method that extends the content of the list.
  10. Create an API method that replaces the content of the list.
  11. Create an API method that calculates the mean of the content of the list.
  12. Place your mouse cursor over csv_to_list() and calculate_mean() to read the documentation.
  13. In the end, your file should look like app/custom-math.py.
  14. Commit your changes to the math.py file.
  15. Sync your commits with the GitHub repository.
  16. Track the deployment progress in the Continuous Delivery Pipeline.