Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
feat(camera): add camera (#33)
Browse files Browse the repository at this point in the history
add camera
  • Loading branch information
XdpCs authored Nov 25, 2023
1 parent 06ff4bf commit 089f91d
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 15 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ require github.com/XdpCs/wujiesdk latest
- [x] 获取视频生视频模型列表及价格表
- [x] 视频生视频模型排队情况查询
- [x] 视频生成结果查询
- [ ] 个性相机
- [ ] 作画模版选项
- [ ] 作画
- [ ] 相机作画轮询接口
- [ ] 相机作画查询接口
- [x] 个性相机
- [x] 作画模版选项
- [x] 作画
- [x] 相机作画轮询接口
- [x] 相机作画查询接口
- [ ] Ai专业版作画
- [x] 专业版发起AI作画
- [x] 专业版作画轮询接口
Expand Down
86 changes: 85 additions & 1 deletion caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package wujiesdk
// @Title caller.go
// @Description handle wujie sdk's response
// @Create XdpCs 2023-09-10 20:47
// @Update XdpCs 2023-11-08 09:30
// @Update XdpCs 2023-11-25 21:13

import (
"context"
Expand Down Expand Up @@ -818,6 +818,90 @@ func (c *Caller) VideoGeneratingInfo(ctx context.Context, keys []string) (WujieC
return code, &vResp.Data, nil
}

// CameraTemplateOptions get camera template options
func (c *Caller) CameraTemplateOptions(ctx context.Context) (WujieCode, []CameraTemplateOption, error) {
resp, err := c.Client.CameraTemplateOptions(ctx)
if err != nil {
return ErrorWujieCode, nil, fmt.Errorf("c.Client.CameraTemplateOptions: %w", err)
}
defer func() { _ = resp.Body.Close() }()

var cResp CameraTemplateOptionsResponse
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
return ErrorWujieCode, nil, fmt.Errorf("json.NewDecoder: %w", err)
}

code := WujieCode(cResp.Code)
if err := code.Err(); err != nil {
return code, nil, fmt.Errorf("TRACE_ID: %s, WujieCode: %w, Message: %s",
getTraceID(resp), err, cResp.Message)
}
return code, cResp.Data, nil
}

// CreateCamera create camera
func (c *Caller) CreateCamera(ctx context.Context, cReq *CreateCameraRequest) (WujieCode, *CreateCameraResult, error) {
resp, err := c.Client.CreateCamera(ctx, cReq)
if err != nil {
return ErrorWujieCode, nil, fmt.Errorf("c.Client.CreateCamera: %w", err)
}
defer func() { _ = resp.Body.Close() }()

var cResp CreateCameraResponse
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
return ErrorWujieCode, nil, fmt.Errorf("json.NewDecoder: %w", err)
}

code := WujieCode(cResp.Code)
if err := code.Err(); err != nil {
return code, nil, fmt.Errorf("TRACE_ID: %s, WujieCode: %w, Message: %s, CreateCameraRequest: %s",
getTraceID(resp), err, cResp.Message, cReq.String())
}
return code, &cResp.Data, nil
}

// CameraGeneratingInfo get camera generating info
func (c *Caller) CameraGeneratingInfo(ctx context.Context, keys []string) (WujieCode, []CameraGeneratingInfo, error) {
resp, err := c.Client.CameraGeneratingInfo(ctx, keys)
if err != nil {
return ErrorWujieCode, nil, fmt.Errorf("c.Client.CameraGeneratingInfo: %w", err)
}
defer func() { _ = resp.Body.Close() }()

var cResp CameraGeneratingInfoResponse
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
return ErrorWujieCode, nil, fmt.Errorf("json.NewDecoder: %w", err)
}

code := WujieCode(cResp.Code)
if err := code.Err(); err != nil {
return code, nil, fmt.Errorf("TRACE_ID: %s, WujieCode: %w, Message: %s",
getTraceID(resp), err, cResp.Message)
}
return code, cResp.Data.Infos, nil
}

// CameraInfo get camera info
func (c *Caller) CameraInfo(ctx context.Context, key string) (WujieCode, *CameraInfo, error) {
resp, err := c.Client.CameraInfo(ctx, key)
if err != nil {
return ErrorWujieCode, nil, fmt.Errorf("c.Client.CameraInfo: %w", err)
}
defer func() { _ = resp.Body.Close() }()

var cResp CameraInfoResponse
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
return ErrorWujieCode, nil, fmt.Errorf("json.NewDecoder: %w", err)
}

code := WujieCode(cResp.Code)
if err := code.Err(); err != nil {
return code, nil, fmt.Errorf("TRACE_ID: %s, WujieCode: %w, Message: %s, key: %s",
getTraceID(resp), err, cResp.Message, key)
}
return code, &cResp.Data, nil
}

func getTraceID(resp *http.Response) string {
return resp.Header.Get(TraceID)
}
57 changes: 56 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package wujiesdk
// @Title client.go
// @Description request wujie's api
// @Create XdpCs 2023-09-10 20:47
// @Update XdpCs 2023-11-08 09:30
// @Update XdpCs 2023-11-25 21:13

import (
"bytes"
Expand Down Expand Up @@ -833,3 +833,58 @@ func (c *Client) VideoGeneratingInfo(ctx context.Context, keys []string) (*http.
}
return resp, nil
}

// CameraTemplateOptions get camera template options
func (c *Client) CameraTemplateOptions(ctx context.Context) (*http.Response, error) {
path, err := url.Parse(Domain + string(CameraTemplateOptionsWujieRouter))
if err != nil {
return nil, fmt.Errorf("url.Parse: url: %v, parse url error: %w", Domain+string(CameraTemplateOptionsWujieRouter), err)
}
resp, err := c.CtxGetJson(ctx, path.String(), nil)
if err != nil {
return nil, fmt.Errorf("c.CtxGetJson: error: %w", err)
}
return resp, nil
}

// CreateCamera create camera
func (c *Client) CreateCamera(ctx context.Context, cReq *CreateCameraRequest) (*http.Response, error) {
path, err := url.Parse(Domain + string(CreateCameraWujieRouter))
if err != nil {
return nil, fmt.Errorf("url.Parse: url: %v, parse url error: %w", Domain+string(CreateCameraWujieRouter), err)
}
resp, err := c.CtxPostJson(ctx, path.String(), nil, cReq)
if err != nil {
return nil, fmt.Errorf("c.CtxPostJson: req: %v, error: %w", cReq.String(), err)
}
return resp, nil
}

// CameraGeneratingInfo get camera generating info
func (c *Client) CameraGeneratingInfo(ctx context.Context, keys []string) (*http.Response, error) {
path, err := url.Parse(Domain + string(CameraGeneratingInfoWujieRouter))
if err != nil {
return nil, fmt.Errorf("url.Parse: url: %v, parse url error: %w", Domain+string(CameraGeneratingInfoWujieRouter), err)
}
resp, err := c.CtxPostJson(ctx, path.String(), nil, keys)
if err != nil {
return nil, fmt.Errorf("c.CtxPostJson: req: %v, error: %w", keys, err)
}
return resp, nil
}

// CameraInfo get camera info
func (c *Client) CameraInfo(ctx context.Context, key string) (*http.Response, error) {
values := url.Values{
"key": []string{key},
}
path, err := url.Parse(Domain + string(CameraInfoWujieRouter))
if err != nil {
return nil, fmt.Errorf("url.Parse: url: %v, parse url error: %w", Domain+string(CameraInfoWujieRouter), err)
}
resp, err := c.CtxGetJson(ctx, path.String(), values)
if err != nil {
return nil, fmt.Errorf("c.CtxGetJson: req: %v, error: %w", key, err)
}
return resp, nil
}
10 changes: 9 additions & 1 deletion const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package wujiesdk
// @Title const.go
// @Description wujie sdk's const
// @Create XdpCs 2023-09-10 20:47
// @Update XdpCs 2023-11-08 09:30
// @Update XdpCs 2023-11-25 21:13

