Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connector: Google Suite #900

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions swirl/connectors/google_suite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from sys import path
from os import environ

import django

from swirl.utils import swirl_setdir
path.append(swirl_setdir()) # path to settings.py file
environ.setdefault('DJANGO_SETTINGS_MODULE', 'swirl_server.settings')
django.setup()

from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)

from swirl.connectors.utils import get_search_obj, get_mappings_dict
from swirl.connectors.requestsget import RequestsGet
from swirl.connectors.requestspost import RequestsPost


########################################
########################################

DEFAULT_DATESORT_X = "createdDateTime desc"


class GoogleSuite(RequestsGet):
"""
Connector to Google Suite
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)

# Get the Google Suite API endpoint
self.api_endpoint = "https://www.googleapis.com/drive/v3"

def get_access_token(self):
"""
Get an OAuth2 access token for Google Suite
"""

# Replace the following values with your own OAuth2 credentials
CLIENT_ID = self.config["client_id"]
CLIENT_SECRET = self.config["client_secret"]
REDIRECT_URI = self.config["redirect_uri"]

url = "https://accounts.google.com/o/oauth2/token"
payload = {
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"grant_type": "authorization_code",
"code": "",
}
headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(url, data=payload, headers=headers)
return response.json()["access_token"]

def search(self, query):
"""
Search Google Suite
"""

# Get an access token
access_token = self.get_access_token()

# Search Google Suite
url = self.api_endpoint + "/search"
headers = {
"Authorization": f"Bearer {access_token}",
}
payload = {
"query": query,
"fields": "files(id, name, mimeType, createdTime, modifiedTime)",
}
response = requests.post(url, json=payload, headers=headers)

# Return the results
return response.json()

def get_file_by_id(self, file_id):
"""
Get a file by ID
"""

# Get an access token
access_token = self.get_access_token()

# Get the file
url = self.api_endpoint + f"/files/{file_id}"
headers = {
"Authorization": f"Bearer {access_token}",
}
response = requests.get(url, headers=headers)

# Return the file
return response.json()

def get_file_content(self, file_id):
"""
Get the content of a file
"""

# Get an access token
access_token = self.get_access_token()

# Get the file content
url = self.api_endpoint + f"/files/{file_id}/?alt=media"
headers = {
"Authorization": f"Bearer {access_token}",
}
response = requests.get(url, headers=headers)

# Return the file content
return response.content

def get_mappings(self):
"""
Get the mappings for Google Suite
"""

return get_mappings_dict(GOOGLE_SUITE_MAPPINGS)