mirror of
https://github.com/hanzoai/papers.git
synced 2026-10-29 07:34:31 +00:00
94 lines
2.0 KiB
TeX
94 lines
2.0 KiB
TeX
\section{Go SDK}
|
|
|
|
|
|
\subsection{Package Structure}
|
|
|
|
\begin{lstlisting}[caption=Go SDK Structure]
|
|
hanzo-go/
|
|
hanzo.go
|
|
client.go
|
|
product.go
|
|
order.go
|
|
customer.go
|
|
error.go
|
|
pagination.go
|
|
webhook.go
|
|
go.mod
|
|
\end{lstlisting}
|
|
|
|
\subsection{Client Implementation}
|
|
|
|
\begin{lstlisting}[language=Go, caption=Go Client]
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/hanzo.ai/hanzo-go"
|
|
)
|
|
|
|
func main() {
|
|
client := hanzo.NewClient("sk_live_...")
|
|
|
|
// List products
|
|
params := &hanzo.ProductListParams{
|
|
Limit: hanzo.Int64(10),
|
|
}
|
|
iter := client.Products.List(params)
|
|
for iter.Next() {
|
|
product := iter.Product()
|
|
fmt.Println(product.Name)
|
|
}
|
|
if err := iter.Err(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Create a product
|
|
product, err := client.Products.Create(&hanzo.ProductParams{
|
|
Name: hanzo.String("Widget"),
|
|
Price: hanzo.Int64(1999),
|
|
Description: hanzo.String("A useful widget"),
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
\end{lstlisting}
|
|
|
|
\subsection{Pointer Helpers}
|
|
|
|
Go requires pointers for optional fields:
|
|
|
|
\begin{lstlisting}[language=Go, caption=Go Pointer Helpers]
|
|
// Helper functions for optional parameters
|
|
func String(v string) *string { return &v }
|
|
func Int64(v int64) *int64 { return &v }
|
|
func Bool(v bool) *bool { return &v }
|
|
|
|
// Usage
|
|
params := &ProductParams{
|
|
Name: hanzo.String("Widget"),
|
|
Price: hanzo.Int64(1999),
|
|
}
|
|
\end{lstlisting}
|
|
|
|
\subsection{Error Handling}
|
|
|
|
\begin{lstlisting}[language=Go, caption=Go Error Handling]
|
|
product, err := client.Products.Create(params)
|
|
if err != nil {
|
|
if hanzoErr, ok := err.(*hanzo.Error); ok {
|
|
switch hanzoErr.Code {
|
|
case hanzo.ErrorCodeInvalidRequest:
|
|
fmt.Printf("Invalid param: %s\n", hanzoErr.Param)
|
|
case hanzo.ErrorCodeAuthentication:
|
|
fmt.Println("Invalid API key")
|
|
default:
|
|
fmt.Printf("API error: %s\n", hanzoErr.Message)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
\end{lstlisting}
|
|
|