78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
from requests import request
|
||
|
||
|
||
class MyBot:
|
||
|
||
def __init__(self, host):
|
||
"""
|
||
:param host: 平台域名
|
||
"""
|
||
self.host = host
|
||
|
||
def get_access_token(self, bot_installer_id, jwt_token):
|
||
"""
|
||
申请授权Token
|
||
:param bot_installer_id: bot安装到某个仓库的安装id bot_install.installer_id=安装用户的id:user_id
|
||
:param jwt_token: jwt_token
|
||
:return: 返回access_token
|
||
"""
|
||
try:
|
||
url = f"{self.host}/app/installations/{bot_installer_id}/access_tokens"
|
||
headers = {
|
||
"Authorization": f"Bearer {jwt_token}"
|
||
}
|
||
response = request(method="POST", url=url, headers=headers)
|
||
access_token = response.json().get("token", "")
|
||
return access_token
|
||
except Exception as e:
|
||
raise e
|
||
|
||
def get_bot_installed_project(self, bot_installer_id, token):
|
||
"""
|
||
获取已安装bot仓库列表
|
||
:param bot_installer_id: bot安装到某个仓库的安装id bot_install.installer_id=安装用户的id:user_id
|
||
:param token:
|
||
:return:
|
||
"""
|
||
try:
|
||
url = f"{self.host}/installations/{bot_installer_id}/repositories.json"
|
||
headers = {
|
||
"Content-Type": "application/json",
|
||
"Authorization": f"Bearer {token}"
|
||
}
|
||
response = request(method="GET", url=url, headers=headers)
|
||
return response
|
||
except Exception as e:
|
||
raise e
|
||
|
||
def get_installed_bot_detail(self, bot_installer_id, token):
|
||
"""
|
||
获取已安装bot详情
|
||
:param bot_installer_id: bot安装到某个仓库的安装id bot_install.installer_id=安装用户的id:user_id
|
||
:param token:
|
||
:return:
|
||
"""
|
||
try:
|
||
url = f"{self.host}/app/installations/{bot_installer_id}.json"
|
||
headers = {
|
||
"Content-Type": "application/json",
|
||
"Authorization": f"Bearer {token}"
|
||
}
|
||
response = request(method="GET", url=url, headers=headers)
|
||
return response
|
||
except Exception as e:
|
||
raise e
|
||
|
||
|
||
if __name__ == '__main__':
|
||
from config.config import host, bot
|
||
from bot.generate_jwt import generate_jwt_token
|
||
|
||
jwt_token = generate_jwt_token(bot)
|
||
mybot = MyBot(host)
|
||
bot_installer_id = 85589
|
||
access_token = mybot.get_access_token(bot_installer_id, jwt_token)
|
||
print(access_token, bot_installer_id)
|
||
res = mybot.get_bot_installed_project(bot_installer_id, access_token)
|
||
print(res.json())
|