Skip to content

Commit

Permalink
fix api bugs (#27)
Browse files Browse the repository at this point in the history
* removed /database and updated license validation

* adding handling to tell what keys are missing

* update the docs
  • Loading branch information
yjpictures committed Oct 20, 2023
1 parent d64b008 commit 6767559
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 85 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ Route `/ui` after the server URL will allow users to open Admin section, where a

Depending on the size and scale, we can host, manage and customize License Mate to suit your needs so you can focus on the client side of things.

[Contact us for more information!](mailto:hello@yashj.ca)
[Contact us for more information!](mailto:hello@licensemate.ca)
2 changes: 1 addition & 1 deletion docker/builds/dev-mount.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# builder base image
FROM python:3.11 as builder

LABEL maintainer="Yash Jain <hello@yashj.ca>"
LABEL maintainer="Yash Jain <yash@licensemate.ca>"

# install poetry
RUN pip install poetry==1.6.1
Expand Down
2 changes: 1 addition & 1 deletion docker/builds/dev-static.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# builder base image
FROM python:3.11 as builder

LABEL maintainer="Yash Jain <hello@yashj.ca>"
LABEL maintainer="Yash Jain <yash@licensemate.ca>"

# install poetry
RUN pip install poetry==1.6.1
Expand Down
2 changes: 1 addition & 1 deletion docker/builds/prod.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# builder base image
FROM python:3.11 as builder

LABEL maintainer="Yash Jain <hello@yashj.ca>"
LABEL maintainer="Yash Jain <yash@licensemate.ca>"

# install poetry
RUN pip install poetry==1.6.1
Expand Down
6 changes: 3 additions & 3 deletions server/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def create(self, requestDict: dict):
x = self.myCollection.insert_one(strippedDict)
return x.inserted_id
else:
raise Exception('Missing one or more of the "REQUIRED_CREATE" key(s) AND/OR "length" key in the json request')
raise Exception('Missing %s key(s) in the json request' % ', '.join(set(self.required_create + ['length']).difference(list(requestDict.keys()))))

def validate(self, requestDict: dict):
def fetch(self, requestDict: dict):
required_validate = ['_id']
if all(name in requestDict for name in required_validate):
strippedDict = {key: requestDict[key] for key in required_validate}
Expand All @@ -86,7 +86,7 @@ def renew(self, requestDict: dict):
elif x.acknowledged and x.matched_count == 0 and x.modified_count == 0:
raise Exception('Unable to find a license with _id %s' % requestDict['_id'])
else:
raise Exception('Missing "_id" AND/OR "length" key in the json request')
raise Exception('Missing %s key(s) in the json request' % ', '.join(set(required_validate).difference(list(requestDict.keys()))))

def delete(self, requestDict: dict):
required_delete = ['_id']
Expand Down
23 changes: 6 additions & 17 deletions server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generate_password_hash, check_password_hash
from database import MongoDB
import pandas as pd
from datetime import date
import os

dB = MongoDB()
Expand Down Expand Up @@ -93,8 +93,11 @@ def validate():
Authorized access: 'client', 'manager', 'admin'
"""
try:
response = dB.validate(request.args.to_dict())
return {'message': 'Validated the license', 'license-details': response}, 200
response = dB.fetch(request.args.to_dict())
if date.fromisoformat(response['created']) <= date.today() <= date.fromisoformat(response['expiry']):
return {'message': 'License is valid', 'license-details': response}, 200
else:
return {'message': 'License is expired', 'license-details': response}, 202
except Exception as e:
return {'message': str(e)}, 404

Expand All @@ -110,20 +113,6 @@ def getAll():
return {'license-database': response}, 200
except Exception as e:
return {'message': str(e)}, 404

@app.route("/api/v1/database", methods=['GET'])
@auth.login_required(role=['admin'])
def showDatabase():
"""
This can be used to get all the licenses from database.
Authorized access: 'admin'
"""
try:
response = dB.getAll()
df = pd.DataFrame(response)
return render_template('database.html', tables=[df.to_html(classes='data')], titles=df.columns.values)
except Exception as e:
return {'message': str(e)}, 404

@app.route('/ui')
def adminUI():
Expand Down
2 changes: 1 addition & 1 deletion server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "license-mate"
version = "1.0.0"
description = "Software License Manager: Docker deployable Flask REST API server with MongoDB database, simplifying software license management for easy integration and efficient tracking"
authors = ["Yash Jain <yashjain@mail.com>"]
authors = ["Yash Jain <yash@licensemate.ca>"]
license = "GPL-3.0 license"
readme = "README.md"

Expand Down
20 changes: 0 additions & 20 deletions server/static/css/style.css

This file was deleted.

77 changes: 53 additions & 24 deletions server/static/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ info:
description: |-
License Mate: Docker deployable Flask REST API server with MongoDB database, simplifying software license management for easy integration and efficient tracking.
Links:
- [Docker Hub](https://hub.docker.com/r/yjpictures/license-mate)
- [GitHub Container Registry](https://ghcr.io/yjpictures/license-mate)
- [GitHub Repository](https://github.com/yjpictures/license-mate)
contact:
email: hello@yashj.ca
email: hello@licensemate.ca
license:
name: GPL-3.0 license
url: https://github.com/yjpictures/license-mate#GPL-3.0-1-ov-file
url: https://github.com/yjpictures/license-mate/blob/main/LICENSE
version: 1.0.0
externalDocs:
description: Find out more about License Mate
url: https://github.com/yjpictures/license-mate#readme
url: https://www.licensemate.ca

tags:
- name: no access control
Expand Down Expand Up @@ -82,11 +78,17 @@ paths:
description: Provide with the license ID to get details from the database
responses:
'200':
description: Validated the license
description: Found the license in the database and license is valid
content:
application/json:
schema:
$ref: '#/components/schemas/ValidateResponse'
$ref: '#/components/schemas/ValidateResponseValid'
'202':
description: Found the license in the database and license is expired
content:
application/json:
schema:
$ref: '#/components/schemas/ValidateResponseExpired'
'404':
description: The license is not present in the database

Expand All @@ -111,6 +113,21 @@ paths:
description: Missing one or more of the "REQUIRED_CREATE" key(s) AND/OR "length" key in the json request
'406':
description: /api/v1/create requires a JSON payload
/api/v1/create-fields:
get:
security:
- BasicAuthentication: []
tags:
- manager
summary: List of required fields
description: This can be used to get a list of required fields to create a license
responses:
'200':
description: All the fields are listed under "fields" key
content:
application/json:
schema:
$ref: '#/components/schemas/CreateFieldsResponse'
/api/v1/renew:
patch:
security:
Expand Down Expand Up @@ -175,24 +192,17 @@ paths:
$ref: '#/components/schemas/GetAllResponse'
'404':
description: Unable to retrieve the licenses
/api/v1/database:
/ui:
get:
security:
- BasicAuthentication: []
tags:
- admin
summary: Retrieves all license details in a HTML table
description: This can be used to get all the license details from the database
summary: The UI for managing the licenses
description: Open /ui in browser to access the admin UI, you will need to login using the admin password
responses:
'200':
description: Successfully retrieved the licenses
content:
text/html:
schema:
type: string
example: '<html>HTML text</html>'
'404':
description: Unable to retrieve the licenses
description: Successfully loaded the website
'503':
description: Unable to locate the server-ui.js in static/js. Make sure React build was done properly.

components:

Expand Down Expand Up @@ -230,12 +240,20 @@ components:
renew_count:
type: number
example: 1
ValidateResponse:
ValidateResponseValid:
type: object
properties:
message:
type: string
example: "Validated the license"
example: "License is valid"
license-details:
$ref: '#/components/schemas/LicenseDetails'
ValidateResponseExpired:
type: object
properties:
message:
type: string
example: "License is expired"
license-details:
$ref: '#/components/schemas/LicenseDetails'
GetAllResponse:
Expand All @@ -260,6 +278,17 @@ components:
message:
type: string
example: 'Created a new license'
CreateFieldsResponse:
type: object
properties:
message:
type: string
example: "All the fields are listed under \"fields\" key"
fields:
type: array
items:
type: string
example: ['name', 'email', 'company', 'product', 'length', 'machine-node', 'machine-sn']
DeleteResponse:
type: object
properties:
Expand Down
16 changes: 0 additions & 16 deletions server/templates/database.html

This file was deleted.

0 comments on commit 6767559

Please sign in to comment.