import (
"fmt"
Expand Down Expand Up @@ -81,6 +81,14 @@ const (
VideoGeneratingInfoWujieRouter WujieRouter = "/ai/video/generating_info"
)

// create avatar camera WujieRouter
const (
CameraTemplateOptionsWujieRouter WujieRouter = "/avatar/camera/template_options"
CreateCameraWujieRouter WujieRouter = "/avatar/camera/create"
CameraGeneratingInfoWujieRouter WujieRouter = "/avatar/camera/generating_info"
CameraInfoWujieRouter WujieRouter = "/avatar/camera/info"
)

const DefaultExpiration = 4 * time.Minute
const Domain string = "https://gate.wujiebantu.com/wj-open/v1"

Expand Down
4 changes: 2 additions & 2 deletions credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package wujiesdk
// @Title credentials.go
// @Description sign request
// @Create XdpCs 2023-09-10 20:47
// @Update XdpCs 2023-10-17 10:21
// @Update XdpCs 2023-11-25 21:13

import (
"crypto"
Expand All @@ -28,7 +28,7 @@ type Credentials struct {
RsaPrivateKey *rsa.PrivateKey
}

// Sign sign the request
// Sign the request
func (c *Credentials) Sign(req *http.Request) (*http.Request, error) {
auth, found := c.cache.Get(HTTPHeaderAuthorization)
if !found {
Expand Down
118 changes: 113 additions & 5 deletions entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package wujiesdk
// @Title entity.go
// @Description entity
// @Create XdpCs 2023-09-10 20:47
// @Update XdpCs 2023-11-08 09:30
// @Update XdpCs 2023-11-25 21:13

import (
"fmt"
Expand Down Expand Up @@ -196,6 +196,10 @@ type CreateImageRequest struct {
ProMethod string `json:"pro_method,omitempty"`
}

func (c *CreateImageRequest) String() string {
return fmt.Sprintf("%+v", *c)
}

type MultiDiffusion struct {
TiledDiffusion struct {
Enabled bool `json:"enabled"`
Expand Down Expand Up @@ -243,6 +247,10 @@ type MultiDiffusion struct {
} `json:"tiled_vae"`
}

func (m *MultiDiffusion) String() string {
return fmt.Sprintf("%+v", *m)
}

type ServiceContext struct {
Source string `json:"source"`
From string `json:"from"`
Expand All @@ -253,15 +261,15 @@ type ServiceContext struct {
RegisterSource string `json:"register_source"`
}

func (s *ServiceContext) String() string {
return fmt.Sprintf("%+v", *s)
}

type ModelFusion struct {
Key string `json:"key"`
Weight float64 `json:"weight"`
}

func (c *CreateImageRequest) String() string {
return fmt.Sprintf("%+v", *c)
}

type CreateImageResponse struct {
BaseResponse
Data CreateImageData `json:"data"`
Expand Down Expand Up @@ -1346,3 +1354,103 @@ type ImageInfoPro struct {
AdPrompt string `json:"ad_prompt"`
} `json:"adetailer"`
}

type CameraTemplateOptionsResponse struct {
BaseResponse
Data []CameraTemplateOption `json:"data"`
}

type CameraTemplateOption struct {
Key string `json:"_key"`
Category string `json:"category"`
Url string `json:"url"`
}

type CreateCameraRequest struct {
AvtarKey string `json:"avtar_key"`
CameraArtworkAdvanced *CameraArtworkAdvanced `json:"camera_artwork_advanced"`
TemplateCreateParam *TemplateCreateParam `json:"template_create_param"`
}

func (c *CreateCameraRequest) String() string {
return fmt.Sprintf("%+v", *c)
}

type CameraArtworkAdvanced struct {
FirstDiffusionSteps int `json:"first_diffusion_steps"`
FirstDenoisingStrength float64 `json:"first_denoising_strength"`
SecondDiffusionSteps int `json:"second_diffusion_steps"`
SecondDenoisingStrength float64 `json:"second_denoising_strength"`
CropFacePreprocess bool `json:"crop_face_preprocess"`
BeforeFaceFusionRatio float64 `json:"before_face_fusion_ratio"`
AfterFaceFusionRatio float64 `json:"after_face_fusion_ratio"`
ApplyFaceFusionBefore bool `json:"apply_face_fusion_before"`
ApplyFaceFusionAfter bool `json:"apply_face_fusion_after"`
ColorShiftMiddle bool `json:"color_shift_middle"`
ColorShiftLast bool `json:"color_shift_last"`
SuperResolution bool `json:"super_resolution"`
BackgroundRestore bool `json:"background_restore"`
}

func (c *CameraArtworkAdvanced) String() string {
return fmt.Sprintf("%+v", *c)
}

type TemplateCreateParam struct {
TemplateKey string `json:"template_key"`
TemplateUrl string `json:"template_url"`
Count int `json:"count"`
}

func (t *TemplateCreateParam) String() string {
return fmt.Sprintf("%+v", *t)
}

type CreateCameraResponse struct {
BaseResponse
Data CreateCameraResult `json:"data"`
}

type CreateCameraResult struct {
Keys []string `json:"keys"`
ExpectedDurationCost int `json:"expected_duration_cost"`
}

type CameraGeneratingInfoResponse struct {
BaseResponse
Data struct {
Infos []CameraGeneratingInfo `json:"infos"`
} `json:"data"`
}

type CameraGeneratingInfo struct {
Key string `json:"key"`
Status int `json:"status"`
ArtworkUrl string `json:"artwork_url"`
ExpectedSeconds int `json:"expected_seconds"`
StartGenTime int `json:"start_gen_time"`
CompleteTime int `json:"complete_time"`
CompletePercent float64 `json:"complete_percent"`
FailMessage struct {
FailCode int `json:"fail_code"`
FailMessage string `json:"fail_message"`
} `json:"fail_message"`
}

type CameraInfoResponse struct {
BaseResponse
Data CameraInfo `json:"data"`
}

type CameraInfo struct {
Key string `json:"key"`
Status int `json:"status"`
ArtworkUrl string `json:"artwork_url"`
Width int `json:"width"`
Height int `json:"height"`
Seed string `json:"seed"`
FailMessage struct {
FailCode int `json:"fail_code"`
FailMessage string `json:"fail_message"`
} `json:"fail_message"`
}

0 comments on commit 089f91d

Please sign in to comment.