Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/create distribution #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/package-upload.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Upload Python Package

on:
release:
types: [created]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
39 changes: 39 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
python -m pip install .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@

# Run artifacts
*.pyc
.coverage

# Local secrets
*.env

# Python package artifacts.
*.egg-info
build/
dist/
26 changes: 0 additions & 26 deletions FlowrouteMessagingLib/APIException.py

This file was deleted.

12 changes: 0 additions & 12 deletions FlowrouteMessagingLib/Configuration.py

This file was deleted.

Empty file.
Empty file.
Empty file removed FlowrouteMessagingLib/__init__.py
Empty file.
13 changes: 8 additions & 5 deletions demo_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import pprint
from time import sleep

from FlowrouteMessagingLib.Controllers.APIController import *
from FlowrouteMessagingLib.Models.Message import *
from flowroute import Controller, Message, FlowrouteException

# Set up your API credentials
# Please replace the variables in Configuration.php with your information.
Expand All @@ -30,7 +29,7 @@
print("Flowroute, Inc - Demo SMS Python script.\n")

# Create the Controller.
controller = APIController(username=username, password=password)
controller = Controller(username=username, password=password)
pprint.pprint(controller)

# Build your message.
Expand All @@ -40,20 +39,24 @@
try:
response = controller.create_message(message)
pprint.pprint(response)
except APIException as e:
except FlowrouteException as e:
print("Send Error - " + str(e.response_code) + '\n')
pprint.pprint(e.response_body['errors'])
exit(1) # can't continue from here

# Get the MDR id from the response.
mdr_id = response['data']['id']

# Wait for message to register.
# Five seconds should be enough.
sleep(5)

# Retrieve the MDR record.
try:
# Example MDR: 'mdr1-b334f89df8de4f8fa7ce377e06090a2e'
mdr_record = controller.get_message_lookup(mdr_id)
pprint.pprint(mdr_record)
except APIException as e:
except FlowrouteException as e:
print("Get Error - " + str(e.response_code) + '\n')
pprint.pprint(e.response_body['errors'])
exit(2)
4 changes: 4 additions & 0 deletions flowroute/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

from .controller import Controller
from .exception import FlowrouteException
from .message import Message
11 changes: 11 additions & 0 deletions flowroute/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-

"""FlowrouteMessagingLib.Configuration

Copyright Flowroute, Inc. 2016
"""


class Configuration():
# The base Uri for API calls
BASE_URI = "https://api.flowroute.com/v2"
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
# -*- coding: utf-8 -*-
"""
FlowrouteMessagingLib.Controllers.APIController
"""flowroute.controller

Copyright Flowroute, Inc. 2016
Copyright Flowroute, Inc. 2016
"""
import requests

from FlowrouteMessagingLib.APIHelper import APIHelper
from FlowrouteMessagingLib.Configuration import Configuration
from FlowrouteMessagingLib.APIException import APIException
from flowroute.helper import Helper
from flowroute.configuration import Configuration
from flowroute.exception import FlowrouteException
from flowroute.message import Message


class APIController(object):
"""
A Controller to access Endpoints in the FlowrouteMessagingLib API.
class Controller():
"""Controller to access Endpoints in the Flowroute API.

Args:
username (str): Username for authentication
password (str): password for authentication
username (str): Username for authentication.
password (str): Password for authentication.
"""

def __init__(self, username, password):
self.__username = username
self.__password = password

def create_message(self, message) -> dict:
def send_message(self, message: Message) -> dict:
"""Does a POST request to /messages.

Send a message.
Expand All @@ -49,7 +48,7 @@ def create_message(self, message) -> dict:
query_builder += "/messages"

# Validate and preprocess url
query_url = APIHelper.clean_url(query_builder)
query_url = Helper.clean_url(query_builder)

# Prepare headers
headers = {
Expand All @@ -61,19 +60,19 @@ def create_message(self, message) -> dict:
response = requests.post(
url=query_url,
headers=headers,
data=APIHelper.json_serialize(message),
data=Helper.json_serialize(message),
auth=(self.__username, self.__password))
json_content = APIHelper.json_deserialize(response.content)
json_content = Helper.json_deserialize(response.content)

# Error handling using HTTP status codes
if response.status_code == 401:
raise APIException("UNAUTHORIZED", 401, json_content)
raise FlowrouteException("UNAUTHORIZED", 401, json_content)

elif response.status_code == 403:
raise APIException("FORBIDDEN", 403, json_content)
raise FlowrouteException("FORBIDDEN", 403, json_content)

elif response.status_code < 200 or response.status_code > 206: # 200 = HTTP OK
raise APIException("HTTP Response Not OK", response.status_code,
raise FlowrouteException("HTTP Response Not OK", response.status_code,
json_content)

return json_content
Expand Down Expand Up @@ -104,26 +103,28 @@ def get_message_lookup(self, record_id: str) -> dict:
query_builder += "/messages/{record_id}"

# Process optional template parameters
query_builder = APIHelper.append_url_with_template_parameters(
query_builder = Helper.append_url_with_template_parameters(
query_builder, {
"record_id": record_id,
})

# Validate and preprocess url
query_url = APIHelper.clean_url(query_builder)
query_url = Helper.clean_url(query_builder)

# Prepare headers
headers = {"user-agent": "Flowroute Messaging SDK 1.0", }
headers = {
"user-agent": "Flowroute Messaging SDK 1.0",
}

# Prepare and invoke the API call request to fetch the response
response = requests.get(
url=query_url,
auth=(self.__username, self.__password))
json_content = APIHelper.json_deserialize(response.content)
json_content = Helper.json_deserialize(response.content)

# Error handling using HTTP status codes
if response.status_code < 200 or response.status_code > 206: # 200 = HTTP OK
raise APIException("HTTP Response Not OK", response.status_code,
raise FlowrouteException("HTTP Response Not OK", response.status_code,
json_content)

return json_content
26 changes: 26 additions & 0 deletions flowroute/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-

"""FlowrouteMessagingLib.APIException

Copyright Flowroute, Inc. 2016
"""


class FlowrouteException(Exception):
"""Class that handles HTTP Exceptions when fetching API Endpoints.

Attributes:
reason (str):
The reason (or error message) for the Exception
to be raised.
response_code (int):
The HTTP Response Code from the API Request that
caused this exception to be raised.
response_body (str):
The body that was retrieved during the API request.
"""

def __init__(self, reason: str, response_code: int, response_body: str):
Exception.__init__(self, reason)
self.response_code = response_code
self.response_body = response_body
Loading