-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More cli support #18
Merged
Merged
More cli support #18
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
65e1ce3
add an option to funclist to generate function from paths
outscale-mgo 651460a
remove argument-list.json
outscale-mgo d6bad35
add debug function
outscale-mgo 85afe61
add some debug
outscale-mgo 2416cfe
enable to choose prefix for Function
outscale-mgo 8bcaa35
funclist now support costum function suffix
outscale-mgo 92945b2
Document --function-suffix=suffix
outscale-mgo 9e45172
path is on thw way
outscale-mgo 0927c7a
add more helper functions
outscale-mgo 56e4c39
add more helper to handle path type
outscale-mgo e4ddaa5
descripttion for path mode
outscale-mgo 7377d15
remove debug
outscale-mgo 311f5b4
fix name for path calls
outscale-mgo 21eddf5
somewhat generate arguments corectly with path
outscale-mgo 11ecece
fix header, and handle query maybe
outscale-mgo 65b5cd3
handle path post data mix with other path arguments
outscale-mgo a358f07
bin: add missing get_path_type
outscale-mgo 8a5cb45
add new C helpers documentation
outscale-mgo 855bff1
fix from thiery feedback
outscale-mgo 45fcbfc
default endpoint is now retrive from osc-api.json
outscale-mgo 18c75ac
enable to set user agent
outscale-mgo 3d9786e
support ssl_verify in config.json
outscale-mgo f7e951c
rename sdk-user-agent to sdk-name
outscale-mgo a68744d
improve doc
outscale-mgo b6195ee
work with guru API
outscale-mgo 20bee6c
add documentation
outscale-mgo c51d577
improve readme
outscale-mgo 7f9efba
improve Doc
outscale-mgo d6d23e3
improve configure --help
outscale-mgo 377dc09
improve ./configure --help again
outscale-mgo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,14 +28,20 @@ It takes three arguments: a source file, a destination file, and a language. | |
The language argument is crucial as certain keywords might be interpreted differently depending on the target language. | ||
|
||
The script uses a file called osc-api.json, which represents the OpenAPI specification in JSON format. | ||
For the Outscale API, the YAML source is converted to JSON using yq. | ||
For the Outscale API, the YAML source is converted to JSON using `yq`: (https://kislyuk.github.io/yq/ or https://github.com/mikefarah/yq) | ||
|
||
When generating API calls, COGNAC assumes that the OpenAPI file contains components named CallRequest. | ||
When generating API calls, COGNAC by default assumes that the OpenAPI file contains components named CallRequest. | ||
For example, if the API has a call named `CreatePony`, the corresponding component should be located at `#/components/schemas/CreatePonyRequest`. | ||
|
||
You can modify the suffix of functions by using `--function-suffix=FUNCTION_SUFFIX` option with `./configure` command. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can modify the function suffix by using the |
||
|
||
Alternatively, you can generate functions based on the contents of `paths` by using the `./configure --from-path` option. | ||
|
||
*Note: There are two versions of yq: one written in Python and one in Go. The default version depends on your distribution. On Arch-based distributions, the Python version is typically the default, whereas on Debian-based distributions, the Go version is default. COGNAC supports both, but to use the Go version, you need to pass `--yq-go` to `./configure`.* | ||
|
||
### Example: Generating a CLI for a New API | ||
### Examples: Generating a CLI for a New API | ||
|
||
#### Configure Cognac for an API using elements in the schema as entry points for API calls. | ||
|
||
Let’s say you have an API that is not the Outscale API, and you want to generate a CLI for it. | ||
You have a URL to a YAML file, such as `https://ponyapi.yolo/`, and the API request components are named `XInput` instead of `XRequest`. | ||
|
@@ -44,22 +50,45 @@ To configure the Makefile to generate the CLI with the name `pony-cli`, and adju | |
|
||
Run the following command: | ||
```bash | ||
./configure --cli-name=pony-cli --api-script='curl -s https://ponyapi.yolo | yq $(YQ_ARG) | sed "s/Input/Request/" > osc-api.json' | ||
./configure --cli-name=pony-cli --function-suffix Input --api-script='curl -s https://ponyapi.yolo | yq $(YQ_ARG)" > osc-api.json' | ||
``` | ||
|
||
`-cli-name=pony-cli` set the generated binary name to `pony-cli` | ||
|
||
```bash | ||
--api-script='curl -s https://ponyapi.yolo | yq $(YQ_ARG) | sed "s/Input/Request/" > osc-api.json' | ||
--function-suffix Input | ||
``` | ||
|
||
Search for functions named XInput instead of XRequest. | ||
```bash | ||
--api-script='curl -s https://ponyapi.yolo | yq $(YQ_ARG) > osc-api.json' | ||
``` | ||
|
||
|
||
This script is used to fetch the API file. | ||
|
||
|
||
Here’s what the script does: | ||
|
||
1. Retrieves the API in YAML format using curl -s `https://ponyapi.yolo/.` | ||
2. Converts the YAML to JSON using yq `$(YQ_ARG)`. *Note the usage of `$(YQ_ARG)`, so ./configure can handle go version of yq* | ||
3. Renames all components named `XInput` to `XRequest`. | ||
|
||
|
||
#### Configure Cognac for an API using elements in the path as entry points for API calls. | ||
|
||
For this example we will use [guru](https://apis.guru/api-doc/) | ||
|
||
Run the following command: | ||
``` | ||
./configure --sdk-name=guru-sdk --cli-name=guru --from-path --api-script='curl -s https://api.apis.guru/v2/openapi.yaml | yq $(YQ_ARG)" > osc-api.json' | ||
``` | ||
|
||
`--cli-name=guru`: Sets the generated binary name to `guru`. | ||
`--sdk-name=guru-sdk`: Sets the generated SDK name and the UserAgent to `guru-sdk`. | ||
`--from-path`: Generates API calls from elements in `paths` instead of `components.schemas`. | ||
|
||
|
||
#### Generate the Code | ||
|
||
Once this setup is complete, you can now use the Makefile. It's also a good idea to run ./configure --help, as it contains several useful options. | ||
- `--wget-json-search`: Helps with downloading `json-search`, which can be tricky to install, **If unsure, we recommend using this by default** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include "json.h" | ||
#define UNFOUND "data" | ||
#include "helper.h" | ||
|
||
int main(int ac, char **av) | ||
{ | ||
struct json_object *j_file = json_object_from_file(av[1]); | ||
char *componant_name = av[2]; | ||
char *arg_name = av[3]; | ||
int ret; | ||
struct json_object *path; | ||
struct json_object *post_or_get; | ||
struct json_object *parameters; | ||
|
||
path = get_path_from_file(j_file, componant_name); | ||
post_or_get = get_or_post_from_path(path); | ||
if (!post_or_get) | ||
goto err; | ||
|
||
OBJ_GET(post_or_get, "parameters", ¶meters); | ||
int len = json_object_array_length(parameters); | ||
for (int i = 0; i < len; ++i) { | ||
struct json_object *param = json_object_array_get_idx(parameters, i); | ||
struct json_object *name_obj; | ||
struct json_object *in; | ||
|
||
OBJ_GET(param, "name", &name_obj); | ||
const char *name = json_object_get_string(name_obj); | ||
struct json_object *type; | ||
struct json_object *schema; | ||
|
||
if (strcmp(name, arg_name)) | ||
continue; | ||
OBJ_GET(param, "in", &in); | ||
puts(json_object_get_string(in)); | ||
goto out; | ||
} | ||
out: | ||
err: | ||
json_object_put(j_file); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means that during 'complex_struct_func_parser' and 'cli_parser', the script fails to parse some parts of the osc-api JSON.