Géraldine Geoffroy


    Packages installation

    pip install Flask Flask-Cors Flask-RESTful Flask-JSON flask-restful-swagger python-dotenv pandas requests

    Library: flask

    from flask_json import FlaskJSON, json_response
    import pandas as pd
    from flask_cors import CORS
    from flask_restful_swagger_2 import Api, swagger, Schema
    from flask_restful import Resource, reqparse
    from dotenv import dotenv_values
    import json
    import config
    from flask import Flask, jsonify, abort, render_template,url_for,request,session, redirect, send_from_directory, Response, Blueprint
    import requests
    env = dotenv_values(".env")
    """ Example for .env
    app = Flask(__name__)
    cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
    port = env['APP_PORT']
    host = env['APP_HOST']
    api_version = env['API_VERSION']
    url_subpath = env['URL_SUBPATH']
    class ReverseProxied(object):
      #Class to dynamically adapt Flask converted url of static files (/static/js...) + templates html href links according to the url app path after the hostname (set in cnfig.py)
      def __init__(self, app, script_name=None, scheme=None, server=None):
        self.app = app
        self.script_name = script_name
        self.scheme = scheme
        self.server = server
      def __call__(self, environ, start_response):
        script_name = environ.get('HTTP_X_SCRIPT_NAME', '') or self.script_name
        if script_name:
          environ['SCRIPT_NAME'] = script_name
          path_info = environ['PATH_INFO']
          if path_info.startswith(script_name):
            environ['PATH_INFO'] = path_info[len(script_name):]
        scheme = environ.get('HTTP_X_SCHEME', '') or self.scheme
        if scheme:
          environ['wsgi.url_scheme'] = scheme
        server = environ.get('HTTP_X_FORWARDED_SERVER', '') or self.server
        if server:
          environ['HTTP_HOST'] = server
        return self.app(environ, start_response)
    api = Api(app, title='SCD-UCA Middleware Koha-Primo', api_version=f'{api_version}', api_spec_url='/api/swagger', base_path=f'{host}:{port}')
    app.wsgi_app = ReverseProxied(app.wsgi_app, script_name=url_subpath)
    # example function for data processing
    def extract_koha_item(item):
      result = {}
      if (item['checked_out_date'] is None) & (item["holding_library_id"] == item["home_library_id"]):
        result['checked_out_date'] = 'Disponible'
      elif (item['checked_out_date'] is None) & (item["holding_library_id"] != item["home_library_id"]):
        result['checked_out_date'] = 'Indisponible : en transfert'
      #elif item['checked_out_date'] is not None:
        result['checked_out_date'] = 'Indisponible : emprunté'
      result["home_library_id"] = mapping_bibs[item["home_library_id"]]
      result["location"] = item["location"]
      result["callnumber"] = item["callnumber"]
      if item["external_id"].startswith('HDL'):
        result["serial_issue_number"] = f"Etat de collection : {item['serial_issue_number']}" # si pério on affiche l' état de coll
        if item["serial_issue_number"] is not None:
          result["serial_issue_number"] =  item["serial_issue_number"] # si pas pério et si champ non vide on affiche la description
        result["item_type_id"] = mapping_codes_types_pret[item["item_type_id"]] # si pas pério on affiche la régle de prêt
      return result
    def output_json(data, code):
      return json_response(data_=data, status_=code)
    class HelloWorld(Resource):
      def get(self):
        # Default to 200 OK
        return jsonify({'msg': 'Hello world'})
    class KohaApiPubliqueBibliosItems(Resource):
      def get(self, biblio_id):
        url = f"{preprod_koha_api_public}biblios/{biblio_id}/items"
        response = requests.request("GET", url).text
        data = json.loads(response)
        new_data = [extract_koha_item(i) for i in data]        
        return jsonify(new_data)
    api.add_resource(HelloWorld, f'/api/{api_version}', f'/api/{api_version}/hello')
    api.add_resource(KohaApiPubliqueBibliosItems, f'/api/{api_version}/koha/biblios_items/<string:biblio_id>')
    if __name__ == '__main__':
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.