Skip to content

Commit

Permalink
introducing web requests
Browse files Browse the repository at this point in the history
Can make web requests including 'get', 'post', 'put'
#1
  • Loading branch information
ashwini0529 committed Sep 23, 2017
1 parent df6034b commit fc9eec7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hacky/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import generator
import generator, web
51 changes: 51 additions & 0 deletions hacky/web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests

"""
Defining this function as of now. Will come back to this later. Need to have a generic function for all the scrape related actions
"""


def scrape(url): # type can be JSON as of now
scraped_webpage = requests.get(url)
if type != "JSON":
return scraped_webpage.text
try:
return scraped_webpage.json()
except ValueError:
return 'No JSON object could be decoded'


def request(url, **kwargs):
if kwargs is not None:
try:
payload = kwargs['params']
except KeyError:
payload = None
return 'It only supports optional argument: method, params and type'
try:
method = kwargs['method']
except KeyError:
print 'Please mention the method: get, post, put'
return

try:
return_type = kwargs['type']
except KeyError:
return_type = None

if method.lower() == "get":
r = requests.get(url, params=payload)
elif method.lower() == "post":
r = requests.post(url, params=payload)
elif method.lower() == "put":
r = requests.put(url, params=payload)
else:
return 'Method not allowed'

if return_type == "JSON":
return r.json()
else:
return r.text
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
urllib3==1.22
Faker==0.8.4
requests==2.18.4

0 comments on commit fc9eec7

Please sign in to comment.