mgbam commited on
Commit
b4beb5e
·
verified ·
1 Parent(s): 7cd5aef

Update mcp/opentargets.py

Browse files
Files changed (1) hide show
  1. mcp/opentargets.py +18 -18
mcp/opentargets.py CHANGED
@@ -1,22 +1,22 @@
1
- import os, httpx, textwrap
 
2
  from functools import lru_cache
 
3
 
4
- _URL = "https://api.platform.opentargets.org/api/v4/graphql"
5
- _HDR = {"Content-Type": "application/json", "Accept": "application/json"}
6
- if os.getenv("OT_KEY"): # optional higher quota
7
- _HDR["Authorization"] = f"Bearer {os.getenv('OT_KEY')}"
 
 
 
 
 
 
8
 
9
- _QUERY = textwrap.dedent("""
10
- query Assoc($g:String!, $n:Int!) {
11
- associations(geneSymbol:$g, size:$n) {
12
- rows { score datasourceId disease { id name } target { id symbol } }
13
- }
14
- }""")
15
 
16
- @lru_cache(maxsize=512)
17
- async def fetch_ot(sym: str, n: int = 30):
18
- payload = {"query": _QUERY, "variables": {"g": sym, "n": n}}
19
- async with httpx.AsyncClient(timeout=10, headers=_HDR) as c:
20
- r = await c.post(_URL, json=payload)
21
- r.raise_for_status()
22
- return r.json()["data"]["associations"]["rows"]
 
1
+ # mcp/opentargets.py
2
+ import textwrap
3
  from functools import lru_cache
4
+ from mcp.clients import BaseClient
5
 
6
+ class OTClient(BaseClient):
7
+ QUERY = textwrap.dedent("""
8
+ query ($g: String!, $n: Int!){
9
+ associations(geneSymbol: $g, size: $n) {
10
+ rows { score datatypeId datasourceId disease { id name } target { id symbol } }
11
+ }
12
+ }
13
+ """)
14
+ def __init__(self):
15
+ super().__init__("https://api.platform.opentargets.org/api/v4/graphql", api_key_env="OT_KEY")
16
 
17
+ @lru_cache(512)
18
+ async def fetch(self, symbol: str, n: int = 30):
19
+ payload = {"query": self.QUERY, "variables": {"g": symbol, "n": n}}
20
+ return (await self.request("POST", "", json=payload))["data"]["associations"]["rows"]
 
 
21
 
22
+ ot = OTClient()