Skip to content

Commit

Permalink
ic
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamingregory committed Nov 14, 2017
0 parents commit 4c2b5e7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Airflow Plugin - Box
7 changes: 7 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from airflow.plugins_manager import AirflowPlugin
from BoxPlugin.hooks.box_hook import BoxHook

class BoxPlugin(AirflowPlugin):
name = "box_plugin"
operators = []
hooks = [BoxHook]
Empty file added hooks/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions hooks/box_hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from airflow.models import Connection
from airflow.utils.db import provide_session
from airflow.hooks.base_hook import BaseHook
from boxsdk import OAuth2
from boxsdk import Client


class BoxHook(BaseHook):
"""
Wrap around the Box Python SDK
"""
def __init__(self, box_conn_id):
self.box_conn_id = self.get_connection(box_conn_id)
self.access_token = self.box_conn_id.login
self.refresh_token = self.box_conn_id.schema
self.CLIENT_ID = self.box_conn_id.extra_dejson.get('client_id')
self.CLIENT_SECRET = self.box_conn_id.extra_dejson.get('client_secret')

def get_conn(self):
@provide_session
def store_tokens(access_token, refresh_token, session=None):
(session
.query(Connection)
.filter(Connection.conn_id == "{}".format(self.box_conn_id))
.update({Connection.login: access_token,
Connection.schema: refresh_token}))

oauth = OAuth2(
client_id=self.CLIENT_ID,
client_secret=self.CLIENT_SECRET,
access_token=self.access_token,
refresh_token=self.refresh_token,
store_tokens=store_tokens,
)
client = Client(oauth)
return client

def upload_file(self,
folder_id=None,
file_path=None,
file_name=None,
preflight_check=False):
client = self.get_conn()
client.folder(folder_id=folder_id).upload(
file_path=file_path,
file_name=file_name,
preflight_check=True)

0 comments on commit 4c2b5e7

Please sign in to comment.