카테고리 없음

금융플랫폼 App이 타회사의 운영계 서버로부터 고객번호별 대출 계약 개수를 호출하는 I/F를 REST API 방식으로 구현 250417

mezzanineX 2025. 4. 17. 07:14

q1: 금융플랫폼 App이 타회사의 운영계 서버로부터 고객번호별 대출 계약 개수를 호출하는 interface를 REST API 방식으로 구현

a1:

요구 조건 요약
• 금융플랫폼 App이
• 타회사의 운영계 서버로부터
• 고객번호별 대출 계약 개수를 호출
• REST API 방식으로 구현



1. Client 측 (금융플랫폼 App) - API 호출 코드 예시

import requests

# 운영계 서버의 API 엔드포인트
API_URL = "https://api.partnerbank.co.kr/loan/count"

def get_loan_count_by_customer(customer_id: str):
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer <YOUR_API_TOKEN>"  # 필요 시 토큰 인증
    }
    payload = {
        "customer_id": customer_id
    }

    try:
        response = requests.post(API_URL, json=payload, headers=headers)
        response.raise_for_status()
        result = response.json()
        return result.get("loan_count")
    
    except requests.exceptions.HTTPError as e:
        print(f"HTTP 오류 발생: {e}")
    except Exception as e:
        print(f"예상치 못한 오류: {e}")

# 예시 실행
if __name__ == "__main__":
    customer_id = "1234567890"
    count = get_loan_count_by_customer(customer_id)
    print(f"고객번호 {customer_id}의 대출 계약 개수: {count}")





2. Server 측 (운영계 서버) - Flask로 만든 예시 API

from flask import Flask, request, jsonify

app = Flask(__name__)

# 샘플 데이터베이스 (고객번호별 대출 계약 수)
loan_data = {
    "1234567890": 2,
    "9876543210": 5,
    "5555555555": 0
}

@app.route('/loan/count', methods=['POST'])
def get_loan_count():
    data = request.get_json()
    customer_id = data.get('customer_id')

    if not customer_id:
        return jsonify({"error": "customer_id is required"}), 400

    loan_count = loan_data.get(customer_id, 0)
    return jsonify({"customer_id": customer_id, "loan_count": loan_count})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)





3. 인터페이스 포맷 예시

[Request]
Method: POST
URL: /loan/count
Headers:
Content-Type: application/json
Authorization: Bearer <token> (필요 시)
Body:

{
  "customer_id": "1234567890"
}


[Response]


{
  "customer_id": "1234567890",
  "loan_count": 2
}





추가 고려사항
인증: OAuth2, JWT 또는 Mutual TLS 적용 가능
로깅 및 모니터링: API 사용량, 실패 로그 기록
보안: HTTPS 필수, 민감정보 암호화 필요
속도: 계약 건수는 DB index 등으로 빠르게 조회 가능해야

반응형