Skip to content

Commit

Permalink
Added search capabilities as asked on googleapis#211.
Browse files Browse the repository at this point in the history
Updated Readme with the Query.search documentation.
  • Loading branch information
Alejandro Casanovas committed Apr 3, 2019
1 parent d97964e commit ff6bc15
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
44 changes: 38 additions & 6 deletions O365/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from .windows_tz import get_iana_tz, get_windows_tz
from .decorators import fluent


ME_RESOURCE = 'me'
USERS_RESOURCE = 'users'

Expand Down Expand Up @@ -570,12 +569,14 @@ def __init__(self, attribute=None, *, protocol):
self._order_by = OrderedDict()
self._selects = set()
self._expands = set()
self._search = None

def __str__(self):
return 'Filter: {}\nOrder: {}\nSelect: {}\nExpand: {}'.format(self.get_filters(),
self.get_order(),
self.get_selects(),
self.get_expands())
return 'Filter: {}\nOrder: {}\nSelect: {}\nExpand: {}\nSearch: {}'.format(self.get_filters(),
self.get_order(),
self.get_selects(),
self.get_expands(),
self._search)

def __repr__(self):
return self.__str__()
Expand Down Expand Up @@ -622,8 +623,29 @@ def expand(self, *relationships):

return self

@fluent
def search(self, text):
"""
Perform a search.
Not from graph docs:
You can currently search only message and person collections.
A $search request returns up to 250 results.
You cannot use $filter or $orderby in a search request.
:param str text: the text to search
:return: the Query instance
"""
if text is None:
self._search = None
else:
# filters an order are not allowed
self.clear_filters()
self.clear_order()
self._search = '"{}"'.format(text)

return self

def as_params(self):
""" Returns the filters and orders as query parameters
""" Returns the filters, orders, select, expands and search as query parameters
:rtype: dict
"""
Expand All @@ -636,6 +658,10 @@ def as_params(self):
params['$select'] = self.get_selects()
if self.has_expands:
params['$expand'] = self.get_expands()
if self._search:
params['$search'] = self._search
params.pop('$filter', None)
params.pop('$orderby', None)
return params

@property
Expand Down Expand Up @@ -767,6 +793,10 @@ def clear_filters(self):
""" Clear filters """
self._filters = []

def clear_order(self):
""" Clears any order commands """
self._order_by = OrderedDict()

@fluent
def clear(self):
""" Clear everything
Expand All @@ -779,6 +809,8 @@ def clear(self):
self._negation = False
self._attribute = None
self._chain = None
self._search = None

return self

@fluent
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -785,16 +785,16 @@ for message in messages: # 100 loops with 4 requests to the api server

#### The Query helper

When using the Office 365 API you can filter some fields.
When using the Office 365 API you can filter, order, select, expand or search on some fields.
This filtering is tedious as is using [Open Data Protocol (OData)](http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/part2-url-conventions/odata-v4.0-errata03-os-part2-url-conventions-complete.html).

Every `ApiComponent` (such as `MailBox`) implements a new_query method that will return a `Query` instance.
This `Query` instance can handle the filtering (and sorting and selecting) very easily.
This `Query` instance can handle the filtering, sorting, selecting, expanding and search very easily.

For example:

```python
query = mailbox.new_query()
query = mailbox.new_query() # you can use the shorthand: mailbox.q()

query = query.on_attribute('subject').contains('george best').chain('or').startswith('quotes')

Expand All @@ -821,6 +821,18 @@ query = mailbox.new_query().select('subject', 'to_recipients', 'created_date_tim
messages_with_selected_properties = mailbox.get_messages(query=query)
```

You can also search content. As said in the graph docs:

> You can currently search only message and person collections. A $search request returns up to 250 results. You cannot use $filter or $orderby in a search request.

> If you do a search on messages and specify only a value without specific message properties, the search is carried out on the default search properties of from, subject, and body.

```python
# searching is the easy part ;)
query = mailbox.q().search('george best is da boss')
messages = mailbox.get_messages(query=query)
```

#### Request Error Handling

Whenever a Request error raises, the connection object will raise an exception.
Expand Down

0 comments on commit ff6bc15

Please sign in to comment.