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 <[email protected]> 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 < > [email protected]> 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 [email protected]. >> To post to this group, send email to [email protected]. >> 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 >> <https://groups.google.com/d/msgid/django-users/59821a72-3471-4a9a-affd-3875d28e3a03%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> 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 [email protected]. > To post to this group, send email to [email protected]. > 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 > <https://groups.google.com/d/msgid/django-users/CAFWa6tJkyPUex%2BzdErn3nnozeYtaM-fbdqoZfEq04b%3D3t2CtsQ%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > > 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 [email protected]. To post to this group, send email to [email protected]. 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/CAFhEBEAjVypPg5c6aEr6ysGjJNwY6rF4e%3DmY18s7Jfuwqv%2BfrA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

