Skip to main content

Client Guide

This guide demonstrates how to create a client that sends JSON definitions using HTTP POST requests to the ObjectWeaver API.

Step 1: Define the Client

Start by defining a client that will manage the API connection.

type Client struct {
Password string // APIKey is the authentication token for the API.
BaseURL string // BaseURL is the base endpoint for API requests.
}

Step 2: Initialize a New Client

Create a constructor function to initialize a new client instance with the API key and base URL.

For a new client the endpoint that is available within the docker image is: /api/objectGen.

An example endpoint is: http://localhost:2008

func NewClient(password, url string) *Client {
return &Client{
Password: password,
BaseURL: url, // Replace with your API's base URL
}
}

Step 3: Define the SendRequest Method

Implement a method to send a POST request with a JSON-encoded definition.

func (c *Client) SendHttpRequest(prompt string, definition *Definition) (*http.Response, error) {
url := c.BaseURL

requestBody := RequestBody{
Prompt: prompt,
Definition: definition,
}

jsonData, err := json.Marshal(requestBody)
if err != nil {
return nil, fmt.Errorf("error marshalling definition: %v", err)
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/objectGen", url), bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.Password)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}

return resp, nil
}

Step 4: Example Usage

Demonstrate how to use the client to send a definition.

// Example usage
func ExampleUsage() {
// Initialize a new client with your API key
password := "your-password"
url := "http://localhost:2008"
client := NewClient(password, url)

// Define a sample definition
definition := &Definition{
Type: "Object",
Instruction: "Sample instruction for the definition.",
Properties: map[string]Definition{
"property1": {Type: "String", Instruction: "Description of property1"},
"property2": {Type: "Number", Instruction: "Description of property2"},
},
}

// Send the request
resp, err := client.SendRequest(definition)
if err != nil {
fmt.Printf("Error sending request: %v\n", err)
return
}
defer resp.Body.Close()

// Process response as needed
fmt.Printf("Response Status: %s\n", resp.Status)
// Additional processing of response body, headers, etc.
}
tip

Ensure to adapt the definition structure and example usage to fit your specific API requirements and data structures.