Snips
A code generator for RESTful APIs.
Install / Use
/learn @qingstor/SnipsREADME
Snips
A code generator for RESTful APIs.
Introduction
Snips generates various code using API specifications in OpenAPI Specification (Swagger) v2.0 format.
Run snips --help to get to help messages of snips.
$ snips --help
A code generator for RESTful APIs.
For example:
$ snips -f ./specs/qingstor/api.json
-t ./templates/qingstor/go \
-o ./publish/qingstor-sdk-go/service
$ ...
$ snips --file=./specs/qingstor/api.json \
--template=./templates/qingstor/ruby \
--output=./publish/qingstor-sdk-ruby/lib/qingstor/sdk/service
$ ...
Copyright (C) 2016 Yunify, Inc.
Usage:
snips [flags]
Flags:
-f, --file string Specify the spec file.
--format string Specify the format of spec file. (default "OpenAPI-v2.0")
-o, --output string Specify the output directory.
-t, --template string Specify template files directory.
-v, --version Show version.
Installation
Snips is a command line tool, and it's easy to have it installed. You can build it from source code or download the precompiled binary directly.
Install from Source Code
Snips requires Go 1.6 or later's vendor feature, the dependencies the project
used are included in the vendor directory. And we use glide
to manage project dependence.
$ git clone git@github.com:yunify/snips.git
$ glide install
$ make install
Notice: You can also use Go 1.5 with the GO15VENDOREXPERIMENT=1.
Download Precompiled Binary
- Go to releases tab and download the binary for your operating
system, for example
snips-v0.0.7-darwin_amd64.tar.gz. - Unarchive the downloaded file, and put the executable file
snipsinto a directory that in the$PATHenvironment variable, for example/usr/local/bin. - Run
snips --helpto get started.
SDK Development Workflow
Snips takes API specifications and template to generate lots of code for APIs, then these generated code plus the handcrafted code makes up the SDK. Next, we use scenario based test to make sure our SDKs are working properly, and ensure their functional consistency.
+---------------------------------------------+--------------------+
| | Workflow Diagram |
| API Specifications +--------------------|
| + |
| | Scenario |
| Templates | +-------------> Based |
| + | +------>SDKs Testing |
| | | | ^ + |
| | v | | | |
| +-----> Snips+---+ | | |
| | | |
| | v |
| Handcraft+------------------+ Publish |
| |
+------------------------------------------------------------------+
Add an SDK for Another Programing Language
- Create handcraft files of SDK, to handle configuration, network request and etc.
- Writing templates for API code.
- Generate code using snips.
- Running tests.
- Publish.
Update an Exists SDK
- Change handcraft files (if needed) and update the API specs.
- Regenerate code.
- Running tests.
- Publish.
Example
Let's take Go SDK for QingStor (qingstor-sdk-go) for
example.
Prepare
./specs/qingstor: Refer to the QingStor API specifications./test/features: Refer to the QingStor SDK test scenarios
Tips: Include these files as git submodule.
Procedures
-
Create template files which will be used to generate API code in
./template. -
Generate code using snips, and format the generated code.
$ snips --version snips version 0.3.6 $ snips -f ./specs/qingstor/2016-01-06/swagger/api_v2.0.json \ -t ./template \ -o ./service Loaded templates from ./template 4 template(s) detected. Loaded specification file ./specs/qingstor/2016-01-06/swagger/api_v2.0.json (Swagger-v2.0) Generating to: ./service/qingstor.go Generating to: ./service/bucket.go Generating to: ./service/object.go Generating to: ./service/types.go Everything looks fine. $ gofmt -w . -
Implement test scenarios in
./test.$ ls ./test bucket.go config.yaml.example test_config.yaml bucket_acl.go features test_config.yaml.example bucket_cors.go main.go utils.go bucket_external_mirror.go object.go vendor bucket_policy.go object_multipart.go config.yaml service.go -
Running scenarios test, and pass all tests.
$ pushd ./test $ go run *.go ... 38 scenarios (38 passed) 84 steps (84 passed) 1m2.408357076s $ popd -
Every time the QingStor API changes, just update the
specs/qingstorand./test/featuressubmodule and regenerate code. Then add/change test for this API change, and rerun the online test to make sure the SDK is working properly.
References
Contributing
Please see Contributing Guidelines of this project
before submitting patches.
LICENSE
The Apache License (Version 2.0, January 2004).
Related Skills
node-connect
343.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
90.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
