From 25ab60e5a2b2b3e27aae1c75c87c027a5772f2df Mon Sep 17 00:00:00 2001 From: louistiti Date: Wed, 24 May 2023 19:53:34 +0800 Subject: [PATCH] feat: add Leon user agent from bridges --- bridges/nodejs/src/constants.ts | 1 + bridges/nodejs/src/sdk/network.ts | 10 +++++++++- bridges/python/src/constants.py | 6 ++++++ bridges/python/src/sdk/network.py | 15 +++++++++++++-- skills/utilities/timekeeper/src/actions/run.py | 2 ++ 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/bridges/nodejs/src/constants.ts b/bridges/nodejs/src/constants.ts index 617549380..5151a427d 100644 --- a/bridges/nodejs/src/constants.ts +++ b/bridges/nodejs/src/constants.ts @@ -32,3 +32,4 @@ export const SKILL_CONFIG: SkillConfigSchema = JSON.parse( export const SKILL_SRC_CONFIG: Record = JSON.parse( fs.readFileSync(path.join(SKILL_PATH, 'src', 'config.json'), 'utf8') ).configurations +export { LEON_VERSION, NODEJS_BRIDGE_VERSION } from '@/constants' diff --git a/bridges/nodejs/src/sdk/network.ts b/bridges/nodejs/src/sdk/network.ts index 855ddc831..627801eaf 100644 --- a/bridges/nodejs/src/sdk/network.ts +++ b/bridges/nodejs/src/sdk/network.ts @@ -3,6 +3,8 @@ import dns from 'node:dns' import type { AxiosInstance } from 'axios' import axios from 'axios' +import { LEON_VERSION, NODEJS_BRIDGE_VERSION } from '@bridge/constants' + interface NetworkOptions { /** `baseURL` will be prepended to `url`. It can be convenient to set `baseURL` for an instance of `Network` to pass relative URLs. */ baseURL?: string @@ -67,8 +69,12 @@ export class Network { url: options.url, method: options.method.toLowerCase(), data: options.data, - headers: options.headers + headers: { + 'User-Agent': `Leon Personal Assistant ${LEON_VERSION} - Node.js Bridge ${NODEJS_BRIDGE_VERSION}`, + ...options.headers + } }) + return { data: response.data, statusCode: response.status, @@ -80,10 +86,12 @@ export class Network { } catch (error) { let statusCode = 500 let data = {} as ResponseErrorData + if (axios.isAxiosError(error)) { data = error?.response?.data statusCode = error?.response?.status ?? 500 } + throw new NetworkError({ data, statusCode, diff --git a/bridges/python/src/constants.py b/bridges/python/src/constants.py index e8e431b26..94d802152 100644 --- a/bridges/python/src/constants.py +++ b/bridges/python/src/constants.py @@ -2,6 +2,8 @@ import json import os +import version + INTENT_OBJ_FILE_PATH = sys.argv[1] with open(INTENT_OBJ_FILE_PATH, 'r') as f: @@ -25,3 +27,7 @@ with open(os.path.join(SKILL_PATH, 'src', 'config.json'), 'r') as f: SKILL_SRC_CONFIG = json.load(f)['configurations'] + +LEON_VERSION = os.getenv('npm_package_version') + +PYTHON_BRIDGE_VERSION = version.__version__ diff --git a/bridges/python/src/sdk/network.py b/bridges/python/src/sdk/network.py index 31ef9e86b..735feda10 100644 --- a/bridges/python/src/sdk/network.py +++ b/bridges/python/src/sdk/network.py @@ -2,6 +2,8 @@ import socket from typing import Any, Dict, TypedDict, Union, Literal +from ..constants import LEON_VERSION, PYTHON_BRIDGE_VERSION + class NetworkOptions(TypedDict, total=False): base_url: str @@ -33,13 +35,21 @@ def __init__(self, options: NetworkOptions = {}) -> None: def request(self, options: NetworkRequestOptions) -> NetworkResponse: try: url = options['url'] + if self.options['base_url']: url = self.options['base_url'] + url method = options['method'] data = options.get('data', {}) headers = options.get('headers', {}) - - response = requests.request(method, url, json=data, headers=headers) + response = requests.request( + method, + url, + json=data, + headers={ + 'User-Agent': f"Leon Personal Assistant {LEON_VERSION} - Python Bridge {PYTHON_BRIDGE_VERSION}", + **headers + } + ) network_response: NetworkResponse = { 'data': response.json(), @@ -70,6 +80,7 @@ def is_network_error(self, error: Exception) -> bool: def is_network_available(self) -> bool: try: socket.gethostbyname('getleon.ai') + return True except socket.error: return False diff --git a/skills/utilities/timekeeper/src/actions/run.py b/skills/utilities/timekeeper/src/actions/run.py index 0e786af48..cb7b591d2 100644 --- a/skills/utilities/timekeeper/src/actions/run.py +++ b/skills/utilities/timekeeper/src/actions/run.py @@ -12,9 +12,11 @@ def run(params: ActionParams) -> None: # TODO # network request # install bs4 and grab it from skill + network = Network({ 'base_url': 'https://jsonplaceholder.typicode.com' }) + try: response = network.request({ 'url': '/todos/1',