Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

Support customizing “Accept” header #1428 #236

Merged
merged 1 commit into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions dynamic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,14 @@ def request(self, method, path, body=None, **params):
header_params = params.get('header_params', {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I didn't realize we had a header_params already. It looks like the user can already pass in 'Accept' but it just gets overwritten unconditionally.

let's do this then:

# User didn't specify the Accept header. Default it to json and yaml.
if not header_params.has_key('Accept'):
    header_params['Accept'] = self.client.select_header_accept([
        'application/json',
        'application/yaml',
    ])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

form_params = []
local_var_files = {}
# HTTP header `Accept`
header_params['Accept'] = self.client.select_header_accept([
'application/json',
'application/yaml',
])

# Checking Accept header.
new_header_params = dict((key.lower(), value) for key, value in header_params.items())
if not 'accept' in new_header_params:
header_params['Accept'] = self.client.select_header_accept([
'application/json',
'application/yaml',
])

# HTTP header `Content-Type`
if params.get('content_type'):
Expand Down
18 changes: 17 additions & 1 deletion dynamic/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,27 @@ def test_configmap_apis(self):

resp = api.get(namespace='default', pretty=True, label_selector="e2e-test=true")
self.assertEqual([], resp.items)

def test_node_apis(self):
client = DynamicClient(api_client.ApiClient(configuration=self.config))
api = client.resources.get(api_version='v1', kind='Node')

for item in api.get().items:
node = api.get(name=item.metadata.name)
self.assertTrue(len(dict(node.metadata.labels)) > 0)

# test_node_apis_partial_object_metadata lists all nodes in the cluster, but only retrieves object metadata
def test_node_apis_partial_object_metadata(self):
client = DynamicClient(api_client.ApiClient(configuration=self.config))
api = client.resources.get(api_version='v1', kind='Node')

params = {'header_params': {'Accept': 'application/json;as=PartialObjectMetadataList;v=v1;g=meta.k8s.io'}}
resp = api.get(**params)
self.assertEqual('PartialObjectMetadataList', resp.kind)
self.assertEqual('meta.k8s.io/v1', resp.apiVersion)

params = {'header_params': {'aCcePt': 'application/json;as=PartialObjectMetadataList;v=v1;g=meta.k8s.io'}}
resp = api.get(**params)
self.assertEqual('PartialObjectMetadataList', resp.kind)
self.assertEqual('meta.k8s.io/v1', resp.apiVersion)