|
|
@ -1,7 +1,8 @@ |
|
|
|
import os |
|
|
|
import requests |
|
|
|
import subprocess |
|
|
|
|
|
|
|
from flask import Flask, jsonify, send_from_directory |
|
|
|
from flask import Flask, jsonify, send_from_directory, request, abort |
|
|
|
from flasgger import Swagger |
|
|
|
from flask_restful import Api, Resource |
|
|
|
|
|
|
@ -11,6 +12,37 @@ swagger = Swagger(app) |
|
|
|
|
|
|
|
#************************************************* |
|
|
|
|
|
|
|
trusted_ips = [ |
|
|
|
"78.127.1.19", |
|
|
|
"31.39.14.228", |
|
|
|
"213.41.146.201", |
|
|
|
"89.234.177.119", |
|
|
|
"80.67.176.91", |
|
|
|
"217.108.155.85", |
|
|
|
"82.64.20.246", |
|
|
|
"109.190.2.75", |
|
|
|
"80.11.47.59" |
|
|
|
] |
|
|
|
|
|
|
|
@app.before_request |
|
|
|
def limit_remote_addr(): |
|
|
|
if request.environ['HTTP_X_FORWARDED_FOR'] not in trusted_ips: |
|
|
|
abort(jsonify(message="Et pis quoi encore "), 400) |
|
|
|
|
|
|
|
#************************************************* |
|
|
|
|
|
|
|
#variables globales |
|
|
|
|
|
|
|
paheko_ident=os.environ.get('paheko_API_USER') |
|
|
|
paheko_pass=os.environ.get('paheko_API_PASSWORD') |
|
|
|
paheko_url=os.environ.get('paheko_url') |
|
|
|
|
|
|
|
mattermost_user=os.environ.get('mattermost_user') |
|
|
|
mattermost_pass=os.environ.get('mattermost_pass') |
|
|
|
mattermost_url=os.environ.get('mattermost_url') |
|
|
|
|
|
|
|
#************************************************* |
|
|
|
|
|
|
|
@app.route('/favicon.ico') |
|
|
|
def favicon(): |
|
|
|
# return send_from_directory(os.path.join(app.root_path, 'static'),'favicon.ico') |
|
|
@ -44,32 +76,34 @@ def silence(): |
|
|
|
#************************************************* |
|
|
|
#DANGER si on touche à l'indentation pour swagger, c'est cuit! |
|
|
|
|
|
|
|
class Username(Resource): |
|
|
|
def get(self, username): |
|
|
|
""" |
|
|
|
This examples uses FlaskRESTful Resource |
|
|
|
It works also with swag_from, schemas and spec_dict |
|
|
|
--- |
|
|
|
parameters: |
|
|
|
- in: path |
|
|
|
name: username |
|
|
|
type: string |
|
|
|
required: true |
|
|
|
responses: |
|
|
|
200: |
|
|
|
description: A single user item |
|
|
|
schema: |
|
|
|
id: User |
|
|
|
properties: |
|
|
|
username: |
|
|
|
type: string |
|
|
|
description: The name of the user |
|
|
|
default: Steven Wilson |
|
|
|
""" |
|
|
|
return {'username': username}, 200 |
|
|
|
|
|
|
|
api.add_resource(Username, '/username/<username>') |
|
|
|
# class Username(Resource): |
|
|
|
# def get(self, username): |
|
|
|
# """ |
|
|
|
# This examples uses FlaskRESTful Resource |
|
|
|
# It works also with swag_from, schemas and spec_dict |
|
|
|
# --- |
|
|
|
# parameters: |
|
|
|
# - in: path |
|
|
|
# name: username |
|
|
|
# type: string |
|
|
|
# required: true |
|
|
|
# responses: |
|
|
|
# 200: |
|
|
|
# description: A single user item |
|
|
|
# schema: |
|
|
|
# id: User |
|
|
|
# properties: |
|
|
|
# username: |
|
|
|
# type: string |
|
|
|
# description: The name of the user |
|
|
|
# default: Steven Wilson |
|
|
|
# """ |
|
|
|
# return {'username': username}, 200 |
|
|
|
# |
|
|
|
# api.add_resource(Username, '/username/<username>') |
|
|
|
|
|
|
|
#************************************************* |
|
|
|
#*******PAHEKO************************************ |
|
|
|
#************************************************* |
|
|
|
|
|
|
|
class Paheko_categories(Resource): |
|
|
@ -82,20 +116,13 @@ class Paheko_categories(Resource): |
|
|
|
responses: |
|
|
|
200: |
|
|
|
description: Liste des catégories Paheko |
|
|
|
schema: |
|
|
|
id: Categorie |
|
|
|
properties: |
|
|
|
categorie: |
|
|
|
type: int |
|
|
|
description: Nom de la catégorie |
|
|
|
default: Example Category |
|
|
|
404: |
|
|
|
description: oops |
|
|
|
""" |
|
|
|
global ident_paheko, pass_paheko, url_paheko |
|
|
|
global paheko_ident, paheko_pass, paheko_url |
|
|
|
|
|
|
|
auth = (ident_paheko, pass_paheko) |
|
|
|
api_url = url_paheko + '/api/user/categories' |
|
|
|
auth = (paheko_ident, paheko_pass) |
|
|
|
api_url = paheko_url + '/api/user/categories' |
|
|
|
|
|
|
|
response = requests.get(api_url, auth=auth) |
|
|
|
|
|
|
@ -123,21 +150,14 @@ class Paheko_users(Resource): |
|
|
|
responses: |
|
|
|
200: |
|
|
|
description: Liste des membres une catégorie Paheko |
|
|
|
schema: |
|
|
|
id: Categories |
|
|
|
properties: |
|
|
|
categorie: |
|
|
|
type: string |
|
|
|
description: Id de la catégorie |
|
|
|
default: 3 |
|
|
|
404: |
|
|
|
description: oops |
|
|
|
""" |
|
|
|
|
|
|
|
global ident_paheko, pass_paheko, url_paheko |
|
|
|
global paheko_ident, paheko_pass, paheko_url |
|
|
|
|
|
|
|
auth = (ident_paheko, pass_paheko) |
|
|
|
api_url = url_paheko + '/api/user/category/'+categorie+'.json' |
|
|
|
auth = (paheko_ident, paheko_pass) |
|
|
|
api_url = paheko_url + '/api/user/category/'+categorie+'.json' |
|
|
|
|
|
|
|
response = requests.get(api_url, auth=auth) |
|
|
|
|
|
|
@ -165,21 +185,14 @@ class Paheko_user(Resource): |
|
|
|
responses: |
|
|
|
200: |
|
|
|
description: Affiche un membre de Paheko |
|
|
|
schema: |
|
|
|
id: Membres |
|
|
|
properties: |
|
|
|
numero: |
|
|
|
type: string |
|
|
|
description: Id du membre |
|
|
|
default: 1 |
|
|
|
404: |
|
|
|
description: oops |
|
|
|
""" |
|
|
|
|
|
|
|
global ident_paheko, pass_paheko, url_paheko |
|
|
|
global paheko_ident, paheko_pass, paheko_url |
|
|
|
|
|
|
|
auth = (ident_paheko, pass_paheko) |
|
|
|
api_url = url_paheko + '/api/user/'+numero |
|
|
|
auth = (paheko_ident, paheko_pass) |
|
|
|
api_url = paheko_url + '/api/user/'+numero |
|
|
|
|
|
|
|
response = requests.get(api_url, auth=auth) |
|
|
|
|
|
|
@ -191,12 +204,65 @@ class Paheko_user(Resource): |
|
|
|
|
|
|
|
api.add_resource(Paheko_user, '/paheko/user/<numero>') |
|
|
|
|
|
|
|
# #************************************************* |
|
|
|
#variables globales |
|
|
|
# TODO: accéder aux mdp pour éviter de les stocker |
|
|
|
ident_paheko='' |
|
|
|
pass_paheko='' |
|
|
|
url_paheko='https://kaz-paheko.kazkouil.fr' |
|
|
|
|
|
|
|
#************************************************* |
|
|
|
#*******MATTERMOST******************************** |
|
|
|
#************************************************* |
|
|
|
class Mattermost_message(Resource): |
|
|
|
def get(self,equipe,canal,message): |
|
|
|
""" |
|
|
|
Titre: Envoyer dans une Equipe/Canal de MM |
|
|
|
Détail: |
|
|
|
--- |
|
|
|
parameters: |
|
|
|
- in: path |
|
|
|
name: equipe |
|
|
|
type: string |
|
|
|
required: true |
|
|
|
- in: path |
|
|
|
name: canal |
|
|
|
type: string |
|
|
|
required: true |
|
|
|
- in: path |
|
|
|
name: message |
|
|
|
type: string |
|
|
|
required: true |
|
|
|
|
|
|
|
responses: |
|
|
|
200: |
|
|
|
description: Affiche un message dans un canal d'une équipe |
|
|
|
404: |
|
|
|
description: oops |
|
|
|
""" |
|
|
|
global mattermost_url, mattermost_user, mattermost_pass |
|
|
|
|
|
|
|
cmd="/mm/mmctl auth login "+mattermost_url+" --name local-server --username "+mattermost_user+" --password " +mattermost_pass |
|
|
|
output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT) |
|
|
|
cmd="/mm/mmctl post create "+equipe+":"+canal+" --message "+ "\"" + message + "\"" |
|
|
|
output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT) |
|
|
|
return "ok" |
|
|
|
|
|
|
|
api.add_resource(Mattermost_message, '/mattermost/message/<equipe>/<canal>/<message>') |
|
|
|
|
|
|
|
#************************************************* |
|
|
|
|
|
|
|
# @app.route('/test') |
|
|
|
# def test(): |
|
|
|
# return os.environ.get('mattermost_url') |
|
|
|
|
|
|
|
#************************************************* |
|
|
|
|
|
|
|
# @app.route('/print_env') |
|
|
|
# def print_environment(): |
|
|
|
# # Crée une chaîne de caractères pour stocker les variables d'environnement |
|
|
|
# env_string = "" |
|
|
|
# |
|
|
|
# # Itère sur les variables d'environnement et les ajoute à la chaîne de caractères |
|
|
|
# for key, value in os.environ.items(): |
|
|
|
# env_string += f"{key}: {value}\n" + "<br>" |
|
|
|
# |
|
|
|
# # Retourne la chaîne de caractères contenant les variables d'environnement |
|
|
|
# return env_string |
|
|
|
|
|
|
|
#************************************************* |
|
|
|
|
|
|
@ -206,6 +272,4 @@ url_paheko='https://kaz-paheko.kazkouil.fr' |
|
|
|
#************************************************* |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
#define the localhost ip and the port that is going to be used |
|
|
|
# in some future article, we are going to use an env variable instead a hardcoded port |
|
|
|
app.run(host='0.0.0.0', port=os.getenv('PORT')) |
|
|
|