how to mock REST calls during development?

99 views
Skip to first unread message

Abraham Varricatt

unread,
May 25, 2015, 8:33:36 AM5/25/15
to django...@googlegroups.com
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.






Avraham Serour

unread,
May 25, 2015, 8:47:48 AM5/25/15
to django...@googlegroups.com
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

--
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.

Jirka Vejrazka

unread,
May 25, 2015, 10:17:26 AM5/25/15
to Django users
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



Abraham Varricatt

unread,
May 27, 2015, 11:33:16 AM5/27/15
to django...@googlegroups.com
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.

Jirka Vejrazka

unread,
May 27, 2015, 3:10:11 PM5/27/15
to Django users
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

Reply all
Reply to author
Forward
0 new messages