Skip to content

Commit

Permalink
chore: python package renaming (#34)
Browse files Browse the repository at this point in the history
* chore: rename package to readme_metrics & linting

* chore: fix import statements

* fix: syntax error
  • Loading branch information
emilyskuo authored Aug 17, 2020
1 parent 680ccae commit 7f32c33
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 33 deletions.
Empty file removed metrics/__init__.py
Empty file.
3 changes: 1 addition & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ pip install readme-metrics
Just include the MetricsMiddleware into your API!

```python
from metrics.MetricsApiConfig import MetricsApiConfig
from metrics.MetricsMiddleware import MetricsMiddleware
from readme_metrics import MetricsApiConfig, MetricsMiddleware

app = Flask(__name__)

Expand Down
31 changes: 20 additions & 11 deletions metrics/Metrics.py → readme_metrics/Metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,42 @@
import threading
import requests
import json
from pprint import pprint

from werkzeug import Request
from pip._vendor.requests import __version__

from metrics import MetricsApiConfig, ResponseInfoWrapper
from metrics.PayloadBuilder import PayloadBuilder
from readme_metrics import MetricsApiConfig, ResponseInfoWrapper
from readme_metrics.PayloadBuilder import PayloadBuilder


class Metrics:
"""
This is the internal central controller class invoked by the WSGI middleware. It handles the creation, queueing,
This is the internal central controller classinvoked by the WSGI
middleware. It handles the creation, queueing,
and submission of the requests.
"""
PACKAGE_NAME: str = 'readme/metrics'
METRICS_API: str = 'https://metrics.readme.io'

def __init__(self, config: MetricsApiConfig):
"""
Constructs and initializes the ReadMe Metrics controller class with the specified configuration.
Constructs and initializes the ReadMe Metrics controller class with
the specified configuration.
:param config: Running configuration
"""

self.config = config
self.payload_builder = PayloadBuilder(config.BLACKLIST, config.WHITELIST, config.IS_DEVELOPMENT_MODE, config.GROUPING_FUNCTION)
self.payload_builder = PayloadBuilder(
config.BLACKLIST,
config.WHITELIST,
config.IS_DEVELOPMENT_MODE,
config.GROUPING_FUNCTION)
self.queue = queue.Queue()

def process(self, request: Request, response: ResponseInfoWrapper) -> None:
"""
Enqueues a request/response combination to be submitted to the ReadMe Metrics API.
Enqueues a request/response combination to be submitted
to the ReadMe Metrics API.
:param request: werkzeug.Request request object
:param response: ResponseInfoWrapper response object
"""
Expand All @@ -54,9 +60,12 @@ def _processAll(self) -> None:

# print("Posting: " + payload)

readme_result = requests.post(self.METRICS_API + "/request", auth=(self.config.README_API_KEY, ""), data = payload, headers = {
'Content-Type': 'application/json',
'User-Agent': 'readme-metrics-' + __version__
})
readme_result = requests.post(self.METRICS_API + "/request",
auth=(self.config.README_API_KEY, ""),
data=payload,
headers={
'Content-Type': 'application/json',
'User-Agent': 'readme-metrics-' + __version__
})

# print("Response: " + readme_result.text)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from metrics.Metrics import Metrics
from metrics.MetricsApiConfig import MetricsApiConfig
from metrics.ResponseInfoWrapper import ResponseInfoWrapper
from readme_metrics.Metrics import Metrics
from readme_metrics.MetricsApiConfig import MetricsApiConfig
from readme_metrics.ResponseInfoWrapper import ResponseInfoWrapper
from werkzeug import Request, Response
import io
import time
import datetime


class MetricsMiddleware:
"""
Core middleware class for ReadMe Metrics
Expand Down Expand Up @@ -35,13 +36,14 @@ def __init__(
def __call__(self, environ, start_response):
"""
Method that is called by the running WSGI server.
You should NOT be calling this method yourself under normal circumstances.
You should NOT be calling this method yourself under
normal circumstances.
"""
response_headers = {}
response_status = 0
iterable = None
req = Request(environ)

def _start_response(_status, _response_headers, *args):
write = start_response(_status, _response_headers, *args)

Expand Down Expand Up @@ -75,10 +77,8 @@ def _start_response(_status, _response_headers, *args):
req.rm_content_length = content_length
req.rm_body = content_body


iterable = self.app(environ, _start_response)


for data in iterable:
res_ctype = ''
res_clength = 0
Expand All @@ -101,4 +101,4 @@ def _start_response(_status, _response_headers, *args):
finally:
# Undocumented in WSGI spec but the iterable has to be closed
if hasattr(iterable, 'close'):
iterable.close()
iterable.close()
11 changes: 6 additions & 5 deletions metrics/PayloadBuilder.py → readme_metrics/PayloadBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
from urllib import parse

import requests
from metrics import ResponseInfoWrapper
from readme_metrics import ResponseInfoWrapper
from pip._vendor.requests import __version__
from werkzeug import Request

from metrics.util import util_exclude_keys, util_filter_keys
from readme_metrics.util import util_exclude_keys, util_filter_keys


class PayloadBuilder:
"""
Internal builder class that handles the construction of the request and response portions of the payload sent to
the ReadMe API.
Internal builder class that handles the construction of the request and
response portions of the payload sent to the ReadMe API.
...
Attributes
Expand Down Expand Up @@ -47,7 +47,8 @@ def __init__(self, blacklist: List[str], whitelist: List[str], development_mode:

def __call__(self, request: Request, response: ResponseInfoWrapper) -> dict:
"""
Builds a HAR payload encompassing the request & response data, to be sent to the ReadMe API
Builds a HAR payload encompassing the request & response data, to be
sent to the ReadMe API
:param request: Request information to use
:param response: Response information to use
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions readme_metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from readme_metrics.MetricsApiConfig import MetricsApiConfig
from readme_metrics.MetricsMiddleware import MetricsMiddleware
File renamed without changes.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
name='readme-metrics',
version='1.0.0',
author='ReadMe',
author_email = 'support@readme.io',
author_email='support@readme.io',
description='ReadMe API Metrics WSGI SDK',
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/readmeio/metrics-sdks/tree/master/packages/python",
packages=['metrics'],
packages=['readme_metrics'],
install_requires=['werkzeug', 'requests'],
)
6 changes: 3 additions & 3 deletions tests/MetricsMiddleware.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import requests
import json

from metrics import MetricsApiConfig
from metrics import MetricsMiddleware
from readme_metrics import MetricsApiConfig
from readme_metrics import MetricsMiddleware

#for this, I'm not exactly sure how to test the __call__ function
#possible options I considered was making a mock server inside this test case connected to the middleware somehow
Expand Down Expand Up @@ -33,7 +33,7 @@ def sendRequestsAtOnce(self, requestQueue):
return requestQueue

class MetricsMiddlewareTestCase(unittest.TestCase):
def setup:
def setUp(self):
self.mockserver = MockServer()

def testNoRequest(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/PayloadBuilder.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import requests
import json

from metrics import MetricsApiConfig
from metrics.PayloadBuilder import PayloadBuilder
from readme_metrics import MetricsApiConfig
from readme_metrics.PayloadBuilder import PayloadBuilder

#fetch json requests
class DataFetcher:
Expand Down

0 comments on commit 7f32c33

Please sign in to comment.