1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "py/builtin.h"
7 #include "py/mperrno.h"
8 #include "py/obj.h"
9 #include "py/runtime.h"
10 #include "ulog/ulog.h"
11
12 #define LOG_TAG "MOD_UCLOUD_AI"
13
14 extern const mp_obj_type_t chatbot_type;
15
obj_get_token_id(size_t n_args,const mp_obj_t * args)16 static mp_obj_t obj_get_token_id(size_t n_args, const mp_obj_t *args)
17 {
18 char *obj_key = (char *)mp_obj_str_get_str(args[0]);
19 char *obj_secret = (char *)mp_obj_str_get_str(args[1]);
20 char *obj_domain = (char *)mp_obj_str_get_str(args[2]);
21 char *obj_region_id = (char *)mp_obj_str_get_str(args[3]);
22 char buffer[512];
23 char *response = NULL;
24
25 LOGD(LOG_TAG, "obj_key = %s;\n", obj_key);
26 LOGD(LOG_TAG, "obj_secret = %s;\n", obj_secret);
27 LOGD(LOG_TAG, "obj_domain = %s;\n", obj_domain);
28 LOGD(LOG_TAG, "obj_region_id = %s;\n", obj_region_id);
29
30 ucloud_ai_set_key_secret(obj_key, obj_secret);
31 response = ucloud_ai_get_token_id(obj_domain, obj_region_id);
32 if (!response)
33 return mp_const_none;
34
35 if (strlen(response) > 512) {
36 LOGE(LOG_TAG, "buffer is not enough\n");
37 free(response);
38 return mp_const_none;
39 }
40
41 strcpy(buffer, response);
42 free(response);
43 return mp_obj_new_strn(buffer);
44 }
45
46 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(mp_obj_get_token_id, 4, obj_get_token_id);
47
48 // this is the actual C-structure for our new object
49 STATIC const mp_rom_map_elem_t ucloud_ai_locals_dict_table[] = {
50 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ucloud_ai) },
51 { MP_OBJ_NEW_QSTR(MP_QSTR_get_token_id), MP_ROM_PTR(&mp_obj_get_token_id) },
52 { MP_OBJ_NEW_QSTR(MP_QSTR_ChatBot), MP_ROM_PTR(&chatbot_type) },
53 };
54
55 STATIC MP_DEFINE_CONST_DICT(ucloud_ai_locals_dict, ucloud_ai_locals_dict_table);
56
57 const mp_obj_module_t ucloud_ai_module = {
58 .base = { &mp_type_module },
59 .globals = (mp_obj_dict_t *)&ucloud_ai_locals_dict,
60 };
61