Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from lalithkota/develop
Browse files Browse the repository at this point in the history
OpenAPI: Reorganized swagger and stoplight docs
  • Loading branch information
shibu-narayanan authored Mar 2, 2024
2 parents 68a5498 + 54a55dd commit 97cb365
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 28 deletions.
24 changes: 6 additions & 18 deletions .github/workflows/openapi-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,26 @@ jobs:
python -m pip install ./spar-connector-g2pconnect
python -m pip install ./spar-mojaloop-als-oracle
python -m pip install ./spar-g2pconnect-id-mapper
- name: Generate openapi json
- name: Generate openapi jsons
run: |
mkdir -p api-docs/generated
python3 main.py getOpenAPI api-docs/generated/openapi.json
if ! [ -z "$(git status --porcelain=v1 2>/dev/null -- api-docs/generated/openapi.json)" ]; then
export OPENAPI_CHANGED="true"
echo OPENAPI_CHANGED=$OPENAPI_CHANGED >> $GITHUB_ENV
fi
- name: Generate mapper openapi json
run: |
cd spar-g2pconnect-id-mapper
mkdir -p api-docs/generated
python3 main.py getOpenAPI api-docs/generated/openapi.json
if ! [ -z "$(git status --porcelain=v1 2>/dev/null -- api-docs/generated/openapi.json)" ]; then
export MAPPER_OPENAPI_CHANGED="true"
echo MAPPER_OPENAPI_CHANGED=$MAPPER_OPENAPI_CHANGED >> $GITHUB_ENV
python3 spar-g2pconnect-id-mapper/main.py getOpenAPI api-docs/generated/mapper-openapi.json
if ! [ -z "$(git status --porcelain=v1 2>/dev/null -- api-docs/generated/mapper-openapi.json)" ]; then
export MAPPER_OPENAPI_CHANGED="true"
echo MAPPER_OPENAPI_CHANGED=$MAPPER_OPENAPI_CHANGED >> $GITHUB_ENV
fi
- name: Commit Changes
uses: EndBug/add-and-commit@v7
if: ${{ (env.OPENAPI_CHANGED == 'true') || (env.MAPPER_OPENAPI_CHANGED == 'true') }}
with:
default_author: github_actions
message: "Generated new openapi.json on push to ${{ github.event.inputs.git-ref }}"
add: "api-docs/generated/openapi.json spar-g2pconnect-id-mapper/api-docs/generated/openapi.json"
add: "api-docs/generated/openapi.json api-docs/generated/mapper-openapi.json"
- name: Setup nodejs
uses: actions/setup-node@v2
if: ${{ (env.OPENAPI_CHANGED == 'true') || (env.MAPPER_OPENAPI_CHANGED == 'true') }}
Expand All @@ -63,11 +59,3 @@ jobs:
--url https://openg2p.stoplight.io \
--branch ${{ env.BRANCH_NAME }} \
--directory api-docs/generated
- name: Publish mapper apis to stoplight
if: env.MAPPER_OPENAPI_CHANGED == 'true'
run: |
npx @stoplight/cli@5 push \
--ci-token ${{ secrets.STOPLIGHT_PROJECT_TOKEN }} \
--url https://openg2p.stoplight.io \
--branch ${{ env.BRANCH_NAME }} \
--directory spar-g2pconnect-id-mapper/api-docs/generated
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Build Status](https://github.com/OpenG2P/social-payments-account-registry/actions/workflows/test.yml/badge.svg?branch=develop)](https://github.com/OpenG2P/social-payments-account-registry/actions/workflows/test.yml?query=branch%3Adevelop)
[![codecov](https://codecov.io/gh/OpenG2P/social-payments-account-registry/branch/develop/graph/badge.svg)](https://codecov.io/gh/OpenG2P/social-payments-account-registry)
[![openapi](https://img.shields.io/badge/open--API-swagger-brightgreen)](https://validator.swagger.io/?url=https://raw.githubusercontent.com/OpenG2P/social-payments-account-registry/develop/api-docs/generated/openapi.json)
[![mapper-openapi](https://img.shields.io/badge/mapper%20open--API-swagger-brightgreen)](https://validator.swagger.io/?url=https://raw.githubusercontent.com/OpenG2P/social-payments-account-registry/develop/api-docs/generated/mapper-openapi.json)

This module implements the Account ID Mapper functionality while providing a self service portal for users to provide their bank account information linked to their ID. See more details here.

Expand Down
1 change: 1 addition & 0 deletions db_scripts/0.1.0/ddl/02.tables.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE TABLE fa_construct_strategy (
id integer NOT NULL,
strategy character varying NOT NULL,
deconstruct_strategy character varying NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone,
active boolean NOT NULL
Expand Down
8 changes: 4 additions & 4 deletions db_scripts/0.1.0/dml/01.data.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
INSERT INTO fa_construct_strategy (id, strategy, created_at, updated_at, active) VALUES (1, 'account:{account_no}@{branch}.{bank}.{type}', CURRENT_TIMESTAMP, NULL, true) ON CONFLICT DO NOTHING;
INSERT INTO fa_construct_strategy (id, strategy, created_at, updated_at, active) VALUES (2, 'account:{phone}@{wallet}.{type}', CURRENT_TIMESTAMP, NULL, true) ON CONFLICT DO NOTHING;
INSERT INTO fa_construct_strategy (id, strategy, created_at, updated_at, active) VALUES (3, 'email:{email}.{type}', CURRENT_TIMESTAMP, NULL, true) ON CONFLICT DO NOTHING;
INSERT INTO fa_construct_strategy (id, strategy, created_at, updated_at, active) VALUES (4, 'token:{sub}@nationalId', CURRENT_TIMESTAMP, NULL, true) ON CONFLICT DO NOTHING;
INSERT INTO fa_construct_strategy (id, strategy, deconstruct_strategy, created_at, updated_at, active) VALUES (1, 'account:{account_no}@{branch}.{bank}.{type}', '^account:(?P<account_no>.[^.]*)@(?P<branch>.[^.]*)\.(?P<bank>.[^.]*)\.(?P<type>.*)$', CURRENT_TIMESTAMP, NULL, true) ON CONFLICT DO NOTHING;
INSERT INTO fa_construct_strategy (id, strategy, deconstruct_strategy, created_at, updated_at, active) VALUES (2, 'account:{phone}@{wallet}.{type}', '^account:(?P<phone>.[^.]*)@(?P<wallet>.[^.]*)\.(?P<type>.[^.]*)$', CURRENT_TIMESTAMP, NULL, true) ON CONFLICT DO NOTHING;
INSERT INTO fa_construct_strategy (id, strategy, deconstruct_strategy, created_at, updated_at, active) VALUES (3, 'email:{email}.{type}', '^email:(?P<email>.[^.]*)(?P<type>.[^.]*)$', CURRENT_TIMESTAMP, NULL, true) ON CONFLICT DO NOTHING;
INSERT INTO fa_construct_strategy (id, strategy, deconstruct_strategy, created_at, updated_at, active) VALUES (4, 'token:{sub}@nationalId', '^token:(?P<sub>.[^.]*)@nationalId$', CURRENT_TIMESTAMP, NULL, true) ON CONFLICT DO NOTHING;


INSERT INTO dfsp_providers (id, name, description, code, strategy_id, created_at, updated_at, active) VALUES (1, 'Bank 1', 'Bank 1', 'dfsp1', 1, CURRENT_TIMESTAMP, NULL, true) ON CONFLICT DO NOTHING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ async def get_fa_request_status(
txn_id: str,
auth: Annotated[AuthCredentials, Depends(JwtBearerAuth())],
deconstruct: Optional[bool] = False,
deconstructCodes: Optional[bool] = True,
):
"""
Get status of a getFaRequest against the given txn_id, along with FA if success.
Expand All @@ -161,7 +162,11 @@ async def get_fa_request_status(
if deconstruct and response and response.fa:
strategies: List[FaConstructStrategy] = await FaConstructStrategy.get_all()
for strategy in strategies:
res = self.construct_service.deconstruct(response.fa, strategy.strategy)
res = self.construct_service.deconstruct(
response.fa, strategy.deconstruct_strategy
)
if deconstructCodes:
res = await self.construct_service.render_code_with_values(res)
if res:
response.fa = res
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ class FaConstructStrategy(BaseORMModelWithTimes):
__tablename__ = "fa_construct_strategy"

strategy: Mapped[str] = mapped_column(String())
deconstruct_strategy: Mapped[str] = mapped_column(String())
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import re
from typing import List

import parse
from openg2p_fastapi_common.context import dbengine
from openg2p_fastapi_common.service import BaseService
from sqlalchemy import and_, select
from sqlalchemy.ext.asyncio import async_sessionmaker

from ..models.key_value import KeyValuePair
from ..models.orm.dfsp_levels import DfspLevel, DfspLevelValue
from ..models.orm.fa_construct_strategy import FaConstructStrategy
from ..models.orm.provider import DfspProvider, IdProvider

Expand All @@ -18,11 +22,65 @@ def construct(self, values: List[KeyValuePair], strategy: str):
)

def deconstruct(self, value: str, strategy: str) -> List[KeyValuePair]:
parse_res = parse.parse(strategy, value)
if parse_res:
return [KeyValuePair(key=k, value=v) for k, v in parse_res.named.items()]
regex_res = re.match(strategy, value)
if regex_res:
regex_res = regex_res.groupdict()
if regex_res:
return [KeyValuePair(key=k, value=v) for k, v in regex_res.items()]
return []

async def render_code_with_values(
self, values: List[KeyValuePair]
) -> List[KeyValuePair]:
if not values:
return values
async_session_maker = async_sessionmaker(dbengine.get())
dfsp_levels = []
dfsp_level_values = []
return_list = []
async with async_session_maker() as session:
db_result = await session.execute(
select(DfspLevel)
.where(
and_(
DfspLevel.code.in_([value.key for value in values]),
DfspLevel.level >= 0,
)
)
.order_by(DfspLevel.level.asc())
)
dfsp_levels = list(db_result.scalars() or [])
db_result = await session.execute(
select(DfspLevel)
.where(
and_(
DfspLevel.code.in_([value.key for value in values]),
DfspLevel.level < 0,
)
)
.order_by(DfspLevel.level.desc())
)
dfsp_levels.extend(db_result.scalars() or [])
db_result = await session.execute(
select(DfspLevelValue).where(
DfspLevelValue.code.in_([value.value for value in values])
)
)
dfsp_level_values = list(db_result.scalars() or [])
for dfsp_level in dfsp_levels:
key_pair = next(value for value in values if value.key == dfsp_level.code)
key_pair.key = dfsp_level.name
return_list.append(key_pair)
if dfsp_level.level >= 0:
dfsp_level_value = next(
level_value
for level_value in dfsp_level_values
if level_value.level_id == dfsp_level.id
)
if dfsp_level_value:
key_pair.value = dfsp_level_value.name
return return_list

async def id_construct(self, id_values: List[KeyValuePair], id_provider_id: int):
id_provider: IdProvider = await IdProvider.get_by_id(id_provider_id)
strategy: FaConstructStrategy = await FaConstructStrategy.get_by_id(
Expand Down
3 changes: 2 additions & 1 deletion spar-g2pconnect-id-mapper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
[![Pre-commit Status](https://github.com/OpenG2P/social-payments-account-registry/actions/workflows/pre-commit.yml/badge.svg?branch=develop)](https://github.com/OpenG2P/social-payments-account-registry/actions/workflows/pre-commit.yml?query=branch%3Adevelop)
[![Build Status](https://github.com/OpenG2P/social-payments-account-registry/actions/workflows/test.yml/badge.svg?branch=develop)](https://github.com/OpenG2P/social-payments-account-registry/actions/workflows/test.yml?query=branch%3Adevelop)
[![codecov](https://codecov.io/gh/OpenG2P/social-payments-account-registry/branch/develop/graph/badge.svg)](https://codecov.io/gh/OpenG2P/social-payments-account-registry)
[![openapi](https://img.shields.io/badge/open--API-swagger-brightgreen)](https://validator.swagger.io/?url=https://raw.githubusercontent.com/OpenG2P/social-payments-account-registry/develop/spar-g2pconnect-id-mapper/api-docs/generated/openapi.json)
[![openapi](https://img.shields.io/badge/open--API-swagger-brightgreen)](https://validator.swagger.io/?url=https://raw.githubusercontent.com/OpenG2P/social-payments-account-registry/develop/api-docs/generated/openapi.json)
[![mapper-openapi](https://img.shields.io/badge/mapper%20open--API-swagger-brightgreen)](https://validator.swagger.io/?url=https://raw.githubusercontent.com/OpenG2P/social-payments-account-registry/develop/api-docs/generated/mapper-openapi.json)
![PyPI](https://img.shields.io/pypi/v/spar-g2pconnect-id-mapper?label=pypi%20package)
![PyPI - Downloads](https://img.shields.io/pypi/dm/spar-g2pconnect-id-mapper)

Expand Down

0 comments on commit 97cb365

Please sign in to comment.