ncw / swift

Go language interface to Swift / Openstack Object Storage / Rackspace cloud files (golang)
MIT License
313 stars 107 forks source link

Support for parsing json in ObjectNames() #181

Open yheonhochoi opened 1 year ago

yheonhochoi commented 1 year ago

Now the swifth client assumes ObjectNames are plain for the response and only calls readLines().

The default value of swift is plain, but there are cases where format=json is used, and support for this case is needed.

How about change readJson() or readLines() depending on "Contect-Type" like below?

// ObjectNames returns a slice of names of objects in a given container.
func (c *Connection) ObjectNames(ctx context.Context, container string, opts *ObjectsOpts) ([]string, error) {
    v, h := opts.parse()
    resp, _, err := c.storage(ctx, RequestOpts{
        Container:  container,
        Operation:  "GET",
        Parameters: v,
        ErrorMap:   ContainerErrorMap,
        Headers:    h,
    })
    if err != nil {
        return nil, err
    }
    if resp.Header.Get("Content-Type") == "application/json" {
        var objects []Object
        err := readJson(resp, &objects)

        var names []string
        for _, obj := range objects {
            names = append(names, obj.Name)
        }

        return names, err
    } else {
        return readLines(resp)
    }
}