Skip to content
Snippets Groups Projects
Commit fb7848d1 authored by Christoph Glaubitz's avatar Christoph Glaubitz
Browse files

Prepared client for POST/PUT/PATCH/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 slashes if necessary.

2. Added two new functions to construct requests

* c.NewDataRequest to create a request with body
* c.NewJSONRequest to make posts more convenient
* Changed c.NewRequest to be a wrap for NewJSONRequest

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)
}
```
parent 93500c8d
No related branches found
No related tags found
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment