Created
July 26, 2018 19:21
-
-
Save openfly/ced7058df9ff036c37760dc35391f53d to your computer and use it in GitHub Desktop.
example using http ref that works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
''' | |
giphy demo | |
''' | |
__author__ = 'Matt Joyce' | |
__email__ = 'matt@nycresistor.com' | |
__copyright__ = 'Copyright 2016-2018, BLAH' | |
import requests | |
class NOPKCS(): | |
def __init__(self): | |
self.blah = 'dummy info' | |
def GET_query(uri, req_args=None, headers=None): | |
''' Generic GET query method ''' | |
# GET request methods only require sessionTokens | |
if headers is None: | |
headers = {'Content-Type': 'application/json'} | |
# headers = {'content-type': 'application/octet-stream'} | |
# HTTP GET query method using requests module | |
try: | |
if req_args is None: | |
response = requests.get(uri, | |
stream=True, | |
headers=headers, | |
verify=True) | |
else: | |
response = requests.get(uri + str(req_args), | |
headers=headers, | |
# settings.STATICMAP_URL.format(**data), | |
stream=True, | |
verify=True) | |
except requests.exceptions.RequestException as e: | |
print e | |
return '500', 'Internal Error in RESTful.GET_query()' | |
# return the token | |
return response.status_code, response.text | |
def POST_query(self, uri, req_args=None, files=None, headers=None,): | |
''' Generic POST query method ''' | |
# HTTP POST queries require keyManagerTokens and sessionTokens | |
if files is None: | |
print 'no attachment specified' | |
if headers is None: | |
print 'headers not specified defaulting' | |
headers = {'Content-Type': 'application/json'} | |
# HTTP POST query to keymanager authenticate API | |
print req_args | |
try: | |
response = requests.post(uri, | |
headers=headers, | |
data=req_args, | |
files=files, | |
verify=True) | |
except requests.exceptions.RequestException as errstr: | |
return '500', 'Internal Error in RESTful.POST_query(' + errstr + ')' | |
# return the token | |
return response.status_code, response | |
def main(): | |
''' main loop ''' | |
REST = NOPKCS() | |
headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json', 'api_key': 'OtlAknY7gvPF7dIkknOtl6U7aURH7yDy'} | |
req_args = {'tags': 'example, upload, wtf', 'source_image_url': 'http://surly.bike/bike.mp4', 'source_post_url': 'http://surly.bike/'} | |
uri = 'https://upload.giphy.com/v1/gifs' | |
status_code, response = REST.POST_query(uri, req_args=req_args, headers=headers) | |
print "(%s) : %s" % (str(status_code), response.text) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment