]> Vexing Labs - hector.git/commitdiff
Parameters and start to responses.
authorAdam A.G. Shamblin <adam.shamblin@pgi.com>
Thu, 13 Jun 2019 20:25:10 +0000 (14:25 -0600)
committerAdam A.G. Shamblin <adam.shamblin@pgi.com>
Thu, 13 Jun 2019 20:25:10 +0000 (14:25 -0600)
README.md
hector.py

index ef17349d2c5a914f14bfb1806fec9a3a6f7cbbc7..c206e2c8b8f83d40e0bd9249614d724782889583 100644 (file)
--- a/README.md
+++ b/README.md
@@ -53,3 +53,4 @@ previous session. Default: `None`
 * Add option to filter by host or regex
 * Optionally write to multiple files instead of just one
 * Post-process swagger to collapse on base-path
+* Add a command to trigger a write to file
index e18ed7877119bdbc51ceafa5637c74cdd958a69e..8902c4c153e7fc98ce6b1c8bc766dd8be91b33c9 100644 (file)
--- a/hector.py
+++ b/hector.py
@@ -34,7 +34,6 @@ class Hector:
         if ctx.options.hector_input != '':
             with open(ctx.options.hector_input, 'r') as swagger:
                 for doc in yaml.load_all(swagger, Loader=yaml.Loader):
-                    print(doc)
                     self._output[doc['host']] = doc
 
     def done(self):
@@ -51,11 +50,65 @@ class Hector:
         target = self._output[request.host]
         target['host'] = request.host
 
+        path = '/'.join(request.path_components)
+        if path == '':
+            path = '/'
+        method = request.method.lower()
+
         if request.scheme not in target['schemes']:
             target['schemes'].append(request.scheme)
 
         if request.path not in target:
-            target['paths'][request.path] = {}
+            target['paths'][path] = {}
+
+        if request.method not in target['paths'][path]:
+            target['paths'][path][method] = {
+                'summary': request.path
+            }
+
+        query = request.query.items()
+        if len(query) != 0:
+            target['paths'][path][method]['parameters'] = []
+            for key, val in query:
+                target['paths'][path][method]['parameters'].append({
+                    'name': key,
+                    'in': 'query',
+                    'type': typePy2OAPI(val),
+                    'example': val
+                })
+
+    def response(self, flow):
+        request = flow.request
+        response = flow.response
+
+        target = self._output[request.host]
+
+        path = '/'.join(request.path_components)
+        if path == '':
+            path = '/'
+        method = request.method.lower()
+        content_type = response.headers.get('Content-Type')
+
+        print(method)
+
+        if 'responses' not in target['paths'][path][method]:
+            target['paths'][path][method]['responses'] = {}
+
+        target['paths'][path][method]['responses'][response.status_code] = {
+            'content': {
+                content_type: {
+                    'schema': {}
+                }
+            }
+        }
+
+
+def typePy2OAPI(val):
+    types = {
+        'str': 'string',
+        'int': 'integer'
+    }
+    return types[type(val).__name__]
 
 
 addons = [