Code Monkey home page Code Monkey logo

copenapi's Introduction

copenapi

Overview

copenapi (C open api) is an openapi spec parser library in C. It includes a general purpose command line tool to browse an openapi spec file. copenapi supports openapi spec files in json format.

Try it out

Prerequisites

  • Jansson
  • libcurl

Build & Run

  1. autoreconf -mif && ./configure && make
  2. cmd line client (cli/copenapi_cli) - cli how to
  3. library - api how to

CLI how to

copenapi_cli can work with swagger specs for eg: swagger petstore json

Use copenapi_cli against the petstore swagger spec. It works like a customized cli for your spec.

get the petstore swagger spec (cli does not support external urls for this)

[ ~/pet ]# wget http://petstore.swagger.io/v2/swagger.json

#create the .copenapi file or you can use --apispec to point to swagger.json on disk.
[ ~/pet ]# cat >> .copenapi <<-EOF
[default]
apispec=$PWD/swagger.json
EOF

Try the cli. Note copenapi_cli maps tags as modules.

[ ~/pet ]# copenapi_cli
The following modules are supported.
 user            : Operations about user
 store           : Access to Petstore orders
 pet             : Everything about your Pets
To get help on a module, do <module> --help.
To get help on a module's command, do <module> <command> --help.

Inspect what is provided by a module

[ ~/pet ]# copenapi_cli pet --help
Commands under module pet :
pet             Update an existing pet
pet             Add a new pet to the store
findByStatus    Finds Pets by status
findByTags      Finds Pets by tags
{petId}         Find pet by ID
{petId}         Updates a pet in the store with form data
{petId}         Deletes a pet
uploadImage     uploads an image

To get help on a command, do pet <command> --help.

Get help on a method in a module / tag

[ ~/pet ]# copenapi_cli pet findByStatus --help

Name : /v2/pet/findByStatus

Method: get
Summary : Finds Pets by status
Description : Multiple status values can be provided with comma separated strings
Param1 : status - Required

Invoke the method. Note GET is the default. If you have multiple methods with the same name, ambiguity is resolved in a way that will not surprise you. For eg: try copenapi_cli pet pet You can use -v to get all the details of the communication.

[ ~/pet ]# copenapi_cli pet findByStatus
Parameter status is required. Specify as --status

[ ~/pet ]# copenapi_cli pet findByStatus --status 1
[{"id":9205436248879947591,"category":{"id":0,"name":"打死你"},"name":"doggie","photoUrls":["string"],"tags":[{"id":0,"name":"二哈"}],"status":"1"}]

API how to

To load an api spec from json file and map implementation, follow the sample code below

#include <copenapi/copenapi.h>

REST_MODULE _module1_rest_module[] =
{
   {
       "/v1/module1/version",
       {module1_rest_get_version, NULL, NULL, NULL}
   }
}

//Module registration map
MODULE_REG_MAP stRegMap[] =
{
    {"module1", module1_rest_module},
    {NULL, NULL}
};
PREST_API_DEF pApiDef = NULL;
//load api spec
coapi_load_from_file("/home/user/apispec.json", &pApiDef);
//map implementation
coapi_map_api_impl(pApiDef, stRegMap);

At this point, pApiDef is populated with all the methods from the spec file and their implementations in source. In this example, the entry point /v1/module1/version is mapped to function module1_rest_get_version. You can now hook this up to a REST engine and handle incoming calls with spec driven parameter validation, type validation, error messages and error codes.

Releases & Major Branches

Initial release 0.0.1 alpha

Contributing

The copenapi project team welcomes contributions from the community. If you wish to contribute code and you have not signed our contributor license agreement (CLA), our bot will update the issue when you open a Pull Request. For any questions about the CLA process, please refer to our FAQ. For more detailed information, refer to CONTRIBUTING.md.

License

copenapi is available under the Apache 2 license.

copenapi's People

Contributors

rongrongqiu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.