From dfb96a86a9d77ee17dc920392524ec9cc7053f8a Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Fri, 13 Dec 2024 14:26:24 +0100 Subject: [PATCH] default endpoint is now retrive from osc-api.json Signed-off-by: Matthias Gatto --- Makefile | 5 ++++- bin/construct_endpoint.c | 26 ++++++++++++++++++++++++++ bin/construct_path.c | 29 ++++++++++++----------------- bin/get_path_begin.sh | 6 ++++++ cognac_gen.sh | 30 ++++++++++++++++++++++++++---- helper.sh | 3 --- lib.c | 4 +--- 7 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 bin/construct_endpoint.c create mode 100755 bin/get_path_begin.sh diff --git a/Makefile b/Makefile index 0299ede..e980ead 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ help: include oapi-cli.mk -BIN_DEPENDANCIES=bin/path_to_snakecase bin/path_to_camelcase bin/line_check bin/get_argument_list bin/funclist bin/get_path_type bin/get_path_description bin/arg_placement bin/construct_path +BIN_DEPENDANCIES=bin/path_to_snakecase bin/path_to_camelcase bin/line_check bin/get_argument_list bin/funclist bin/get_path_type bin/get_path_description bin/arg_placement bin/construct_path bin/construct_endpoint osc-api.json:: ./bin/osc-api-seems-valid.sh osc-api.json "need_remove" @@ -38,6 +38,9 @@ bin/funclist: bin/funclist.c bin/construct_path: bin/construct_path.c $(CC) -O3 bin/construct_path.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/construct_path +bin/construct_endpoint: bin/construct_endpoint.c + $(CC) -O3 bin/construct_endpoint.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/construct_endpoint + bin/path_to_snakecase: bin/path_to_snakecase.c $(CC) -O3 bin/path_to_snakecase.c $(JSON_C_LDFLAGS) $(JSON_C_CFLAGS) -o bin/path_to_snakecase diff --git a/bin/construct_endpoint.c b/bin/construct_endpoint.c new file mode 100644 index 0000000..a896b22 --- /dev/null +++ b/bin/construct_endpoint.c @@ -0,0 +1,26 @@ +#include +#include + + +int main(int ac, char **av) +{ + int buf_i; + char *componant_name = av[1]; + +again: + char *brk = strchr(componant_name, '{'); + if (!brk) { + printf("\tosc_str_append_string(&e->endpoint, \"%s\");\n", componant_name); + return 0; + } + + *brk = 0; + printf("\tosc_str_append_string(&e->endpoint, \"%s\");\n", componant_name); + + printf("\tosc_str_append_string(&e->endpoint, e->"); + for (++brk; *brk != '}'; ++brk) + putchar(*brk); + puts(");"); + componant_name = brk + 1; + goto again; +} diff --git a/bin/construct_path.c b/bin/construct_path.c index b3192ac..5068f8c 100644 --- a/bin/construct_path.c +++ b/bin/construct_path.c @@ -3,37 +3,32 @@ int main(int ac, char **av) { - char buf[1024]; - int buf_i; char *componant_name = av[1]; + char *path_begin = ac > 2 ? av[2] : ""; if (*componant_name == '/') ++componant_name; + char *brk = strchr(componant_name, '{'); if (!brk) { - printf("\tosc_str_append_string(&end_call, \"/api/v1/%s\");\n", componant_name); + printf("\tosc_str_append_string(&end_call, \"%s/%s\");\n", path_begin, componant_name); return 0; } *brk = 0; - printf("\tosc_str_append_string(&end_call, \"/api/v1/%s\");\n", componant_name); + printf("\tosc_str_append_string(&end_call, \"%s/%s\");\n", path_begin, componant_name); again: printf("\tosc_str_append_string(&end_call, args->"); for (++brk; *brk != '}'; ++brk) putchar(*brk); puts(");"); - buf_i = 0; - for (++brk; *brk; ++brk) { - if (*brk == '{') { - buf[buf_i] = 0; - printf("\tosc_str_append_string(&end_call, \"%s\");\n", buf); - goto again; - } - buf[buf_i++] = *brk; - } - if (buf_i) { - buf[buf_i] = 0; - printf("\tosc_str_append_string(&end_call, \"%s\");\n", buf); - } + componant_name = brk + 1; + brk = strchr(componant_name, '{'); + if (brk) + *brk = 0; + if (*componant_name) + printf("\tosc_str_append_string(&end_call, \"%s\");\n", componant_name); + if (brk) + goto again; } diff --git a/bin/get_path_begin.sh b/bin/get_path_begin.sh new file mode 100755 index 0000000..6436893 --- /dev/null +++ b/bin/get_path_begin.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +e=$1 +e=${e:8} + +echo $(cut -s -d "/" -f2- <<< "$e") | tr -d '\n' diff --git a/cognac_gen.sh b/cognac_gen.sh index 29c0f05..8668532 100755 --- a/cognac_gen.sh +++ b/cognac_gen.sh @@ -14,6 +14,26 @@ source ./helper.sh debug "debug mode is on" debug "Get functions call from osc-api.json path: $FROM_PATH" +default_endpoint=$(cat osc-api-api.json | jq -r .servers[0].url) +if [[ "$default_endpoint" == "null" ]]; then + default_endpoint='https://api.{region}.outscale.com/api/v1' +fi +#default_endpoint='https://api.{region}.outscale.com' + +debug $default_endpoint + +path_begin=$(bin/get_path_begin.sh "$default_endpoint") +path_begin_l=${#path_begin} +if [[ $path_begin_l != 0 ]]; then + path_begin="/$path_begin" + path_begin_l=$(($path_begin_l + 1)) + default_endpoint=${default_endpoint:0:-$path_begin_l} +fi + +debug $path_begin +debug $default_endpoint + + dash_this_arg() { local cur="$1" @@ -209,7 +229,7 @@ replace_args() SDK_VERSION=$(cat sdk-version) while IFS= read -r line do - arg_check=$(bin/line_check ____args____ ____func_code____ ____functions_proto____ ____cli_parser____ ____complex_struct_func_parser____ ____complex_struct_to_string_func____ ____call_list_dec____ ____call_list_descriptions____ ____call_list_args_descriptions____ <<< "$line") + arg_check=$(bin/line_check ____args____ ____func_code____ ____functions_proto____ ____cli_parser____ ____complex_struct_func_parser____ ____complex_struct_to_string_func____ ____call_list_dec____ ____call_list_descriptions____ ____call_list_args_descriptions____ ____make_default_endpoint____ <<< "$line") if [ "$arg_check" == "____args____" ]; then debug "____args____" @@ -232,6 +252,10 @@ replace_args() echo -en $D2 done echo -ne $D3 + elif [ "$arg_check" == "____make_default_endpoint____" ]; then + debug "____make_default_endpoint____" + debug "$default_endpoint" + bin/construct_endpoint "$default_endpoint" elif [ "$arg_check" == "____call_list_args_descriptions____" ]; then debug "____call_list_args_descriptions____" DELIMES=$(cut -d '(' -f 2 <<< $line | tr -d ')') @@ -494,7 +518,6 @@ EOF local snake_x=$(bin/path_to_snakecase $x) #local caml_l=$(bin/path_to_camelcase "$x") local args=$(bin/get_argument_list osc-api.json ${x}${FUNCTION_SUFFIX}) - debug "wil unknow ?" local have_post=$(cat osc-api.json | json-search ${x}${FUNCTION_SUFFIX} | jq .post) dashed_args="" local have_quey=0 @@ -506,13 +529,12 @@ EOF dash_this "$x" "$arg" done - debug "--- here 11 ----" while IFS= read -r fline do if [[ $( grep -q ____construct_data____ <<< "$fline" )$? == 0 ]]; then ./construct_data.${lang}.sh $x elif [[ $( grep -q ____construct_path____ <<< "$fline" )$? == 0 ]]; then - bin/construct_path $x + bin/construct_path $x "$path_begin" elif [[ $( grep -q ____maybe_query_init____ <<< "$fline" )$? == 0 ]]; then if [[ "$have_quey" == "1" ]]; then echo -e "\tstruct osc_str query;" diff --git a/helper.sh b/helper.sh index 0942b54..57dd672 100644 --- a/helper.sh +++ b/helper.sh @@ -102,7 +102,6 @@ get_type3() { fi arg_info=$(jq .properties[\"$arg\"] <<< $st_info) get_type_direct "$arg_info" - debug "type 3 ($arg): " $(get_type_direct "$arg_info") return 0 } @@ -115,7 +114,6 @@ get_type2() { local path_type=$(bin/get_path_type ./osc-api.json $struct $arg) if [[ "$path_type" != "$struct" ]]; then if [[ "$path_type" != 'string' && 'path_type' != 'null' ]]; then - debug "get_type2: $path_type" struct=$path_type else echo $path_type @@ -202,7 +200,6 @@ get_type() { local path_type=$(bin/get_path_type ./osc-api.json $func $x) if [[ "$path_type" != "$func" ]]; then if [[ "$path_type" != 'string' && 'path_type' != 'null' ]]; then - debug "get_type: $path_type $func $x" func=$path_type else echo $path_type diff --git a/lib.c b/lib.c index 8809aa9..50d347a 100644 --- a/lib.c +++ b/lib.c @@ -591,9 +591,7 @@ int osc_init_sdk_ext(struct osc_env *e, const char *profile, unsigned int flag, e->region = "eu-west-2"; if (!endpoint && !e->endpoint_allocated_) { - osc_str_append_string(&e->endpoint, "https://api."); - osc_str_append_string(&e->endpoint, e->region); - osc_str_append_string(&e->endpoint, ".outscale.com"); +____make_default_endpoint____ } else { if (e->endpoint_allocated_) { osc_str_append_string(&e->endpoint,