Skip to content

Commit

Permalink
v2.1.0 - user can change swagger run port
Browse files Browse the repository at this point in the history
  • Loading branch information
ProdByGodfather committed Jul 21, 2024
1 parent d4e1d9f commit b9a958b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
Binary file modified py2proto/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file modified py2proto/__pycache__/main.cpython-310.pyc
Binary file not shown.
13 changes: 7 additions & 6 deletions py2proto/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def generate_pb2(cls, proto_file: str):
print(f"An error occurred: {e}")

@classmethod
def generate_swagger(cls, proto_file, version="1.0.0"):
def generate_swagger(cls, proto_file):
swagger_file = os.path.join(cls.output_directory, "swagger.json")
protoc.main((
'',
Expand All @@ -140,7 +140,7 @@ def generate_swagger(cls, proto_file, version="1.0.0"):

swagger = {
"swagger": "2.0",
"info": {"title": module_name, "version": version},
"info": {"title": module_name, "version": "1.0.0"},
"schemes": ["http"],
"consumes": ["application/json"],
"produces": ["application/json"],
Expand Down Expand Up @@ -187,7 +187,7 @@ def descriptor_to_json(descriptor):
"name": field.name,
"type": type_mapping.get(field.type, "string"),
"required": False,
"description": f"Field {field.name} (type: {type_mapping.get(field.type, 'string')})"
"description": f"Field {field.name}"
} for field in method.input_type.fields
],
"responses": {
Expand All @@ -205,7 +205,7 @@ def descriptor_to_json(descriptor):
return swagger_file

@classmethod
def run_swagger(cls, version="2.0.0"):
def run_swagger(cls, version="2.0.0", port=5937):
swagger_file = os.path.join(cls.output_directory, "swagger.json")

from flask import Flask, request, jsonify, send_file, abort
Expand Down Expand Up @@ -305,7 +305,8 @@ def grpc_request(service, method):
print(f"Error processing gRPC request: {e}")
return jsonify({"error": str(e)}), 500

app.run(debug=True)
print(f"Starting Swagger UI on port {port}")
app.run(debug=True, port=port)

def relation(request: str, response: str):
return ("MessageService", request, response)
return ("MessageService", request, response)
8 changes: 4 additions & 4 deletions sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class MessageResponse(ProtoGenerator):
proto_file = MessageProto.generate_proto("messageservice", "message_service")
# generate_pb2(`proto file`)
MessageProto.generate_pb2(proto_file)
# generate_wsagger(`proto file`, `version name`)
swagger_file = MessageProto.generate_swagger(proto_file, "2.0.0")
# run swagger with Flask
MessageProto.run_swagger()
# generate_wsagger(`proto file`)
swagger_file = MessageProto.generate_swagger(proto_file)
# run_swagger(`version`, `port`)
MessageProto.run_swagger(version="2.0.1", port=5937)

0 comments on commit b9a958b

Please sign in to comment.