Skip to content
Snippets Groups Projects
  • Christoph Glaubitz's avatar
    fb7848d1
    Prepared client for POST/PUT/PATCH/DELETE · fb7848d1
    Christoph Glaubitz authored
    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)
    }
    ```
    fb7848d1
    History
    Prepared client for POST/PUT/PATCH/DELETE
    Christoph Glaubitz authored
    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)
    }
    ```
Code owners
Assign users and groups as approvers for specific file changes. Learn more.