Skip to content

Commit

Permalink
add shares apis
Browse files Browse the repository at this point in the history
  • Loading branch information
bachue committed Sep 5, 2024
1 parent 1a17f24 commit 40355bf
Show file tree
Hide file tree
Showing 16 changed files with 897 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* 多媒体处理库 [media](github.com/qiniu/go-sdk/v7/media/apis) 包,提供多媒体处理接口
* IAM [iam](github.com/qiniu/go-sdk/v7/iam/apis) 包,提供权限管理系统接口
* 获取下载 URL 的实用方法
* 提供文件夹分享接口
* 修复
* `downloader.DownloadDirectory` 下载根目录出错的 Bug

Expand Down
96 changes: 96 additions & 0 deletions iam/apis/api_delete_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// THIS FILE IS GENERATED BY api-generator, DO NOT EDIT DIRECTLY!

package apis

import (
"context"
auth "github.com/qiniu/go-sdk/v7/auth"
deletegroup "github.com/qiniu/go-sdk/v7/iam/apis/delete_group"
uplog "github.com/qiniu/go-sdk/v7/internal/uplog"
errors "github.com/qiniu/go-sdk/v7/storagev2/errors"
httpclient "github.com/qiniu/go-sdk/v7/storagev2/http_client"
region "github.com/qiniu/go-sdk/v7/storagev2/region"
uptoken "github.com/qiniu/go-sdk/v7/storagev2/uptoken"
"strings"
"time"
)

type innerDeleteGroupRequest deletegroup.Request

func (path *innerDeleteGroupRequest) buildPath() ([]string, error) {
allSegments := make([]string, 0, 1)
if path.Alias != "" {
allSegments = append(allSegments, path.Alias)
} else {
return nil, errors.MissingRequiredFieldError{Name: "Alias"}
}
return allSegments, nil
}

type DeleteGroupRequest = deletegroup.Request
type DeleteGroupResponse = deletegroup.Response

// 删除用户分组
func (iam *Iam) DeleteGroup(ctx context.Context, request *DeleteGroupRequest, options *Options) (*DeleteGroupResponse, error) {
if options == nil {
options = &Options{}
}
innerRequest := (*innerDeleteGroupRequest)(request)
serviceNames := []region.ServiceName{region.ServiceApi}
if innerRequest.Credentials == nil && iam.client.GetCredentials() == nil {
return nil, errors.MissingRequiredFieldError{Name: "Credentials"}
}
pathSegments := make([]string, 0, 4)
pathSegments = append(pathSegments, "iam", "v1", "groups")
if segments, err := innerRequest.buildPath(); err != nil {
return nil, err
} else {
pathSegments = append(pathSegments, segments...)
}
path := "/" + strings.Join(pathSegments, "/")
var rawQuery string
uplogInterceptor, err := uplog.NewRequestUplog("deleteGroup", "", "", func() (string, error) {
credentials := innerRequest.Credentials
if credentials == nil {
credentials = iam.client.GetCredentials()
}
putPolicy, err := uptoken.NewPutPolicy("", time.Now().Add(time.Hour))
if err != nil {
return "", err
}
return uptoken.NewSigner(putPolicy, credentials).GetUpToken(ctx)
})
if err != nil {
return nil, err
}
req := httpclient.Request{Method: "DELETE", ServiceNames: serviceNames, Path: path, RawQuery: rawQuery, Endpoints: options.OverwrittenEndpoints, Region: options.OverwrittenRegion, Interceptors: []httpclient.Interceptor{uplogInterceptor}, AuthType: auth.TokenQiniu, Credentials: innerRequest.Credentials, OnRequestProgress: options.OnRequestProgress}
if options.OverwrittenEndpoints == nil && options.OverwrittenRegion == nil && iam.client.GetRegions() == nil {
bucketHosts := httpclient.DefaultBucketHosts()

req.Region = iam.client.GetAllRegions()
if req.Region == nil {
if options.OverwrittenBucketHosts != nil {
if bucketHosts, err = options.OverwrittenBucketHosts.GetEndpoints(ctx); err != nil {
return nil, err
}
}
allRegionsOptions := region.AllRegionsProviderOptions{UseInsecureProtocol: iam.client.UseInsecureProtocol(), HostFreezeDuration: iam.client.GetHostFreezeDuration(), Resolver: iam.client.GetResolver(), Chooser: iam.client.GetChooser(), BeforeSign: iam.client.GetBeforeSignCallback(), AfterSign: iam.client.GetAfterSignCallback(), SignError: iam.client.GetSignErrorCallback(), BeforeResolve: iam.client.GetBeforeResolveCallback(), AfterResolve: iam.client.GetAfterResolveCallback(), ResolveError: iam.client.GetResolveErrorCallback(), BeforeBackoff: iam.client.GetBeforeBackoffCallback(), AfterBackoff: iam.client.GetAfterBackoffCallback(), BeforeRequest: iam.client.GetBeforeRequestCallback(), AfterResponse: iam.client.GetAfterResponseCallback()}
if hostRetryConfig := iam.client.GetHostRetryConfig(); hostRetryConfig != nil {
allRegionsOptions.RetryMax = hostRetryConfig.RetryMax
allRegionsOptions.Backoff = hostRetryConfig.Backoff
}
credentials := innerRequest.Credentials
if credentials == nil {
credentials = iam.client.GetCredentials()
}
if req.Region, err = region.NewAllRegionsProvider(credentials, bucketHosts, &allRegionsOptions); err != nil {
return nil, err
}
}
}
resp, err := iam.client.Do(ctx, &req)
if err != nil {
return nil, err
}
return &DeleteGroupResponse{}, resp.Body.Close()
}
15 changes: 15 additions & 0 deletions iam/apis/delete_group/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// THIS FILE IS GENERATED BY api-generator, DO NOT EDIT DIRECTLY!

