def getSchemaName(token=""):
    if (token == ""):
        userToken = LoginPortal.getToken()
    else:
        userToken = token
    keystoneUserId = LoginPortal.getKeystoneUserWithToken(userToken).id
    usersUrl = Config.CasJobsRESTUri + "/users/" + keystoneUserId
    headers = {'X-Auth-Token': userToken, 'Content-Type': 'application/json'}
    getResponse = requests.get(usersUrl, headers=headers)
    jsonResponse = json.loads(getResponse.content.decode())
    return "wsid_" + str(jsonResponse["WebServicesId"])
    def test_LoginPortal_allMethods(self):

        newToken1 = "myToken1"
        newToken2 = "myToken2"

        token1 = LoginPortal.login(Authentication_loginName,
                                   Authentication_loginPassword)
        token2 = LoginPortal.getToken()
        token3 = LoginPortal.getKeystoneToken()
        user = LoginPortal.getKeystoneUserWithToken(token1)
        iden = LoginPortal.identArgIdentifier()

        self.assertEqual(iden, "--ident=")

        self.assertNotEqual(token1, "")
        self.assertIsNot(token1, None)
        self.assertEqual(token1, token2)
        self.assertEqual(token1, token3)

        self.assertIsNot(user.userName, None)
        self.assertNotEqual(user.userName, "")
        self.assertIsNot(user.id, None)
        self.assertNotEqual(user.id, "")

        LoginPortal.setKeystoneToken(newToken1)
        self.assertEqual(newToken1, LoginPortal.getKeystoneToken())
def sqlSearch(sql, limit="10", token=""):
    """Runs a SQL query against the SDSS database. If a token is supplied, then it will run on behalf of the token's user.
    'sql': a string containing the sql query
    'limit': maximum number of rows in the result table (string). If set to '0', then the function will return all rows.
    'token': Sciserver's authentication token for the user.
    """
    url = Config.SkyServerWSurl + '/' + Config.DataRelease + '/SearchTools/SqlSearch?'
    url = url + 'format=csv&'
    url = url + 'cmd=' + sql + '&'
    url = url + 'limit=' + limit + '&'
    url = urllib.quote_plus(url)
    acceptHeader = "text/plain"
    headers = {'Content-Type': 'application/json', 'Accept': acceptHeader}

    if (token != ""):
        headers['X-Auth-Token'] = token
    else:
        Token = ""
        try:
            Token = LoginPortal.getToken()
        except:
            Token = ""
        if(Token != ""):
            headers['X-Auth-Token'] = Token

    try:
        response = requests.get(url,headers=headers)
        if response.status_code != 200:
            return {"Error":{"ErrorCode":response.status_code,"Message":response.content.decode()}}

        r=response.content.decode();
        return pandas.read_csv(StringIO(r), comment='#')
    except requests.exceptions.RequestException as e:
        return e
def getTables(context="MyDB"):
    """Returns all the tables from the current user in the given context.
    The result is a json object with format [{"Date":seconds,"Name":"TableName","Rows":int,"Size",int},..]"""

    TablesUrl = Config.CasJobsRESTUri + "/contexts/" + context + "/Tables"

    headers={'X-Auth-Token': LoginPortal.getToken(), 'Content-Type': 'application/json'}

    getResponse = requests.get(TablesUrl,headers=headers)

    jsonResponse = json.loads(getResponse.content.decode())

    return jsonResponse
def getJobStatus(jobid):
    """Gets a casjobs job status.
    Returns the dict object (https://docs.python.org/3.4/library/stdtypes.html#dict) coresponding to the json received from casjobs."""

    QueryUrl = Config.CasJobsRESTUri + "/jobs/" + str(jobid)

    headers={'X-Auth-Token': LoginPortal.getToken(),'Content-Type': 'application/json'}

    try:
        postResponse =requests.get(QueryUrl,headers=headers)

        return json.loads(postResponse.content.decode())
    except requests.exceptions.RequestException as e:
        return e.code
def upload(path, data, token=""):
    if (token == ""):
        userToken = LoginPortal.getToken()
    else:
        userToken = token;

    url = Config.SciDriveHost + '/vospace-2.0/1/files_put/dropbox/' + path
    data = data
    headers = {'X-Auth-Token': userToken}
    try:
        res = requests.put(url, data=data, headers=headers)
        print(res.content.decode())
    except requests.exceptions.RequestException as error:
        print(error, error.read().decode())
        raise
