feat(bookstack-api-02m): create example code in examples/ directory
Add three example programs: basic (list books, get page), search (full-text search with CLI args), and export (markdown/PDF export).
This commit is contained in:
parent
e633fc2764
commit
5185222577
5 changed files with 170 additions and 5 deletions
59
examples/basic/main.go
Normal file
59
examples/basic/main.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
// Basic example: set up client, list books, get a page.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
bookstack "code.beautifulmachines.dev/jakoubek/bookstack-api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
client, err := bookstack.NewClient(bookstack.Config{
|
||||
BaseURL: os.Getenv("BOOKSTACK_URL"),
|
||||
TokenID: os.Getenv("BOOKSTACK_TOKEN_ID"),
|
||||
TokenSecret: os.Getenv("BOOKSTACK_TOKEN_SECRET"),
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// List first 5 books
|
||||
books, err := client.Books.List(ctx, &bookstack.ListOptions{Count: 5})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, book := range books {
|
||||
fmt.Printf("Book %d: %s\n", book.ID, book.Name)
|
||||
}
|
||||
|
||||
// Get the first page if any books exist
|
||||
if len(books) > 0 {
|
||||
pages, err := client.Pages.List(ctx, &bookstack.ListOptions{
|
||||
Count: 1,
|
||||
Filter: map[string]string{"book_id": fmt.Sprintf("%d", books[0].ID)},
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if len(pages) > 0 {
|
||||
page, err := client.Pages.Get(ctx, pages[0].ID)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("\nPage: %s\n%s\n", page.Name, page.HTML[:min(200, len(page.HTML))])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
59
examples/export/main.go
Normal file
59
examples/export/main.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
// Export example: export a page to markdown or PDF.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
bookstack "code.beautifulmachines.dev/jakoubek/bookstack-api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 3 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s <page-id> <markdown|pdf>\n", os.Args[0])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
pageID, err := strconv.Atoi(os.Args[1])
|
||||
if err != nil {
|
||||
log.Fatalf("Invalid page ID: %s", os.Args[1])
|
||||
}
|
||||
format := os.Args[2]
|
||||
|
||||
client, err := bookstack.NewClient(bookstack.Config{
|
||||
BaseURL: os.Getenv("BOOKSTACK_URL"),
|
||||
TokenID: os.Getenv("BOOKSTACK_TOKEN_ID"),
|
||||
TokenSecret: os.Getenv("BOOKSTACK_TOKEN_SECRET"),
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
switch format {
|
||||
case "markdown":
|
||||
data, err := client.Pages.ExportMarkdown(ctx, pageID)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Print(string(data))
|
||||
|
||||
case "pdf":
|
||||
data, err := client.Pages.ExportPDF(ctx, pageID)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
filename := fmt.Sprintf("page-%d.pdf", pageID)
|
||||
if err := os.WriteFile(filename, data, 0644); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Saved to %s (%d bytes)\n", filename, len(data))
|
||||
|
||||
default:
|
||||
log.Fatalf("Unknown format: %s (use markdown or pdf)", format)
|
||||
}
|
||||
}
|
||||
47
examples/search/main.go
Normal file
47
examples/search/main.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// Search example: search for content and display results.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
bookstack "code.beautifulmachines.dev/jakoubek/bookstack-api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s <search query>\n", os.Args[0])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
client, err := bookstack.NewClient(bookstack.Config{
|
||||
BaseURL: os.Getenv("BOOKSTACK_URL"),
|
||||
TokenID: os.Getenv("BOOKSTACK_TOKEN_ID"),
|
||||
TokenSecret: os.Getenv("BOOKSTACK_TOKEN_SECRET"),
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
query := os.Args[1]
|
||||
|
||||
results, err := client.Search.Search(ctx, query, &bookstack.ListOptions{Count: 10})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Search results for %q:\n\n", query)
|
||||
for i, r := range results {
|
||||
fmt.Printf("%d. [%s] %s (score: %.1f)\n", i+1, r.Type, r.Name, r.Score)
|
||||
if r.Preview != "" {
|
||||
fmt.Printf(" %s\n", r.Preview)
|
||||
}
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
fmt.Println("No results found.")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue