Param
#struct ParamOne parameter.
| Name | Type | Description |
|---|---|---|
name | str = "" | The name. |
loc | str = "query" | Where it sits: query, header, cookie or path. |
required | bool = false | It is required. |
description | str = "" | The description. |
schema | str = "{"type":"string"}" | The schema. |
import net.http.swagger
import encoding.json
struct Book:
@json "id"
pub id: int = 0
@json "title"
pub title: str = ""
end
func main:
mut doc := swagger.New("library", "1.2.0")
swagger.Describe(doc, "a sample API for books")
swagger.Server(doc, "https://api.example.com", "the main one")
swagger.Contact(doc, "the Salam team", "https://salamlang.ir", "team@salamlang.ir")
swagger.License(doc, "MIT", "https://opensource.org/licenses/MIT")
swagger.BearerAuth(doc, "token", "JWT")
swagger.ApiKeyAuth(doc, "key", "header", "X-API-Key")
_ := swagger.Route(doc, "GET", "/books/:id")
swagger.Summary(doc, "one book")
swagger.Details(doc, "Returns the book with this id.")
swagger.Tag(doc, "books")
swagger.OperationId(doc, "getBook")
swagger.PathParam(doc, "id", "the id of the book")
swagger.Query(doc, "fields", "the fields asked for", false)
swagger.QueryTyped(doc, "limit", "the greatest number", false, 10)
mut orders := Vector {} as Vector<str>
orders.Push("title")
orders.Push("id")
swagger.QueryEnum(doc, "sort", "the order", false, orders)
swagger.Header(doc, "X-Trace", "the trace id", false)
swagger.Cookie(doc, "sid", "the session", false)
swagger.Secure(doc, "token")
swagger.Returns(doc, 200, "the book", Book {})
swagger.ReturnsHeader(doc, 200, "ETag", "the version")
swagger.ReturnsNothing(doc, 404, "not found")
_ := swagger.Route(doc, "POST", "/books")
swagger.Accepts(doc, "the new book", Book {})
swagger.Returns(doc, 201, "created", Book {})
_ := swagger.Route(doc, "PUT", "/books/:id/cover")
swagger.AcceptsRaw(
doc, "the cover image", "image/png", "{\"type\":\"string\",\"format\":\"binary\"}", true
)
swagger.ReturnsText(doc, 200, "done")
_ := swagger.Route(doc, "POST", "/books/:id/notes")
swagger.AcceptsOptional(doc, "the note", Book {})
swagger.AcceptsForm(doc, "the form", Book {})
swagger.ReturnsRaw(doc, 200, "the export", "text/csv", "{\"type\":\"string\"}")
swagger.ReturnsHtml(doc, 200, "the page")
swagger.ReturnsFile(doc, 200, "the file", "application/pdf")
swagger.Deprecated(doc)
println swagger.At(doc, "get", "/books/:id"), swagger.At(doc, "GET", "/nope")
println doc.ops.Len(),
doc.ops[0].params.Len(),
doc.ops[0].replies.Len(),
doc.ops[3].is_deprecated,
doc.schemes.Len()
text := swagger.Spec(doc)
println json.Valid(text), json.Get(text, "openapi")
println swagger.SpecIndent(doc).Len() > text.Len()
println swagger.Problems(doc).Len(), swagger.Undocumented(doc)
println swagger.WriteSpec(doc, "openapi.json"),
swagger.SpecFromFile("openapi.json").Len() > 0,
swagger.SpecFromFile("missing.json")
println swagger.TEXT_SCHEMA, swagger.JSON_TYPE
swagger.Free(doc)
endtrue false
4 6 2 true 2
true 3.1.0
true
0 []
true true
{"type":"string"} application/json