def executeQuery(queryString, context="MyDB", acceptHeader="application/json+array", token="", format="pandas"):
    """Executes a casjob query.  If a token is supplied then it will execute on behalf of the token's user.
    format parameter specifies the return type:
    'pandas': pandas.DataFrame
    'csv': a csv string
    'readable' : a StringIO, readable object wrapping a csv string that can be passed into pandas.read_csv for example.
    'json': a dict created from a JSON string with the Query, a Result consisting of a Columns and a Data field.
    """

    if (format == "pandas") or (format =="json"):
        acceptHeader="application/json+array"
    elif (format == "csv") or (format == "readable"):
        acceptHeader = "text/plain"
    else:
        return {"Error":{"Message":"Illegal format specification '"+format+"'"}}

    QueryUrl = Config.CasJobsRESTUri + "/contexts/" + context + "/query"

    query = {"Query": queryString}

    data = json.dumps(query).encode()

    headers = {'Content-Type': 'application/json', 'Accept': acceptHeader}
    if (token == ""):
        headers['X-Auth-Token'] = LoginPortal.getToken()
    else:
        headers['X-Auth-Token'] = token


    try:
        postResponse = requests.post(QueryUrl,data=data,headers=headers)
        if postResponse.status_code != 200:
            return {"Error":{"ErrorCode":postResponse.status_code,"Message":postResponse.content.decode()}}
        r=postResponse.content.decode()
        if (format == "readable"):
            return StringIO(r)
        elif format == "pandas":
            r=json.loads(r)
            return pandas.DataFrame(r['Result'][0]['Data'],columns=r['Result'][0]['Columns'])
        elif format == "csv":
            return r
        elif format == "json":
            return json.loads(r)
        else: # should not occur
            return {"Error":{"Message":"Illegal format specification '"+format+"'"}}
    except requests.exceptions.RequestException as e:
        return e
def createContainer(path, token=""):
    if (token == ""):
        userToken = LoginPortal.getToken()
    else:
        userToken = token;

    containerBody = ('<vos:node xmlns:xsi="http://www.w3.org/2001/thisSchema-instance" '
                     'xsi:type="vos:ContainerNode" xmlns:vos="http://www.ivoa.net/xml/VOSpace/v2.0" '
                     'uri="vos://' + Config.SciDriveHost + '!vospace/' + path + '">'
                                                                                '<vos:properties/><vos:accepts/><vos:provides/><vos:capabilities/>'
                                                                                '</vos:node>')
    url = Config.SciDriveHost + '/vospace-2.0/nodes/' + path
    data = str.encode(containerBody)
    headers = {'X-Auth-Token': userToken, 'Content-Type': 'application/xml'}
    try:
        res = requests.put(url, data=data, headers=headers)
    except requests.exceptions.RequestException as error:
        print(error, error.read().decode())
def rectangularSearch(min_ra, max_ra, min_dec, max_dec, coordType="equatorial", whichPhotometry="optical", limit="10", token=""):
    """Runs a query in the SDSS database that searches for all objects within a certain rectangular box defined on the the sky, and retrieves the result table as a Panda's dataframe.
    'min_ra': Minimum value of Right Ascension coordinate that defines the box boundaries on the sky.
    'max_ra': Maximum value of Right Ascension coordinate that defines the box boundaries on the sky.
    'min_dec': Minimum value of Declination coordinate that defines the box boundaries on the sky.
    'max_dec': Maximum value of Declination coordinate that defines the box boundaries on the sky.
    'coordType': Type of celestial coordinate system. Can be set to "equatorial" or "galactic".
    'whichPhotometry': Type of retrieved data. Can be set to "optical" or "infrared".
    'limit': Maximum number of rows in the result table (string). If set to "0", then the function will return all rows.
    'token': Sciserver's authentication token for the user.
    """
    url = Config.SkyServerWSurl + '/' + Config.DataRelease + '/SearchTools/RectangularSearch?'
    url = url + 'format=csv&'
    url = url + 'min_ra=' + str(min_ra) + '&'
    url = url + 'max_ra=' + str(max_ra) + '&'
    url = url + 'min_dec=' + str(min_dec) + '&'
    url = url + 'max_dec=' + str(max_dec) + '&'
    url = url + 'coordType=' + coordType + '&'
    url = url + 'whichPhotometry=' + whichPhotometry + '&'
    url = url + 'limit=' + limit + '&'
    #url = urllib.quote_plus(url)
    acceptHeader = "text/plain"
    headers = {'Content-Type': 'application/json', 'Accept': acceptHeader}

    if (token != ""):
        headers['X-Auth-Token'] = token
    else:
        Token = ""
        try:
            Token = LoginPortal.getToken()
        except:
            Token = ""
        if(Token != ""):
            headers['X-Auth-Token'] = Token

    try:
        response = requests.get(url,headers=headers)
        if response.status_code != 200:
            return {"Error":{"ErrorCode":response.status_code,"Message":response.content.decode()}}

        r=response.content.decode();
        return pandas.read_csv(StringIO(r), comment='#')
    except requests.exceptions.RequestException as e:
        return e
