from samples.Logic import TokenHandler
from samples.Logic import ForceBridgeConnector
import urllib3

urllib3.disable_warnings()

user = "GitHub"
password = "<PASSWORD>"
urlToken = "https://forcebridgehackathon.force.eco:25443/ffauth/oauth2.0/accessToken?client_id=" + user + "&client_secret=" + password + "&grant_type=client_credentials&scope=read%20write"
urlToBridgeAPI = "https://forcebridgehackathon.force.eco:24443/ffwebservices/api/v3/"
documentID = "F318273D75604B9EA1E255AAF522A477"
fileName = "Test.txt"

print("Welcome to uploading a file example!\n")
print("Determine token ...\n")

tokenhandler = TokenHandler.TokenHandler
tokeninit = tokenhandler(user, password, urlToken)
token = tokeninit.GetAccessToken()
accessToken = token['access_token']

print("Token: " + accessToken + "\n")
print("Upload ....\n")

files=[
  ('file', ('Test.txt', open('Test.txt', 'rb'), 'text/plain'))
]

Connector = ForceBridgeConnector.FORCEBridgeConnector
BridgeConnector = Connector(urlToBridgeAPI, accessToken)
header = BridgeConnector.Tokenheader(accessToken)
upload = BridgeConnector.PutFileinDocument(documentID, fileName, header, files)

print(upload, "\n")

print("Editor {} uploads file {} into documentID {} with version {} ".format(upload['properties']['editor'], upload['properties']['name'], upload['properties']['id'], upload['properties']['version']))





