Skip to content

Commit ff6bc15

Browse files
author
Alejandro Casanovas
committed
Added search capabilities as asked on googleapis#211.
Updated Readme with the Query.search documentation.
1 parent d97964e commit ff6bc15

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

O365/utils/utils.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from .windows_tz import get_iana_tz, get_windows_tz
1111
from .decorators import fluent
1212

13-
1413
ME_RESOURCE = 'me'
1514
USERS_RESOURCE = 'users'
1615

@@ -570,12 +569,14 @@ def __init__(self, attribute=None, *, protocol):
570569
self._order_by = OrderedDict()
571570
self._selects = set()
572571
self._expands = set()
572+
self._search = None
573573

574574
def __str__(self):
575-
return 'Filter: {}\nOrder: {}\nSelect: {}\nExpand: {}'.format(self.get_filters(),
576-
self.get_order(),
577-
self.get_selects(),
578-
self.get_expands())
575+
return 'Filter: {}\nOrder: {}\nSelect: {}\nExpand: {}\nSearch: {}'.format(self.get_filters(),
576+
self.get_order(),
577+
self.get_selects(),
578+
self.get_expands(),
579+
self._search)
579580

580581
def __repr__(self):
581582
return self.__str__()
@@ -622,8 +623,29 @@ def expand(self, *relationships):
622623

623624
return self
624625

626+
@fluent
627+
def search(self, text):
628+
"""
629+
Perform a search.
630+
Not from graph docs:
631+
You can currently search only message and person collections.
632+
A $search request returns up to 250 results.
633+
You cannot use $filter or $orderby in a search request.
634+
:param str text: the text to search
635+
:return: the Query instance
636+
"""
637+
if text is None:
638+
self._search = None
639+
else:
640+
# filters an order are not allowed
641+
self.clear_filters()
642+
self.clear_order()
643+
self._search = '"{}"'.format(text)
644+
645+
return self
646+
625647
def as_params(self):
626-
""" Returns the filters and orders as query parameters
648+
""" Returns the filters, orders, select, expands and search as query parameters
627649
628650
:rtype: dict
629651
"""
@@ -636,6 +658,10 @@ def as_params(self):
636658
params['$select'] = self.get_selects()
637659
if self.has_expands:
638660
params['$expand'] = self.get_expands()
661+
if self._search:
662+
params['$search'] = self._search
663+
params.pop('$filter', None)
664+
params.pop('$orderby', None)
639665
return params
640666

641667
@property
@@ -767,6 +793,10 @@ def clear_filters(self):
767793
""" Clear filters """
768794
self._filters = []
769795

796+
def clear_order(self):
797+
""" Clears any order commands """
798+
self._order_by = OrderedDict()
799+
770800
@fluent
771801
def clear(self):
772802
""" Clear everything
@@ -779,6 +809,8 @@ def clear(self):
779809
self._negation = False
780810
self._attribute = None
781811
self._chain = None
812+
self._search = None
813+
782814
return self
783815

784816
@fluent

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,16 +785,16 @@ for message in messages: # 100 loops with 4 requests to the api server
785785

786786
#### The Query helper
787787

788-
When using the Office 365 API you can filter some fields.
788+
When using the Office 365 API you can filter, order, select, expand or search on some fields.
789789
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).
790790

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

794794
For example:
795795

796796
```python
797-
query = mailbox.new_query()
797+
query = mailbox.new_query() # you can use the shorthand: mailbox.q()
798798

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

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

824+
You can also search content. As said in the graph docs:
825+
826+
> 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.
827+
828+
> 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.
829+
830+
```python
831+
# searching is the easy part ;)
832+
query = mailbox.q().search('george best is da boss')
833+
messages = mailbox.get_messages(query=query)
834+
```
835+
824836
#### Request Error Handling
825837

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

0 commit comments

Comments
 (0)