Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #284 from jrperritt/remove-BuildQuery
Browse files Browse the repository at this point in the history
update functions to use BuildQueryString instead of BuildQuery
  • Loading branch information
smashwilson committed Oct 28, 2014
2 parents 57847d7 + 1980344 commit da56de6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 50 deletions.
29 changes: 9 additions & 20 deletions openstack/identity/v3/endpoints/requests.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package endpoints

import (
"strconv"

"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
Expand Down Expand Up @@ -71,33 +69,24 @@ func Create(client *gophercloud.ServiceClient, opts EndpointOpts) CreateResult {
// ListOpts allows finer control over the endpoints returned by a List call.
// All fields are optional.
type ListOpts struct {
Availability gophercloud.Availability
ServiceID string
Page int
PerPage int
Availability gophercloud.Availability `q:"interface"`
ServiceID string `q:"service_id"`
Page int `q:"page"`
PerPage int `q:"per_page"`
}

// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
q := make(map[string]string)
if opts.Availability != "" {
q["interface"] = string(opts.Availability)
}
if opts.ServiceID != "" {
q["service_id"] = opts.ServiceID
u := listURL(client)
q, err := gophercloud.BuildQueryString(opts)
if err != nil {
return pagination.Pager{Err: err}
}
if opts.Page != 0 {
q["page"] = strconv.Itoa(opts.Page)
}
if opts.PerPage != 0 {
q["per_page"] = strconv.Itoa(opts.Page)
}

u += q.String()
createPage := func(r pagination.PageResult) pagination.Page {
return EndpointPage{pagination.LinkedPageBase{PageResult: r}}
}

u := listURL(client) + gophercloud.BuildQuery(q)
return pagination.NewPager(client, u, createPage)
}

Expand Down
24 changes: 8 additions & 16 deletions openstack/identity/v3/services/requests.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package services

import (
"strconv"

"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
Expand Down Expand Up @@ -32,25 +30,19 @@ func Create(client *gophercloud.ServiceClient, serviceType string) CreateResult

// ListOpts allows you to query the List method.
type ListOpts struct {
ServiceType string
PerPage int
Page int
ServiceType string `q:"type"`
PerPage int `q:"perPage"`
Page int `q:"page"`
}

// List enumerates the services available to a specific user.
func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
q := make(map[string]string)
if opts.ServiceType != "" {
q["type"] = opts.ServiceType
}
if opts.Page != 0 {
q["page"] = strconv.Itoa(opts.Page)
u := listURL(client)
q, err := gophercloud.BuildQueryString(opts)
if err != nil {
return pagination.Pager{Err: err}
}
if opts.PerPage != 0 {
q["perPage"] = strconv.Itoa(opts.PerPage)
}
u := listURL(client) + gophercloud.BuildQuery(q)

u += q.String()
createPage := func(r pagination.PageResult) pagination.Page {
return ServicePage{pagination.LinkedPageBase{PageResult: r}}
}
Expand Down
14 changes: 0 additions & 14 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,3 @@ func NormalizeURL(url string) string {
}
return url
}

// BuildQuery constructs the query section of a URI from a map.
func BuildQuery(params map[string]string) string {
if len(params) == 0 {
return ""
}

query := "?"
for k, v := range params {
query += k + "=" + v + "&"
}
query = query[:len(query)-1]
return query
}

0 comments on commit da56de6

Please sign in to comment.