def publicUrl(path, token):
    """
    retrieve public URL for file identified by path
    """
    if (token == ""):
        userToken = LoginPortal.getToken()
    else:
        userToken = token;

    url = Config.SciDriveHost + '/vospace-2.0/1/media/sandbox/' + path
    headers={'X-Auth-Token': userToken}
    try:
        res = requests.get(url, headers=headers)
    except requests.exceptions.RequestException as error:
        print(error, error.read().decode())
        raise

    jsonRes = json.loads(res.content.decode())
    fileUrl = jsonRes["url"]
    return fileUrl
def uploadCVSDataToTable(CVSdata, tableName, context="MyDB", token=""):
    """Uploads  cvs data into casjobs.  data should support the buffered interface (it should have a read() method).
    https://docs.python.org/3/library/urllib.request.html
    Returns the output from casjobs in string form."""

    print("Uploading ", sys.getsizeof(CVSdata), "bytes...")
    tablesUrl = Config.CasJobsRESTUri + "/contexts/" + context + "/Tables/" + tableName

    headers={}
    if (token == ""):
        headers['X-Auth-Token']= LoginPortal.getToken()
    else:
        headers['X-Auth-Token']= token

    try:
        postResponse = requests.post(tablesUrl,data=CVSdata,headers=headers)
        print("uploadCVSDataFrameToTable POST response: ", postResponse.status_code, postResponse.reason)

        return postResponse.content.decode()
    except Exception as error:
        print("There was a problem uploading the data. Exception message: ", error.read())
def getJpegImgCutout(ra, dec, scale=0.7, width=512, height=512, token = ""):
    """Gets a rectangular image cutout from a region of the sky in SDSS, centered at (ra,dec).
    'ra': Right Ascension of the image's center.
    'dec': Declination of the image's center.
    'scale': scale of the image, measured in [arcsec/pix]
    'width': Right Ascension of the image's center.
    'ra': Right Ascension of the image's center.
    'height': Height of the image, measured in [pix].
    'token': Sciserver's authentication token for the user.
    """
    url = Config.SkyServerWSurl + '/' + Config.DataRelease + '/ImgCutout/getjpeg?'
    url = url + 'ra=' + str(ra) + '&'
    url = url + 'dec=' + str(dec) + '&'
    url = url + 'scale=' + str(scale) + '&'
    url = url + 'width=' + str(width) + '&'
    url = url + 'height=' + str(height) + '&'
    #url = urllib.quote_plus(url)
    acceptHeader = "text/plain"
    headers = {'Content-Type': 'application/json', 'Accept': acceptHeader}

    if (token != ""):
        headers['X-Auth-Token'] = token
    else:
        Token = ""
        try:
            Token = LoginPortal.getToken()
        except:
            Token = ""
        if(Token != ""):
            headers['X-Auth-Token'] = Token

    try:
        response = requests.get(url,headers=headers)
        if response.status_code != 200:
            return {"Error":{"ErrorCode":response.status_code,"Message":response.content.decode()}}

        return skimage.io.imread( BytesIO( response.content  ) )
    except requests.exceptions.RequestException as e:
        return e
def submitJob(queryString, context="MyDB", acceptHeader="text/plain", token=""):
    """Submits a query to the casjobs queue.  If a token is supplied then it will execute on behalf of the token's user.
    Returns the casjobs jobID (int)."""

    QueryUrl = Config.CasJobsRESTUri + "/contexts/" + context + "/jobs"

    query = {"Query": queryString}

    data = json.dumps(query).encode()

    headers = {'Content-Type': 'application/json', 'Accept': acceptHeader}
    if (token == ""):
        headers['X-Auth-Token']= LoginPortal.getToken()
    else:
        headers['X-Auth-Token']=  token

    try:
        putResponse = requests.put(QueryUrl,data=data,headers=headers)

        return int(putResponse.content.decode())
    except requests.exceptions.RequestException as e:
        return e
def testTurbulenceToken():
    token = LoginPortal.getToken(py3.Turbulence.Config.CasJobsTurbulenceUser, py3.Turbulence.Config.CasJobsTurbulencePassword)
    return token
def testLoginPortal(user,password):
    token= LoginPortal.login(user,password)
    print("token=",token)
    user= LoginPortal.getKeystoneUserWithToken(token)
    print("user=",user.id,user.userName)
    return token
# *******************************************************************************************************
# LoginPortal section:
# *******************************************************************************************************


# In[ ]:

#help(LoginPortal)


# In[ ]:

#logging in and getting current token from different ways

token1 = LoginPortal.login(Authentication_loginName, Authentication_loginPassword);
token2 = LoginPortal.getToken()
token3 = LoginPortal.getKeystoneToken()
print("token1=" + token1)#
print("token2=" + token2)#
print("token3=" + token3)#


# In[ ]:

#getting curent user info

user = LoginPortal.getKeystoneUserWithToken(token1)
print("userName="******"id=" + user.id)#
iden = LoginPortal.identArgIdentifier()