Skip to content

Fix/post requests #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 27, 2020
Merged

Fix/post requests #41

merged 16 commits into from
Apr 27, 2020

Conversation

jobala
Copy link
Contributor

@jobala jobala commented Apr 21, 2020

Overview

This PRs makes the following changes

  • Refactor how the middleware control is used
  • Refactor how HTTP methods are implemented in GraphSession

Why make these changes

GraphSession extends Session with Graph use cases like adding support for middleware options. To do this we were building a Request object and passing it through the middleware pipeline, together with other arguments -- **kwargs. This is where I went wrong with the previous implementation.

The _prepare_and_send_request method, built the request object, removed the scopes from **kwargs and mutated the request object by adding to it a scopes property. This was done because the Request constructor doesn't expect scopes to be passed. It then sends the request object down the pipeline with some arguments. This breaks the send method -- bug -- when it gets arguments it doesn't expect like headers.

To fix this, I needed to stop drilling arguments from users to the send method. That means, I need to stop calling send in _prepare_and_send_request. So I decided to call Session's HTTP methods in GraphSession instead of prepare_and_send request.

class GraphSession:
   def get(url, **kwargs)
       super().get(url, **kwargs)

instead of

class GraphSession
     def get(url, **kwargs)
           self._prepare_and_send_request()

But now we have another bug, if we call graph_session.get('/me', scopes=[]), get will break because super().get(url, **kwargs) doesn't expect scopes to be in **kwargs. So I had to figure out a way of removing scopes or more broadly middleware options -- from **kwargs -- before we call super().get(url, **kwargs). I did this using a decorator.

    def get_middleware_options(self, func):
        self._reset_middleware_options()

        def wrapper(*args, **kwargs):
            scopes = kwargs.pop('scopes')
            self.set(AUTH_MIDDLEWARE_OPTIONS, AuthMiddlewareOptions(scopes))
            return func(*args, **kwargs)
        return wrapper

The decorater lives in the MiddlewareControl class.

This how it is used

    @middleware_control.get_middleware_options
    def get(self, url, **kwargs):
        return super().get(self._graph_url(url))

For all requests, the decorator does the following;

  • Reset the middleware_options property, so that they are not persisted across requests
  • Get and remove the middleware options from **kwargs, before it reaches super().get(url, **kwargs)
  • Add the middleware options to middleware_options, for use in the middleware pipeline.

The MiddlewareController is exposed as a singleton for use in the MiddlewarePipeline. The snippet below demonstrates how it is used.

middleware_control.get(AUTH_MIDDLEWARE_OPTIONS)

Fixes #39
Closes #13
Fixes AB#4607

@jobala jobala mentioned this pull request Apr 21, 2020
@jobala jobala changed the base branch from feat/graph-session-scopes to dev April 22, 2020 09:13
@jobala jobala requested review from MIchaelMainer and ddyett April 23, 2020 12:51
@jobala jobala marked this pull request as ready for review April 23, 2020 12:51
@jobala jobala merged commit 14fe407 into dev Apr 27, 2020
@jobala jobala deleted the fix/post-requests branch April 27, 2020 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to Pass Request Body Sample
2 participants