Wednesday, May 27, 2015

Re: how to mock REST calls during development?

Hi Abraham,

  I guess my advantage is that my applications are quite tightly coupled, so I can set some flags in settings.py.

  The key point is that I'm importing class instance from my API code. That instance represents either the real endpoint or mocked endpoint based on some flags in settings.py

  I've pasted relevant code here: http://dpaste.com/00DCQT6

  HTH

    Jirka

On 27 May 2015 at 17:33, Abraham Varricatt <abraham.varricatt@googlemail.com> wrote:
Hello Jirka,

That's right. I'd forgotten that the settings.py could be used to initialize something like this! (facepalm!) But since you mentioned it, .. how would you initialize (or call mock_setup()) from your app or model? I can't recollect any entry point which can be used.

-Abraham V.


On Monday, 25 May 2015 19:47:26 UTC+5:30, JirkaV wrote:
Hi Abraham,

  I needed to solve this a few months ago, decided to use https://github.com/getsentry/responses

  The way I did this is that I have a function that sets all required responses (using the endpoint URL and the expected response in a file). Then I detect in the application setup (apps, models, settings - whatever you prefer) if I'm on a development machine and call that "mock_setup()" in that case.

  Relatively simple code and no change required to my production views.

  Small sample at http://dpaste.com/0E1PNVQ

  HTH

   Jirka



On 25 May 2015 at 14:47, Avraham Serour <tov...@gmail.com> wrote:
I used https://pypi.python.org/pypi/requests-mock, but the mocking was done on the testCase class, the view was written without if DEBUG or anything similar

On Mon, May 25, 2015 at 3:33 PM, Abraham Varricatt <abraham....@googlemail.com> wrote:
Hello everyone,

I'm working on a Django application which needs to communicate with a 3rd-party REST API. In production the flow would be like this;
  1. end-user browser sends a request to my django server
  2. my server makes a remote REST call to 3rd party server
  3. 3rd party server responds
  4. my server sends back response to end-user's browser
I'm simulating the above flow during development by using the Httpretty mocking library. Here is how my view looks like;

import httpretty

THIRD_PARTY_SERVER
= http://api.gitlab.com/

def my_view(request):
   
if settings.DEBUG:
       
httpretty.enable()
       
httpretty.register_uri(httpretty.GET, THIRD_PARTY_SERVER, body='{some_mock_response_here}')

    partner_response
= requests.get(THIRD_PARTY_SERVER)

   
if settings.DEBUG:
       
httpretty.disable()
       
httpretty.reset()

   
# Do some stuff here
   
# ...

   
return render(request, 'template.html', context)


For the most part, the above works. I can experiment around without hitting the 3rd-party API. But it doesn't feel good. My mocking code is now part of the view function - not what I consider a good design. Problem is, I'm not sure how else this can be done? Does anyone have any better ideas? Note - I'm not doing any testing here. Just need a way to mock 3rd-party REST responses during development when I run "python manage.py runserver" for debugging/experimentation. 

Ideally, I'd like to move all the mocking code to it's own file and away from my views. This should somehow get activated when I start 'runserver' and work for all my views. 

Puzzled,
Abraham V.






--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/59821a72-3471-4a9a-affd-3875d28e3a03%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFWa6tJkyPUex%2BzdErn3nnozeYtaM-fbdqoZfEq04b%3D3t2CtsQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bed800c3-27ce-4adb-b838-db4dcb450c44%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFhEBEAu-gWoG4QXU%3DwhnYX0VT_oCSaRtuHWXoP%3D6ptxfetNNQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment