netbox-community / go-netbox

The official Go API client for Netbox IPAM and DCIM service.
Other
195 stars 148 forks source link

Prepared client for POST/PUT/PATCH/DELETE #12

Closed chrigl closed 7 years ago

chrigl commented 7 years ago

Here is one more groundworking PR. With this in, I'm able to implement a working type TenantGroup with Get, List, Extract, Create, Update and Delete.

  1. Fixed issue with concating urls, removed use of path.Join

The problem here: With path.Join, the trailing slash has been removed on each call. Even when using NewRequest(..., "/api/XXX/", nil). As a result, Do always queries http://nebox.example.com/api/XXX, getting back a 301 and retries the new Location. While this works well with GET, it breaks all the pushing methods. To not override too much, or too less, of Client.u, I append/prepend/remove slashes if necessary.

  1. Added two new functions to construct requests

As a result, using NewRequest stays the same, but NewJSONRequest can be used to create "writing" functions, without the need of repeatedly checking for errors. e.g. for tenant-groups:

func (s *TenantGroupsService) Update(group *TenantGroup) (*TenantGroup, error) {
    req, err := s.c.NewJSONRequest(
        http.MethodPatch,
        fmt.Sprintf("api/tenancy/tenant-groups/%d/", group.ID),
        nil,
        group)
    if err != nil {
        return nil, err
    }

    g := new(TenantGroup)
    err = s.c.Do(req, g)
    return g, err
}

func (s *TenantGroupsService) Delete(group *TenantGroup) error {
    req, err := s.c.NewRequest(
        http.MethodDelete,
        fmt.Sprintf("api/tenancy/tenant-groups/%d/", group.ID),
        nil)
    if err != nil {
        return err
    }

    return s.c.Do(req, nil)
}
mdlayher commented 7 years ago

Apologies for the delay. Got this notification and forgot. Looking now.

chrigl commented 7 years ago

All done and thanks for your feedback.

Don't worry about delayed responding! I am not in a hurry :)

mdlayher commented 7 years ago

LGTM, thank you!