Skip to content

Commit

Permalink
default endpoint is now retrive from osc-api.json
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Gatto <[email protected]>
  • Loading branch information
outscale-mgo committed Dec 13, 2024
1 parent acb8c18 commit dfb96a8
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 28 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down
26 changes: 26 additions & 0 deletions bin/construct_endpoint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <string.h>


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;
}
29 changes: 12 additions & 17 deletions bin/construct_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions bin/get_path_begin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

e=$1
e=${e:8}

echo $(cut -s -d "/" -f2- <<< "$e") | tr -d '\n'
30 changes: 26 additions & 4 deletions cognac_gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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____"
Expand All @@ -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 ')')
Expand Down Expand Up @@ -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
Expand All @@ -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;"
Expand Down
3 changes: 0 additions & 3 deletions helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit dfb96a8

Please sign in to comment.