// 删除用户分组
package delete_group

import credentials "github.com/qiniu/go-sdk/v7/storagev2/credentials"

// 调用 API 所用的请求
type Request struct {
Alias string // 用户分组别名
Credentials credentials.CredentialsProvider // 鉴权参数,用于生成鉴权凭证,如果为空,则使用 HTTPClientOptions 中的 CredentialsProvider
}

// 获取 API 所用的响应
type Response struct{}
55 changes: 40 additions & 15 deletions internal/api-generator/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,20 @@ func (description *ApiDetailedDescription) generatePackage(group *jen.Group, opt
}
}
}
group.Add(jen.Id("pathSegments").Op(":=").Make(jen.Index().String(), jen.Lit(0), jen.Lit(guessPathSegmentsCount)))
if guessPathSegmentsCount > 0 {
group.Add(jen.Id("pathSegments").Op(":=").Make(jen.Index().String(), jen.Lit(0), jen.Lit(guessPathSegmentsCount)))
} else {
group.Add(jen.Id("pathSegments").Op(":=").Make(jen.Index().String(), jen.Lit(0)))
}
if description.BasePath != "" {
group.Add(jen.Id("pathSegments").Op("=").AppendFunc(func(group *jen.Group) {
group.Add(jen.Id("pathSegments"))
for _, pathSegment := range description.getBasePathSegments() {
group.Add(jen.Lit(pathSegment))
}
}))
if basePathSegments := description.getBasePathSegments(); len(basePathSegments) > 0 {
group.Add(jen.Id("pathSegments").Op("=").AppendFunc(func(group *jen.Group) {
group.Add(jen.Id("pathSegments"))
for _, pathSegment := range basePathSegments {
group.Add(jen.Lit(pathSegment))
}
}))
}
}
if description.Request.PathParams != nil {
group.Add(
Expand All @@ -207,14 +213,19 @@ func (description *ApiDetailedDescription) generatePackage(group *jen.Group, opt
)
}
if description.PathSuffix != "" {
group.Add(jen.Id("pathSegments").Op("=").AppendFunc(func(group *jen.Group) {
group.Add(jen.Id("pathSegments"))
for _, pathSegment := range description.getPathSuffixSegments() {
group.Add(jen.Lit(pathSegment))
}
}))
if suffixSegments := description.getPathSuffixSegments(); len(suffixSegments) > 0 {
group.Add(jen.Id("pathSegments").Op("=").AppendFunc(func(group *jen.Group) {
group.Add(jen.Id("pathSegments"))
for _, pathSegment := range suffixSegments {
group.Add(jen.Lit(pathSegment))
}
}))
}
}
group.Add(jen.Id("path").Op(":=").Lit("/").Op("+").Qual("strings", "Join").Call(jen.Id("pathSegments"), jen.Lit("/")))
if description.PathSuffix == "/" {
group.Add(jen.Id("path").Op("+=").Lit("/"))
}
if description.Command != "" {
group.Add(jen.Id("rawQuery").Op(":=").Lit(description.Command + "&"))
} else {
Expand Down Expand Up @@ -867,13 +878,27 @@ func (description *ApiDetailedDescription) addJsonMarshalerUnmarshaler(group *je
func (description *ApiDetailedDescription) getBasePathSegments() []string {
basePath := strings.TrimPrefix(description.BasePath, "/")
basePath = strings.TrimSuffix(basePath, "/")
return strings.Split(basePath, "/")
segments := strings.Split(basePath, "/")
newSegments := make([]string, 0, len(segments))
for _, segment := range segments {
if segment != "" {
newSegments = append(newSegments, segment)
}
}
return newSegments
}

func (description *ApiDetailedDescription) getPathSuffixSegments() []string {
pathSuffix := strings.TrimPrefix(description.PathSuffix, "/")
pathSuffix = strings.TrimSuffix(pathSuffix, "/")
return strings.Split(pathSuffix, "/")
segments := strings.Split(pathSuffix, "/")
newSegments := make([]string, 0, len(segments))
for _, segment := range segments {
if segment != "" {
newSegments = append(newSegments, segment)
}
}
return newSegments
}

func (description *ApiDetailedDescription) isBucketService() bool {
Expand Down
4 changes: 0 additions & 4 deletions internal/api-generator/form.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"fmt"

"github.com/dave/jennifer/jen"
Expand Down Expand Up @@ -217,9 +216,6 @@ func (form *FormUrlencodedRequestStruct) addSetCall(group *jen.Group, field Form
)
fieldName := field.camelCaseName()
if field.Multiple {
if field.Optional.ToOptionalType() != OptionalTypeRequired {
return errors.New("multiple field must be required")
}
valueConvertCode, err = field.Type.GenerateConvertCodeToString(jen.Id("value"))
if err != nil {
return err
Expand Down
13 changes: 6 additions & 7 deletions internal/api-generator/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,22 @@ func (names QueryNames) generateSetCall(group *jen.Group, queryName QueryName, q
fieldName := queryName.camelCaseName()
field := jen.Id("query").Dot(fieldName)
if queryName.Multiple {
if queryName.Optional.ToOptionalType() != OptionalTypeRequired {
return errors.New("multiple field must be required")
}
valueConvertCode, err = queryName.QueryType.GenerateConvertCodeToString(jen.Id("value"))
if err != nil {
return err
}
group.Add(jen.If(jen.Len(jen.Id("query").Dot(fieldName)).Op(">").Lit(0)).
code := jen.If(jen.Len(jen.Id("query").Dot(fieldName)).Op(">").Lit(0)).
BlockFunc(func(group *jen.Group) {
group.Add(
jen.For(jen.List(jen.Id("_"), jen.Id("value")).Op(":=").Range().Add(jen.Id("query").Dot(fieldName))).BlockFunc(func(group *jen.Group) {
group.Add(jen.Id(queryVarName).Dot("Add").Call(jen.Lit(queryName.QueryName), valueConvertCode))
}),
)
}).
Else().
BlockFunc(appendMissingRequiredFieldErrorFunc(fieldName)))
})
if queryName.Optional.ToOptionalType() == OptionalTypeRequired {
code = code.Else().BlockFunc(appendMissingRequiredFieldErrorFunc(fieldName))
}
group.Add(code)
} else {
if queryName.Optional.ToOptionalType() == OptionalTypeNullable {
valueConvertCode, err = queryName.QueryType.GenerateConvertCodeToString(jen.Op("*").Add(field))
Expand Down
Loading

0 comments on commit 40355bf

Please sign in to comment.