Last active
February 22, 2023 21:33
-
-
Save hxy9243/0378a0601bc329f5c20cc5d8efaf9c6c to your computer and use it in GitHub Desktop.
Example openAPI definition
This file contains hidden or 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
openapi: 3.0.0 | |
info: | |
title: OpenAPI Library Demo App | |
version: 0.0.1 | |
servers: [ | |
{ | |
"url": "http://localhost:8000", | |
"description": "local development server", | |
}, | |
] | |
paths: | |
/books: | |
get: | |
operationId: GET-books | |
parameters: | |
- $ref: "./book.yaml#/components/parameters/isbn" | |
- $ref: "./book.yaml#/components/parameters/title" | |
- $ref: "./book.yaml#/components/parameters/author" | |
- $ref: "./book.yaml#/components/parameters/genre" | |
responses: | |
"200": | |
description: get list of books available | |
content: | |
application/json: | |
schema: | |
type: array | |
items: | |
$ref: "./book.yaml#/components/schemas/book" | |
post: | |
operationId: POST-books | |
requestBody: | |
required: true | |
content: | |
application/json: | |
schema: | |
$ref: "./book.yaml#/components/schemas/book" | |
responses: | |
"200": | |
description: create a new book | |
content: | |
application/json: | |
schema: | |
$ref: "./book.yaml#/components/schemas/book" | |
"400": | |
description: bad request, bad data | |
"409": | |
description: conflict | |
delete: | |
operationId: DELETE-books | |
responses: | |
"204": | |
description: book removed from record | |
/books/{book-id}: | |
get: | |
operationId: GET-book | |
parameters: | |
- in: path | |
name: book-id | |
schema: | |
type: string | |
required: true | |
- in: query | |
name: book-details | |
schema: | |
type: array | |
items: | |
type: string | |
- in: query | |
name: book-author | |
schema: | |
type: string | |
responses: | |
"200": | |
description: get one book based on item id | |
content: | |
application/json: | |
schema: | |
$ref: "./book.yaml#/components/schemas/book" | |
/books/{book-id}/copies: | |
parameters: | |
- in: path | |
name: book-id | |
schema: | |
type: string | |
required: true | |
get: | |
operationId: GET-book-copies | |
responses: | |
"200": | |
description: get all copies of this book in circulation | |
content: | |
application/json: | |
schema: | |
$ref: "./book.yaml#/components/schemas/copy" | |
post: | |
operationId: POST-book-copies | |
requestBody: | |
required: true | |
description: to create a new book copy | |
content: | |
application/json: | |
schema: | |
$ref: "./book.yaml#/components/schemas/copy" | |
responses: | |
"201": | |
description: post a new copy of this book | |
content: | |
application/json: | |
schema: | |
$ref: "./book.yaml#/components/schemas/copy" | |
/books/{book-id}/copies/{copy-id}: | |
parameters: | |
- in: path | |
name: book-id | |
schema: | |
type: string | |
required: true | |
- in: path | |
name: copy-id | |
schema: | |
type: string | |
required: true | |
get: | |
operationId: GET-book-copy | |
responses: | |
"200": | |
description: get one copy of a book | |
content: | |
application/json: | |
schema: | |
$ref: "./book.yaml#/components/schemas/book" | |
patch: | |
operationId: PATCH-book-copy | |
requestBody: | |
content: | |
application/json: | |
schema: | |
$ref: "./book.yaml#/components/schemas/book" | |
responses: | |
"200": | |
description: update information on one book | |
content: | |
application/json: | |
schema: | |
$ref: "./book.yaml#/components/schemas/book" | |
delete: | |
operationId: DELETE-book-copy | |
responses: | |
"204": | |
description: this copy of the book has been removed | |
/users/: | |
get: | |
operationId: GET-users | |
responses: | |
"200": | |
description: get list of users | |
content: | |
application/json: | |
schema: | |
type: array | |
items: | |
$ref: "./user.yaml#/components/schemas/user" | |
post: | |
operationId: POST-users | |
responses: | |
"200": | |
description: get list of users | |
"400": | |
description: bad request, bad data | |
"409": | |
description: conflict | |
/users/{user-id}: | |
parameters: | |
- in: path | |
name: user-id | |
schema: | |
type: string | |
required: true | |
get: | |
operationId: GET-user | |
responses: | |
"200": | |
description: get one user information | |
patch: | |
operationId: PATCH-user | |
responses: | |
"200": | |
description: update user information | |
delete: | |
operationId: DELETE-user | |
responses: | |
"204": | |
description: user has been removed | |
/users/{user-id}/borrows: | |
parameters: | |
- in: path | |
name: user-id | |
schema: | |
type: string | |
required: true | |
get: | |
operationId: GET-user-borrows | |
responses: | |
"200": | |
description: get list of borrows of this user | |
content: | |
application/json: | |
schema: | |
type: array | |
items: | |
$ref: "./borrow.yaml#/components/schemas/borrow" | |
post: | |
operationId: POST-user-borrows | |
responses: | |
"201": | |
description: create a borrow for this user | |
/users/{user-id}/borrows/{borrow-id}: | |
parameters: | |
- in: path | |
name: user-id | |
schema: | |
type: string | |
required: true | |
- in: path | |
name: borrow-id | |
schema: | |
type: string | |
required: true | |
get: | |
operationId: GET-user-borrow | |
responses: | |
"200": | |
description: get all the borrow information of this user | |
content: | |
application/json: | |
schema: | |
$ref: "./borrow.yaml#/components/schemas/borrow" | |
patch: | |
operationId: PATCH-user-borrow | |
responses: | |
"200": | |
description: update the borrow information of a user | |
content: | |
application/json: | |
schema: | |
$ref: "./borrow.yaml#/components/schemas/borrow" |
This file contains hidden or 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
package main | |
import ( | |
"context" | |
"log" | |
client "github.com/hxy9243/openapi-example/client/gen/openapi" | |
) | |
func main() { | |
apicli := client.NewAPIClient(&client.Configuration{ | |
Scheme: "http://", | |
Servers: client.ServerConfigurations{ | |
client.ServerConfiguration{ | |
URL: "localhost:8000", | |
}, | |
}, | |
}) | |
books, _, err := apicli.DefaultApi.GETBooks(context.TODO()).Execute() | |
if err != nil { | |
log.Fatalf("Error: getting books: %s", err) | |
} | |
for i, book := range books { | |
log.Printf("Book %d info: %+v", i, book) | |
} | |
book, resp, err := apicli.DefaultApi.GETBook(context.TODO(), books[0].GetId()). | |
BookDetails([]string{"book", "details", "example"}). | |
BookAuthor("whatever"). | |
Execute() | |
log.Printf("Getting book request: %+v", *resp.Request.URL) | |
if err != nil { | |
log.Fatalf("Error: getting book: %s", err) | |
} | |
log.Printf("Getting book info: %+v", book) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment