pixai_client

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2024 License: MIT Imports: 10 Imported by: 0

README

PixAI Client Go

Installation
go get github.com/pixai-art/pixai-client-go
Usage
package main

import (
	"context"
	"fmt"

	pixai_client "github.com/pixai-art/pixai-client-go"
)

func main() {
	// Initialize the client
	client := pixai_client.NewPixAIClient().
		SetApiKey("replace to your api keys").
		Init()

	ctx := context.Background()

	// Prepare the parameters for the request with JSONObject structure.
	// You can learn the detail about the parameters from our API documentation.
	params := pixai_client.JSONObject{
		"width":   512,
		"height":  512,
		"prompts": "miku",
		"modelId": "1648918127446573124",

		// Specific the priority value as 1000 to make sure the task will be processed immediately.
		// This is a historical legacy issue. If this parameter is not passed, the public queue will be used for queuing. We will set high priority tasks as the default value later.
		"priority": 1000,
	}

	var taskId string

	{
		// Here is an example of how to generate an image and wait for the result
		task, err := client.GenerateImage(ctx, params, func(task *pixai_client.TaskBase) {
			fmt.Printf("Task: %+v\n", task)
		})
		fmt.Printf("Task: %+v\n", task)
		fmt.Printf("Error: %+v\n", err)
		taskId = task.Id
	}

	{
		// Here is an example of how to get the result of a task
		task, err := client.GetTaskById(ctx, taskId)
		fmt.Printf("Task: %+v\n", task)
		fmt.Printf("Error: %+v\n", err)
	}

	{
		// If you don't want to wait for the result, you can use the following code to generate an image and get the task id.
		// Then you can poll the task status by task id or use our webhook to get the result.
		task, err := client.CreateGenerationTask(ctx, params)
		if err != nil {
			fmt.Printf("Error: %+v\n", err)
		}
		fmt.Printf("Task: %+v\n", task)
	}
}

Documentation

Index

Constants

View Source
const (
	ApiBaseUrl       = "https://api.pixai.art"
	WebsocketBaseUrl = "wss://gw.pixai.art"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AllEvents

type AllEvents struct {
	TaskUpdated *TaskBase `json:"taskUpdated"`
}

type ImageVariant added in v0.3.0

type ImageVariant string
const (
	ImageVariant_Public ImageVariant = "PUBLIC"
)

type JSONObject

type JSONObject map[string]any

func (*JSONObject) GetArray added in v0.3.0

func (o *JSONObject) GetArray(key string) []any

func (*JSONObject) GetGraphQLType

func (o *JSONObject) GetGraphQLType() string

func (*JSONObject) GetObject added in v0.3.0

func (o *JSONObject) GetObject(key string) JSONObject

func (*JSONObject) GetString added in v0.3.0

func (o *JSONObject) GetString(key string) string

type MarshalJSON

type MarshalJSON = func(v interface{}) ([]byte, error)

type MediaBase

type MediaBase struct {
	Id     string `json:"id"`
	Type   string `json:"type"`
	Width  *int   `json:"width"`
	Height *int   `json:"height"`
	Urls   []struct {
		Variant string `json:"variant"`
		Url     string `json:"url"`
	} `json:"urls"`
	ImageType *string `json:"imageType"`
}

type MediaProvider added in v0.2.0

type MediaProvider string
const (
	MediaProvider_S3 MediaProvider = "S3"
)

type MediaType added in v0.2.0

type MediaType string
const (
	MediaType_Image MediaType = "IMAGE"
)

type PixAIClient

type PixAIClient struct {
	// contains filtered or unexported fields
}

func NewPixAIClient

func NewPixAIClient() *PixAIClient

func (*PixAIClient) CancelGenerationTask

func (p *PixAIClient) CancelGenerationTask(ctx context.Context, id string) (*TaskBase, error)

func (*PixAIClient) Close

func (p *PixAIClient) Close()

func (*PixAIClient) CreateGenerationTask

func (p *PixAIClient) CreateGenerationTask(ctx context.Context, parameters JSONObject) (*TaskBase, error)

func (*PixAIClient) Do

func (p *PixAIClient) Do(req *http.Request) (*http.Response, error)

func (*PixAIClient) DownloadMedia added in v0.3.0

func (p *PixAIClient) DownloadMedia(ctx context.Context, media *MediaBase) (file []byte, mimeType string, err error)

func (*PixAIClient) GenerateImage

func (p *PixAIClient) GenerateImage(ctx context.Context, parameters JSONObject, onUpdate func(*TaskBase)) (*TaskBase, error)

func (*PixAIClient) GetMediaById added in v0.2.0

func (p *PixAIClient) GetMediaById(ctx context.Context, id string) (*MediaBase, error)

func (*PixAIClient) GetMediaFromTask added in v0.3.0

func (p *PixAIClient) GetMediaFromTask(ctx context.Context, task *TaskBase) ([]*MediaBase, error)

func (*PixAIClient) GetPublicUrl added in v0.3.0

func (p *PixAIClient) GetPublicUrl(media *MediaBase) string

func (*PixAIClient) GetTaskById

func (p *PixAIClient) GetTaskById(ctx context.Context, id string) (*TaskBase, error)

func (*PixAIClient) Init

func (p *PixAIClient) Init() *PixAIClient

func (*PixAIClient) ParseWebhookPayload

func (p *PixAIClient) ParseWebhookPayload(data []byte) (*WebhookEvent, error)

func (*PixAIClient) PersonalEvents

func (p *PixAIClient) PersonalEvents() (*eventSource, error)

func (*PixAIClient) SetApiBaseUrl

func (p *PixAIClient) SetApiBaseUrl(apiBaseUrl string) *PixAIClient

func (*PixAIClient) SetApiKey

func (p *PixAIClient) SetApiKey(apiKey string) *PixAIClient

func (*PixAIClient) SetMarshalJSON

func (p *PixAIClient) SetMarshalJSON(marshal MarshalJSON) *PixAIClient

func (*PixAIClient) SetUnmarshalJSON

func (p *PixAIClient) SetUnmarshalJSON(unmarshal UnmarshalJSON) *PixAIClient

func (*PixAIClient) SetWebSocketBaseUrl

func (p *PixAIClient) SetWebSocketBaseUrl(websocketBaseUrl string) *PixAIClient

func (*PixAIClient) UploadMediaFile added in v0.2.0

func (p *PixAIClient) UploadMediaFile(ctx context.Context, file io.Reader, mimeType string) (*MediaBase, error)

func (*PixAIClient) UploadMediaUrl added in v0.2.0

func (p *PixAIClient) UploadMediaUrl(ctx context.Context, url string) (*MediaBase, error)

type TaskBase

type TaskBase struct {
	Id         string         `json:"id"`
	UserId     string         `json:"userId"`
	Parameters map[string]any `json:"parameters" scalar:"true"`
	Outputs    map[string]any `json:"outputs" scalar:"true"`
	Status     *string        `json:"status"`
	StartedAt  *time.Time     `json:"startedAt"`
	EndAt      *time.Time     `json:"endAt"`
	CreatedAt  time.Time      `json:"createdAt"`
	UpdatedAt  time.Time      `json:"updatedAt"`
	Media      *MediaBase     `json:"media"`
}

type TaskStatus

type TaskStatus string
const (
	TaskStatus_Waiting   TaskStatus = "waiting"
	TaskStatus_Running   TaskStatus = "running"
	TaskStatus_Completed TaskStatus = "completed"
	TaskStatus_Failed    TaskStatus = "failed"
	TaskStatus_Cancelled TaskStatus = "cancelled"
)

type UnmarshalJSON

type UnmarshalJSON = func(data []byte, v interface{}) error

type UploadMediaInput added in v0.2.0

type UploadMediaInput struct {
	Type       MediaType     `json:"type"`
	Provider   MediaProvider `json:"provider"`
	ExternalId *string       `json:"externalId,omitempty"`
	ImageType  *string       `json:"imageType,omitempty"`
}

type UploadMediaResult added in v0.2.0

type UploadMediaResult struct {
	UploadUrl  *string    `json:"uploadUrl,omitempty"`
	ExternalId *string    `json:"externalId,omitempty"`
	MediaId    *string    `json:"mediaId,omitempty"`
	Media      *MediaBase `json:"media,omitempty"`
}

type WebhookEvent

type WebhookEvent struct {
	Id         string
	UserId     string
	WebhookId  string
	WebhookUrl string
	Type       WebhookEventType
	RetryCount int
	Data       any
	CreatedAt  *time.Time

	Task *TaskBase
}

type WebhookEventType

type WebhookEventType string
const (
	WebhookEventType_TaskUpdated   WebhookEventType = "task_updated"
	WebhookEventType_TaskRunning   WebhookEventType = "task_running"
	WebhookEventType_TaskCanceled  WebhookEventType = "task_canceled"
	WebhookEventType_TaskCompleted WebhookEventType = "task_completed"
	WebhookEventType_TaskFailed    WebhookEventType = "task_failed"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL