Skip to content

Commit b4d6c66

Browse files
Merge pull request #451 from watson-developer-cloud/readme
feat(readme): Update readme with gdpr support
2 parents 4474038 + bc72c99 commit b4d6c66

3 files changed

Lines changed: 56 additions & 4 deletions

File tree

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,50 @@ response = assistant.message(workspace_id=workspace_id, input={
9393
print(json.dumps(response, indent=2))
9494
```
9595

96+
## Sending Request Headers
97+
Custom headers can be passed in any request in the form of a `dict` as:
98+
```python
99+
headers = {
100+
'Custom-Header': 'custom_value'
101+
}
102+
```
103+
For example, to send a header called `Custom-Header` to a call in Watson Assistant, pass
104+
the headers parameter as:
105+
```python
106+
from watson_developer_cloud import AssistantV1
107+
108+
assistant = AssistantV1(
109+
username='xxx',
110+
password='yyy',
111+
version='2017-04-21')
112+
113+
response = assistant.list_workspaces(headers={'Custom-Header': 'custom_value'})
114+
```
115+
116+
## Parsing HTTP Response Info
117+
If you would like access to some HTTP response information along with the response model, you can set the `set_detailed_response()` to `True`
118+
```python
119+
from watson_developer_cloud import AssistantV1
120+
121+
assistant = AssistantV1(
122+
username='xxx',
123+
password='yyy',
124+
version='2017-04-21')
125+
126+
assistant.set_detailed_response(True)
127+
response = assistant.list_workspaces(headers={'Custom-Header': 'custom_value'})
128+
print(response)
129+
```
130+
131+
This would give an output of `DetailedResponse` having the structure:
132+
```python
133+
{
134+
'result': <response returned by service>,
135+
'headers': { <http response headers> }
136+
}
137+
```
138+
You can use the `get_result()` and `get_headers()` to return the result and headers respectively.
139+
96140
## Dependencies
97141

98142
* [requests]

examples/tone_analyzer_v3.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@
5353
headers={'Custom-Header': 'custom_value'})
5454

5555
print(tone)
56+
print(tone.get_headers())
57+
print(tone.get_result())

watson_developer_cloud/watson_service.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,21 @@ class DetailedResponse(object):
145145
:param dict headers: A dict of response headers
146146
"""
147147
def __init__(self, response=None, headers=None):
148-
self.response = response
148+
self.result = response
149149
self.headers = headers
150150

151+
def get_result(self):
152+
return self.result
153+
154+
def get_headers(self):
155+
return self.headers
156+
151157
def _to_dict(self):
152158
_dict = {}
153-
if hasattr(self, 'response') and self.response is not None:
154-
_dict['response'] = self.response
159+
if hasattr(self, 'result') and self.result is not None:
160+
_dict['result'] = self.result if isinstance(self.result, dict) else 'HTTP response'
155161
if hasattr(self, 'headers') and self.headers is not None:
156-
_dict['details'] = {'headers': self.headers}
162+
_dict['headers'] = self.headers
157163
return _dict
158164

159165
def __str__(self):

0 commit comments

Comments
 (0)