File size: 2,663 Bytes
769e684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import sys

import requests


def test_law_api():
    oc = os.getenv("LAW_GO_KR_OC")
    url = "http://www.law.go.kr/DRF/lawSearch.do"
    params = {"target": "law", "query": "민원", "type": "XML", "OC": oc}
    try:
        res = requests.get(url, params=params, timeout=10)
        if res.status_code == 200 and "<law" in res.text:
            print(f"[LAW API] βœ… μœ νš¨ν•¨ (μƒνƒœμ½”λ“œ: 200)")
            return True
        else:
            print(f"[LAW API] ❌ 였λ₯˜ (μƒνƒœμ½”λ“œ: {res.status_code})")
            print(f"μ‘λ‹΅λ‚΄μš© 일뢀: {res.text[:200]}")
            return False
    except Exception as e:
        print(f"[LAW API] ❌ μ—°κ²° μ‹€νŒ¨: {e}")
        return False


def test_alio_api():
    key = os.getenv("DATA_GO_KR_API_KEY")
    # Decoding ν‚€λ₯Ό μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— requestsκ°€ ν•œ 번 더 μΈμ½”λ”©ν•˜λ„λ‘ 함
    url = "https://apis.data.go.kr/1051000/public_inst/list"
    params = {"serviceKey": key, "pageNo": 1, "numOfRows": 1, "resultType": "json"}
    try:
        res = requests.get(url, params=params, timeout=10)
        if res.status_code == 200:
            try:
                data = res.json()
                code = data.get("response", {}).get("header", {}).get("resultCode")
                if code == "00":
                    print(f"[ALIO API] βœ… μœ νš¨ν•¨ (μƒνƒœμ½”λ“œ: 200, κ²°κ³Όμ½”λ“œ: 00)")
                    return True
                else:
                    msg = (
                        data.get("response", {})
                        .get("header", {})
                        .get("resultMsg", "μ•Œ 수 μ—†λŠ” 였λ₯˜")
                    )
                    print(f"[ALIO API] ❌ 인증 였λ₯˜ (κ²°κ³Όμ½”λ“œ: {code}, λ©”μ‹œμ§€: {msg})")
                    return False
            except Exception:
                if "<ServiceKey Error" in res.text:
                    print("[ALIO API] ❌ 인증킀 였λ₯˜ (ServiceKey Error)")
                else:
                    print(f"[ALIO API] ❌ 비정상 응닡: {res.text[:200]}")
                return False
        else:
            print(f"[ALIO API] ❌ HTTP 였λ₯˜ (μƒνƒœμ½”λ“œ: {res.status_code})")
            return False
    except Exception as e:
        print(f"[ALIO API] ❌ μ—°κ²° μ‹€νŒ¨: {e}")
        return False


if __name__ == "__main__":
    print("-" * 50)
    print("πŸš€ API ν‚€ μœ νš¨μ„± 검사 μ‹œμž‘")
    law_ok = test_law_api()
    alio_ok = test_alio_api()
    print("-" * 50)
    if law_ok and alio_ok:
        print("✨ λͺ¨λ“  API ν‚€κ°€ μ •μƒμ μœΌλ‘œ μž‘λ™ν•©λ‹ˆλ‹€!")
    else:
        print("⚠️ 일뢀 API 킀에 확인이 ν•„μš”ν•©λ‹ˆλ‹€.")
        sys.exit(1)