-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c74956
commit 12b1471
Showing
39 changed files
with
892 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
venv/Lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[[source]] | ||
url = "https://pypi.org/simple" | ||
verify_ssl = true | ||
name = "pypi" | ||
|
||
[packages] | ||
flask-cors = "==3.0.7" | ||
|
||
[dev-packages] | ||
|
||
[requires] | ||
python_version = "3.9" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from __future__ import absolute_import, print_function |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"web": { | ||
"auth_uri": "https://dev-1329113.okta.com/oauth2/default/v1/authorize", | ||
"client_id": "0oarbd7h69A2tET9q5d6", | ||
"client_secret": "_cMHA9mfg3-tcxH0taD6p6XhbmIkJfsYr9JFvyM1", | ||
"redirect_uris": ["http://localhost:8080/authorization-code/callback"], | ||
"issuer": "https://dev-1329113.okta.com/oauth2/default", | ||
"token_uri": "https://dev-1329113.okta.com/oauth2/default/v1/token", | ||
"token_introspection_uri": "https://dev-1329113.okta.com/oauth2/default/v1/introspect", | ||
"userinfo_uri": "https://dev-1329113.okta.com/oauth2/default/v1/userinfo" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version: "3" | ||
services: | ||
mongo: | ||
image: mongo | ||
restart: always | ||
ports: | ||
- "27017:27017" | ||
environment: | ||
MONGO_INITDB_ROOT_USERNAME: mongo_user | ||
MONGO_INITDB_ROOT_PASSWORD: mongo_secret |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from flask_oidc import OpenIDConnect | ||
from flask import Flask, json, g, request | ||
from app.kudo.service import Service as Kudo | ||
from app.kudo.schema import GithubRepoSchema | ||
from flask_cors import CORS,cross_origin | ||
|
||
app = Flask(__name__) | ||
app.config.update({ | ||
'OIDC_CLIENT_SECRETS': './app/client_secrets.json', | ||
'OIDC_RESOURCE_SERVER_ONLY': False, | ||
'SECRET_KEY':'test' | ||
}) | ||
oidc = OpenIDConnect(app) | ||
CORS(app) | ||
|
||
@app.route("/kudos", methods=["GET"]) | ||
@oidc.accept_token(True) | ||
@cross_origin() | ||
def index(): | ||
return json_response(Kudo(g.oidc_token_info['sub']).find_all_kudos()) | ||
|
||
|
||
@app.route("/kudos", methods=["POST"]) | ||
@oidc.accept_token(True) | ||
@cross_origin() | ||
def create(): | ||
github_repo = GithubRepoSchema().load(json.loads(request.data)) | ||
|
||
if github_repo.errors: | ||
return json_response({'error': github_repo.errors}, 422) | ||
|
||
kudo = Kudo(g.oidc_token_info['sub']).create_kudo_for(github_repo) | ||
return json_response(kudo) | ||
|
||
|
||
@app.route("/kudo/<int:repo_id>", methods=["GET"]) | ||
@oidc.accept_token(True) | ||
@cross_origin() | ||
def show(repo_id): | ||
kudo = Kudo(g.oidc_token_info['sub']).find_kudo(repo_id) | ||
|
||
if kudo: | ||
return json_response(kudo) | ||
else: | ||
return json_response({'error': 'kudo not found'}, 404) | ||
|
||
|
||
@app.route("/kudo/<int:repo_id>", methods=["PUT"]) | ||
@oidc.accept_token(True) | ||
@cross_origin() | ||
def update(repo_id): | ||
github_repo = GithubRepoSchema().load(json.loads(request.data)) | ||
|
||
if github_repo.errors: | ||
return json_response({'error': github_repo.errors}, 422) | ||
|
||
kudo_service = Kudo(g.oidc_token_info['sub']) | ||
if kudo_service.update_kudo_with(repo_id, github_repo): | ||
return json_response(github_repo.data) | ||
else: | ||
return json_response({'error': 'kudo not found'}, 404) | ||
|
||
|
||
@app.route("/kudo/<int:repo_id>", methods=["DELETE"]) | ||
@oidc.accept_token(True) | ||
@cross_origin() | ||
def delete(repo_id): | ||
kudo_service = Kudo(g.oidc_token_info['sub']) | ||
if kudo_service.delete_kudo_for(repo_id): | ||
return json_response({}) | ||
else: | ||
return json_response({'error': 'kudo not found'}, 404) | ||
|
||
|
||
def json_response(payload, status=200): | ||
return (json.dumps(payload), status, {'content-type': 'application/json'}) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from marshmallow import Schema, fields | ||
|
||
class GithubRepoSchema(Schema): | ||
id = fields.Int(required=True) | ||
repo_name = fields.Str() | ||
full_name = fields.Str() | ||
language = fields.Str() | ||
description = fields.Str() | ||
repo_url = fields.URL() | ||
|
||
class KudoSchema(GithubRepoSchema): | ||
user_id = fields.Email(required=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from ..repository import Repository | ||
from ..repository.mongo import MongoRepository | ||
from .schema import KudoSchema | ||
|
||
class Service(object): | ||
def __init__(self, user_id, repo_client=Repository(adapter=MongoRepository)): | ||
self.repo_client = repo_client | ||
self.user_id = user_id | ||
|
||
if not user_id: | ||
raise Exception("user id not provided") | ||
|
||
def find_all_kudos(self): | ||
kudos = self.repo_client.find_all({'user_id': self.user_id}) | ||
return [self.dump(kudo) for kudo in kudos] | ||
|
||
def find_kudo(self, repo_id): | ||
kudo = self.repo_client.find({'user_id': self.user_id, 'repo_id': repo_id}) | ||
return self.dump(kudo) | ||
|
||
def create_kudo_for(self, githubRepo): | ||
self.repo_client.create(self.prepare_kudo(githubRepo)) | ||
return self.dump(githubRepo.data) | ||
|
||
def update_kudo_with(self, repo_id, githubRepo): | ||
records_affected = self.repo_client.update({'user_id': self.user_id, 'repo_id': repo_id}, self.prepare_kudo(githubRepo)) | ||
return records_affected > 0 | ||
|
||
def delete_kudo_for(self, repo_id): | ||
records_affected = self.repo_client.delete({'user_id': self.user_id, 'repo_id': repo_id}) | ||
return records_affected > 0 | ||
|
||
def dump(self, data): | ||
return KudoSchema(exclude=['_id']).dump(data).data | ||
|
||
def prepare_kudo(self, githubRepo): | ||
data = githubRepo.data | ||
data['user_id'] = self.user_id | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Repository(object): | ||
def __init__(self, adapter=None): | ||
self.client = adapter() | ||
|
||
def find_all(self, selector): | ||
return self.client.find_all(selector) | ||
|
||
def find(self, selector): | ||
return self.client.find(selector) | ||
|
||
def create(self, kudo): | ||
return self.client.create(kudo) | ||
|
||
def update(self, selector, kudo): | ||
return self.client.update(selector, kudo) | ||
|
||
def delete(self, selector): | ||
return self.client.delete(selector) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import os | ||
from pymongo import MongoClient | ||
|
||
COLLECTION_NAME = 'kudos' | ||
|
||
class MongoRepository(object): | ||
def __init__(self): | ||
mongo_url = os.environ.get('MONGO_URL') | ||
self.db = MongoClient(mongo_url).kudos | ||
|
||
def find_all(self, selector): | ||
return self.db.kudos.find(selector) | ||
|
||
def find(self, selector): | ||
return self.db.kudos.find_one(selector) | ||
|
||
def create(self, kudo): | ||
return self.db.kudos.insert_one(kudo) | ||
|
||
def update(self, selector, kudo): | ||
return self.db.kudos.replace_one(selector, kudo).modified_count | ||
|
||
def delete(self, selector): | ||
return self.db.kudos.delete_one(selector).deleted_count | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from flask import Flask | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route("/") | ||
def hello_world(): | ||
return "<p>Hello, World!</p>" |
Oops, something went wrong.