From 6bda59acc8944e85238e595295c2899282c1d885 Mon Sep 17 00:00:00 2001 From: Prithviraj Chaudhuri Date: Thu, 8 Dec 2022 12:38:40 -0500 Subject: [PATCH 1/3] updated test and methods --- pyigloo/__init__.py | 50 +++++++++++++++++++++++++++++++++++ tests/uploadUserProfilePic.py | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 tests/uploadUserProfilePic.py diff --git a/pyigloo/__init__.py b/pyigloo/__init__.py index e5adf3b..0686354 100644 --- a/pyigloo/__init__.py +++ b/pyigloo/__init__.py @@ -558,3 +558,53 @@ def user_get (self, userId): url = '{0}{1}/User/{2}/Get'.format(self.endpoint, self.IGLOO_API_ROOT_V2, userId) result = self.igloo.get(url) return result.json() + + def post_community_attachment(self, payload, communityKey): + """ + APIv2 .api2/api/v1/communities/{communityKey}/attachments + + Creates community attachment entry + """ + url = '{0}{1}v1/communities/{2}/attachments'.format(self.endpoint, self.IGLOO_API_ROOT_V2, communityKey) + result = self.igloo.post(url, data=payload) + return result.headers['Location'] + + def send_image_to_url(self, url, image, headers): + """ + APIv1 .api2/api/v1/communities/{communityKey}/attachments/uploads/{attachmentKey} + + Uploads attachment to entry provided from previous call + """ + url = '{0}{1}'.format(self.endpoint, url) + result = self.igloo.patch(url, headers=headers, data=image) + return result + + def associate_attachment_to_user(self, communityKey, attachmentKey, userId): + """ + APIv1 .api2/api/v1/communities/{communityKey}/attachments/{attachmentKey}/association/{objectId}?type=ProfilePhoto + + Tags attachment to user + """ + url = '{0}{1}v1/communities/{2}/attachments/{3}/association/{4}?type=ProfilePhoto'.format(self.endpoint, self.IGLOO_API_ROOT_V2, communityKey, attachmentKey, userId) + result = self.igloo.post(url) + return result + + def update_profile_picture(self, communityKey, userId, image, contentType): + """ + Calls the below methods in order to upload a profile picture to user + post_community_attachment + send_image_to_url + associate_attachment_to_user + """ + contentLength = len(image) + contentRange = "bytes 0-"+str(contentLength-1)+"/"+str(contentLength) + + payload = {"name": userId+".png", "contentType": contentType, "contentLength": contentLength} + location = self.post_community_attachment(payload, communityKey) + + headers = {"Content-Range": contentRange, "Content-Length": str(contentLength), "Content-Type": contentType} + + self.send_image_to_url(location[1:], image, headers) + + attachmentKey = location.split('/')[-1] + self.associate_attachment_to_user(communityKey, attachmentKey, userId) \ No newline at end of file diff --git a/tests/uploadUserProfilePic.py b/tests/uploadUserProfilePic.py new file mode 100644 index 0000000..95bcfce --- /dev/null +++ b/tests/uploadUserProfilePic.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- + +import sys +import os +import pathlib + + +from dotenv import load_dotenv +if os.getenv("ENV") != None: + load_dotenv('.env.' + os.getenv("ENV")) + +load_dotenv() + +try: + sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) +except NameError: + sys.path.insert(0, "../") + +import pyigloo + + +params = { + "ACCESS_KEY": os.getenv("ACCESS_KEY"), + "API_KEY": os.getenv("API_KEY"), + "API_USER": os.getenv("API_USER"), + "API_PASSWORD": os.getenv("API_PASSWORD"), + "API_ENDPOINT": os.getenv("API_ENDPOINT") +} + +import argparse +parser = argparse.ArgumentParser() +parser.add_argument("communityKey", help="Community Key") +parser.add_argument("userid", help="User ID") +parser.add_argument("image", help="Path to the image") +parser.add_argument("contentType", help="Content Type of the image") +args = parser.parse_args() + +igloo = pyigloo.igloo(params) + +allowedContentType = ['image/png', 'image/jpg'] + +if args.contentType not in allowedContentType: + print("Allowed contentTypes "+str(" ".joing(allowedContentType)) + return + + +image = pathlib.Path(args.image).read_bytes() +update_profile_picture(self, args.communityKey, args.userId, image, args.contentType) \ No newline at end of file From 01d0e4ce002936c124fd8c223b42cef18c25f251 Mon Sep 17 00:00:00 2001 From: Prithviraj Chaudhuri Date: Fri, 9 Dec 2022 16:19:18 -0500 Subject: [PATCH 2/3] updated with PR recommendations --- pyigloo/__init__.py | 28 ++++++++++++++++------------ tests/uploadUserProfilePic.py | 11 +++++++---- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/pyigloo/__init__.py b/pyigloo/__init__.py index 0686354..248a0eb 100644 --- a/pyigloo/__init__.py +++ b/pyigloo/__init__.py @@ -559,13 +559,13 @@ def user_get (self, userId): result = self.igloo.get(url) return result.json() - def post_community_attachment(self, payload, communityKey): + def post_community_attachment(self, payload): """ APIv2 .api2/api/v1/communities/{communityKey}/attachments Creates community attachment entry """ - url = '{0}{1}v1/communities/{2}/attachments'.format(self.endpoint, self.IGLOO_API_ROOT_V2, communityKey) + url = '{0}{1}v1/communities/{2}/attachments'.format(self.endpoint, self.IGLOO_API_ROOT_V2, self.communityKey) result = self.igloo.post(url, data=payload) return result.headers['Location'] @@ -575,36 +575,40 @@ def send_image_to_url(self, url, image, headers): Uploads attachment to entry provided from previous call """ + image = pathlib.Path(args.image).read_bytes() + + contentLength = len(image) + contentRange = "bytes 0-"+str(contentLength-1)+"/"+str(contentLength) + + headers = {"Content-Range": contentRange, "Content-Length": str(contentLength), "Content-Type": contentType} + url = '{0}{1}'.format(self.endpoint, url) result = self.igloo.patch(url, headers=headers, data=image) return result - def associate_attachment_to_user(self, communityKey, attachmentKey, userId): + def associate_attachment_to_user(self, attachmentKey, userId): """ APIv1 .api2/api/v1/communities/{communityKey}/attachments/{attachmentKey}/association/{objectId}?type=ProfilePhoto Tags attachment to user """ - url = '{0}{1}v1/communities/{2}/attachments/{3}/association/{4}?type=ProfilePhoto'.format(self.endpoint, self.IGLOO_API_ROOT_V2, communityKey, attachmentKey, userId) + url = '{0}{1}v1/communities/{2}/attachments/{3}/association/{4}?type=ProfilePhoto'.format(self.endpoint, self.IGLOO_API_ROOT_V2, self.communityKey, attachmentKey, userId) result = self.igloo.post(url) return result - def update_profile_picture(self, communityKey, userId, image, contentType): + def update_profile_picture(self, userId, filePath, contentType): """ Calls the below methods in order to upload a profile picture to user post_community_attachment send_image_to_url associate_attachment_to_user """ - contentLength = len(image) - contentRange = "bytes 0-"+str(contentLength-1)+"/"+str(contentLength) + contentLength = os.path.getsize(filePath) payload = {"name": userId+".png", "contentType": contentType, "contentLength": contentLength} - location = self.post_community_attachment(payload, communityKey) - - headers = {"Content-Range": contentRange, "Content-Length": str(contentLength), "Content-Type": contentType} + location = self.post_community_attachment(payload) - self.send_image_to_url(location[1:], image, headers) + self.send_image_to_url(location[1:], filePath, headers) attachmentKey = location.split('/')[-1] - self.associate_attachment_to_user(communityKey, attachmentKey, userId) \ No newline at end of file + self.associate_attachment_to_user(attachmentKey, userId) \ No newline at end of file diff --git a/tests/uploadUserProfilePic.py b/tests/uploadUserProfilePic.py index 95bcfce..0737795 100644 --- a/tests/uploadUserProfilePic.py +++ b/tests/uploadUserProfilePic.py @@ -3,6 +3,7 @@ import sys import os import pathlib +import magic from dotenv import load_dotenv @@ -31,7 +32,7 @@ parser = argparse.ArgumentParser() parser.add_argument("communityKey", help="Community Key") parser.add_argument("userid", help="User ID") -parser.add_argument("image", help="Path to the image") +parser.add_argument("filePath", help="Path to the image") parser.add_argument("contentType", help="Content Type of the image") args = parser.parse_args() @@ -39,10 +40,12 @@ allowedContentType = ['image/png', 'image/jpg'] +if args.contentType is None: + contentType = magic.detect_from_filename(args.filePath).mime_type + if args.contentType not in allowedContentType: print("Allowed contentTypes "+str(" ".joing(allowedContentType)) - return + exit() -image = pathlib.Path(args.image).read_bytes() -update_profile_picture(self, args.communityKey, args.userId, image, args.contentType) \ No newline at end of file +update_profile_picture(self, args.communityKey, args.userId, args.filePath, args.contentType) \ No newline at end of file From ba4dbc85a4480a79bcb08b7b42a17ce95618e8a8 Mon Sep 17 00:00:00 2001 From: Prithviraj Chaudhuri Date: Tue, 13 Dec 2022 09:03:56 -0500 Subject: [PATCH 3/3] changed params and fixed bugs --- pyigloo/__init__.py | 11 +++++++---- requirements.txt | 2 ++ tests/uploadUserProfilePic.py | 20 ++++++++++---------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/pyigloo/__init__.py b/pyigloo/__init__.py index 248a0eb..e5bddda 100644 --- a/pyigloo/__init__.py +++ b/pyigloo/__init__.py @@ -9,6 +9,8 @@ >>> """ +import os +import pathlib class igloo: @@ -569,13 +571,13 @@ def post_community_attachment(self, payload): result = self.igloo.post(url, data=payload) return result.headers['Location'] - def send_image_to_url(self, url, image, headers): + def send_image_to_url(self, url, image, contentType): """ APIv1 .api2/api/v1/communities/{communityKey}/attachments/uploads/{attachmentKey} Uploads attachment to entry provided from previous call """ - image = pathlib.Path(args.image).read_bytes() + image = pathlib.Path(image).read_bytes() contentLength = len(image) contentRange = "bytes 0-"+str(contentLength-1)+"/"+str(contentLength) @@ -604,11 +606,12 @@ def update_profile_picture(self, userId, filePath, contentType): associate_attachment_to_user """ contentLength = os.path.getsize(filePath) + fileName = os.path.basename(filePath) - payload = {"name": userId+".png", "contentType": contentType, "contentLength": contentLength} + payload = {"name": fileName, "contentType": contentType, "contentLength": contentLength} location = self.post_community_attachment(payload) - self.send_image_to_url(location[1:], filePath, headers) + self.send_image_to_url(location[1:], filePath, contentType) attachmentKey = location.split('/')[-1] self.associate_attachment_to_user(attachmentKey, userId) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 6d3327c..1bf499f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ requests>=2.8.1 +python-magic==0.4.27 +python-dotenv==0.20.0 \ No newline at end of file diff --git a/tests/uploadUserProfilePic.py b/tests/uploadUserProfilePic.py index 0737795..cabc03c 100644 --- a/tests/uploadUserProfilePic.py +++ b/tests/uploadUserProfilePic.py @@ -30,22 +30,22 @@ import argparse parser = argparse.ArgumentParser() -parser.add_argument("communityKey", help="Community Key") -parser.add_argument("userid", help="User ID") -parser.add_argument("filePath", help="Path to the image") -parser.add_argument("contentType", help="Content Type of the image") +parser.add_argument("--community_key", help="Community Key") +parser.add_argument("--user_id", help="User ID") +parser.add_argument("--file", help="Path to the image") +parser.add_argument("--content_type", help="Content Type of the image", required=False) args = parser.parse_args() igloo = pyigloo.igloo(params) +igloo.communityKey = args.community_key allowedContentType = ['image/png', 'image/jpg'] -if args.contentType is None: - contentType = magic.detect_from_filename(args.filePath).mime_type +if args.content_type is None: + args.content_type = magic.detect_from_filename(args.file).mime_type -if args.contentType not in allowedContentType: - print("Allowed contentTypes "+str(" ".joing(allowedContentType)) - exit() +if args.content_type not in allowedContentType: + print("Allowed contentTypes "+str(" ".join(allowedContentType))) -update_profile_picture(self, args.communityKey, args.userId, args.filePath, args.contentType) \ No newline at end of file +igloo.update_profile_picture(args.user_id, args.file, args.content_type) \ No newline at end of file