Skip to content
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
8 changes: 4 additions & 4 deletions src/msgraph_core/requests/batch_request_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class StreamInterface(BytesIO):


class BatchRequestItem(Parsable):
API_VERSION_REGEX = re.compile(r'/\/(v1.0|beta)/')
ME_TOKEN_REGEX = re.compile(r'/\/users\/me-token-to-replace/')
API_VERSION_REGEX = re.compile(r'^/(v1\.0|beta)(?=/|$)')
ME_TOKEN_REGEX = re.compile(r'/users/me-token-to-replace')

def __init__(
self,
Expand All @@ -47,7 +47,7 @@ def __init__(
self._method = request_information.http_method
self._headers: Optional[dict[str, str]] = request_information.request_headers
self._body = request_information.content
self.url = request_information.url.replace('/users/me-token-to-replace', '/me', 1)
self.set_url(request_information.url)
self._depends_on: Optional[list[str]] = []
if depends_on is not None:
self.set_depends_on(depends_on)
Expand Down Expand Up @@ -114,7 +114,7 @@ def set_url(self, url: str) -> None:
f"Error occurred during regex replacement of API version in URL string: {url}"
)

relative_url = relative_url.replace('/users/me-token-to-replace', '/me', 1)
relative_url = re.sub(BatchRequestItem.ME_TOKEN_REGEX, '/me', relative_url, 1)
if not relative_url:
raise ValueError(
f"""Error occurred during regex replacement
Expand Down
36 changes: 29 additions & 7 deletions tests/requests/test_batch_request_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def batch_request_item(request_info):
def test_initialization(batch_request_item, request_info):
assert batch_request_item.id == "123"
assert batch_request_item.method == "GET"
assert batch_request_item.url == base_url
assert batch_request_item.url == "/me"
assert batch_request_item.headers == {"content-type": "application/json"}
assert batch_request_item.body == b'{"key": "value"}'

Expand All @@ -43,7 +43,7 @@ def test_create_with_urllib_request():
urllib_request.data = b'{"key": "value"}'
batch_request_item = BatchRequestItem.create_with_urllib_request(urllib_request)
assert batch_request_item.method == "POST"
assert batch_request_item.url == "https://graph.microsoft.com/v1.0/me"
assert batch_request_item.url == "/me"
assert batch_request_item.body == b'{"key": "value"}'


Expand All @@ -54,7 +54,29 @@ def test_set_depends_on(batch_request_item):

def test_set_url(batch_request_item):
batch_request_item.set_url("https://graph.microsoft.com/v1.0/me")
assert batch_request_item.url == "/v1.0/me"
assert batch_request_item.url == "/me"


def test_set_url_beta(batch_request_item):
batch_request_item.set_url("https://graph.microsoft.com/beta/me/messages?$top=5#frag")
assert batch_request_item.url == "/me/messages?$top=5#frag"


def test_set_url_already_relative(batch_request_item):
batch_request_item.set_url("/users/u1/messages")
assert batch_request_item.url == "/users/u1/messages"


def test_set_url_keeps_version_like_segments(batch_request_item):
batch_request_item.set_url("https://graph.microsoft.com/v1.0/sites/beta-site/lists")
assert batch_request_item.url == "/sites/beta-site/lists"


def test_set_url_only_strips_a_leading_version(batch_request_item):
batch_request_item.set_url("/sites/beta/lists")
assert batch_request_item.url == "/sites/beta/lists"
batch_request_item.set_url("https://graph.microsoft.com/v1.0/sites/beta/lists")
assert batch_request_item.url == "/sites/beta/lists"


def test_constructor_url_replacement():
Expand All @@ -66,7 +88,7 @@ def test_constructor_url_replacement():

batch_request_item = BatchRequestItem(request_info)

assert batch_request_item.url == "https://graph.microsoft.com/v1.0/me"
assert batch_request_item.url == "/me"


def test_set_url_replacement():
Expand All @@ -79,7 +101,7 @@ def test_set_url_replacement():
batch_request_item = BatchRequestItem(request_info)
batch_request_item.set_url("https://graph.microsoft.com/v1.0/users/me-token-to-replace")

assert batch_request_item.url == "/v1.0/me"
assert batch_request_item.url == "/me"


def test_constructor_url_replacement_with_query():
Expand All @@ -91,7 +113,7 @@ def test_constructor_url_replacement_with_query():

batch_request_item = BatchRequestItem(request_info)

assert batch_request_item.url == "https://graph.microsoft.com/v1.0/me?param=value"
assert batch_request_item.url == "/me?param=value"


def test_id_property(batch_request_item):
Expand Down Expand Up @@ -138,7 +160,7 @@ def test_serialize_json(batch_request_item):
content = json.loads(writer.get_serialized_content())
assert content["id"] == "123"
assert content["method"] == "GET"
assert content["url"] == base_url
assert content["url"] == "/me"
assert content["headers"] == {"content-type": "application/json"}
assert content["body"] == {"key": "value"}

Loading