Example #1
0
    def __init__(self, portalInfo):
        self.logger = logging.getLogger('authDataLogger')

        self.portalInfo = portalInfo

        #server credentials
        self.tokenCred = portalInfo['tokenCred']
        self.generateTokenUrl = portalInfo['generateTokenUrl']
        self.serverTokenUrl = portalInfo['serverTokenUrl']

        #feature service URLs
        self.eventLocationsUrl = portalInfo[
            'baseUrl'] + '/Event_Locations/FeatureServer/0'
        self.eventBoundariesUrl = portalInfo[
            'baseUrl'] + '/Event_Boundaries/FeatureServer/0'
        self.wildfireBoundariesUrl = portalInfo[
            'baseUrl'] + '/Wildfire_Boundaries/FeatureServer/0'
        self.hurricaneBoundariesUrl = portalInfo[
            'baseUrl'] + '/Hurricane_Boundaries/FeatureServer/0'

        #dashboard update URL
        self.dashboardUpdateUrl = portalInfo['dashboardUrl']

        #obtain an auth token for portal
        self.logger.info('get portal token')
        if portalInfo['useNegotiateAuth'] == True:
            response = requests.post(self.generateTokenUrl,
                                     data=self.tokenCred,
                                     headers=self.tokenHeaders,
                                     verify=False,
                                     auth=HttpNegotiateAuth())
        else:
            response = requests.post(self.generateTokenUrl,
                                     data=self.tokenCred,
                                     headers=self.tokenHeaders)
        portalToken = json.loads(response.content)
        self.logger.info(portalToken)

        #get a portal server token
        self.logger.info('get server token')
        if portalInfo['useNegotiateAuth'] == False:
            response = requests.get(self.serverTokenUrl + portalToken['token'],
                                    headers=self.tokenHeaders)
        else:
            response = requests.get(self.serverTokenUrl + portalToken['token'],
                                    headers=self.tokenHeaders,
                                    verify=False,
                                    auth=HttpNegotiateAuth())

        self.serverToken = json.loads(response.content)
        serverTokenParam = 'token=' + self.serverToken['token']
        self.logger.info(serverTokenParam)

        self.portalQuery = '/query?where=eventid%3D%27{eventId}%27&&outFields=*&f=pjson&' + serverTokenParam
        self.addFeatures = '/addFeatures?' + serverTokenParam
        self.deleteFeatures = '/deleteFeatures?' + serverTokenParam
        self.updateDashboard = '/update?' + serverTokenParam
Example #2
0
 def _build_authorization_token_cam(user: str = None,
                                    password: str = None,
                                    namespace: str = None,
                                    gateway: str = None,
                                    verify: bool = False) -> str:
     if gateway:
         try:
             HttpNegotiateAuth
         except NameError:
             raise RuntimeError(
                 "SSO failed due to missing dependency requests_negotiate_sspi.HttpNegotiateAuth. "
                 "SSO only supported for Windows")
         response = requests.get(gateway,
                                 auth=HttpNegotiateAuth(),
                                 verify=verify,
                                 params={"CAMNamespace": namespace})
         if not response.status_code == 200:
             raise RuntimeError(
                 "Failed to authenticate through CAM. Expected status_code 200, received status_code: "
                 + str(response.status_code))
         elif 'cam_passport' not in response.cookies:
             raise RuntimeError(
                 "Failed to authenticate through CAM. HTTP response does not contain 'cam_passport' cookie"
             )
         else:
             return 'CAMPassport ' + response.cookies['cam_passport']
     else:
         return 'CAMNamespace ' + b64encode(
             str.encode("{}:{}:{}".format(user, password,
                                          namespace))).decode("ascii")
def download_pbix(ItemID, Path):
    """Download locally the given Power BI report
	Parameters
	----------
	ItemID : string
		Power BI report identifier
        
	Path : string
		Local folder where to extract the Power BI report
	Returns
	-------
	Local folder where to extract the Power BI report, Zipped Power BI report
	"""
    url = POWER_BI_API + 'catalogitems(' + ItemID + ')/Content/$value'
    # Download query
    r = requests.get(url, allow_redirects=True, auth=HttpNegotiateAuth())
    # create the report destination folder
    os.makedirs(REPORTS_SUBFOLDER_NAME + Path.rsplit('/', 1)[0], exist_ok=True)
    # Delete any previously imported report
    print(('del "./' + REPORTS_SUBFOLDER_NAME + Path +
           '/*"').replace("/", "\\") + " /F /Q")
    subprocess.check_output(
        ('if exist ./' + REPORTS_SUBFOLDER_NAME + Path + ' del "./' +
         REPORTS_SUBFOLDER_NAME + Path + '/*" 2>nul').replace("/", "\\") +
        " /F /Q",
        shell=True,
        encoding="437")

    open(REPORTS_SUBFOLDER_NAME + Path + '.zip', 'wb').write(r.content)

    return str(REPORTS_SUBFOLDER_NAME + Path), str(REPORTS_SUBFOLDER_NAME +
                                                   Path + '.zip')
Example #4
0
    def _start_session(self,
                       user: str,
                       password: str,
                       decode_b64: bool = False,
                       namespace: str = None,
                       gateway: str = None,
                       integrated_login: bool = None,
                       impersonate: str = None):
        """ perform a simple GET request (Ask for the TM1 Version) to start a session
        """
        # Authorization with integrated_login
        if integrated_login:
            self._s.auth = HttpNegotiateAuth()

        # Authorization [Basic, CAM] through Headers
        else:
            token = self._build_authorization_token(
                user,
                self.b64_decode_password(password) if decode_b64 else password,
                namespace, gateway, self._verify)
            self.add_http_header('Authorization', token)

        url = '/api/v1/Configuration/ProductVersion/$value'
        try:
            additional_headers = dict()
            if impersonate:
                additional_headers["TM1-Impersonate"] = impersonate

            response = self.GET(url=url, headers=additional_headers)
            self._version = response.text
        finally:
            # After we have session cookie, drop the Authorization Header
            self.remove_http_header('Authorization')
Example #5
0
    def request_client(self, url, params, method):
        Token = self.if_refresh_token(start_time, token)
        # get or post for further purpose
        if method == 'get':
            # put the token in headers
            if Token is not None:
                response = requests.get(url,
                                        params=params,
                                        headers={
                                            'Content-Type': 'application/json',
                                            "Authorization": "Bearer " + Token
                                        })

            # if access_token is None, try windows id to request
            else:
                try:
                    response = requests.get(
                        url,
                        params=params,
                        headers={'Content-Type': 'application/json'},
                        auth=HttpNegotiateAuth())
                except Exception as e:
                    print(str(e))

        elif method == 'post':
            if Token is not None:
                response = requests.post(url,
                                         data=json.dumps(params),
                                         headers={
                                             'Content-Type':
                                             'application/json',
                                             "Authorization": "Bearer " + Token
                                         })

            else:
                try:
                    response = requests.post(
                        url,
                        data=json.dumps(params),
                        headers={'Content-Type': 'application/json'},
                        auth=HttpNegotiateAuth())

                except Exception as e:
                    print(str(e))

        return json.loads(response.text)
    def __init__(self, host: str, port: int = None, scheme: str = 'https',
                 domain: str = None, username: str = None, password: str = None):
        super().__init__(host=host, port=port, scheme=scheme)

        if domain and username and password:
            _logger.debug('__SET NTLM AUTH')
            self._auth = HttpNtlmAuth(username=f'{domain}\\{username}', password=password)
        else:
            _logger.debug('__SET NTLM SSPI AUTH')
            self._auth = HttpNegotiateAuth()
def take_existed_value(config_code_no):
    # get data from my own api
    # or write your own way to get the lattest value for the data in Sql
    url = "http://..."
    params = {"code": config_code_no, "dataList": [{"fDate": "", "value": ""}]}
    t_json = requests.get(url,
                          params=params,
                          headers={'Content-Type': 'application/json'},
                          auth=HttpNegotiateAuth())
    data_2 = json.loads(t_json.text)
    return data_2
def updated_Sql(code, emp):
    url = "http://..."
    salesDate = json.loads(connect_tws(emp))["data"]["time"]
    salesMonth = json.loads(connect_tws(emp))["data"]["datasets"]["saleMonth"]
    # unit adjust
    new_Value = [i * 1000 for i in salesMonth][-1]
    f_Date = timeStamp_2_datatime(salesDate)[-1]
    dic = {"fDate": f_Date, "value": new_Value}

    # if there were existing data of this code in Sql, choose the lattest value to compare
    # lattest value in Sql
    t = take_existed_value(code)
    t_value = t[0]["Value"]

    outList = []
    for i in range(0, len(t)):
        dic2 = {"fDate": t[i]["FDate"], "value": t[i]["Value"]}
        outList.append(dic2)

    # Compare the value if there is new value in
    if salesMonth[-2] * 1000 == int(t_value):
        # if the second value got this time equals the lattest value in Sql, then start updating,else pass
        outList.append(dic)
        json_output = {"code": code, "dataList": outList}
        data_json = json.dumps(json_output)
        try:

            r_json = requests.post(
                url,
                data_json,
                headers={'Content-Type': 'application/json'},
                auth=HttpNegotiateAuth())
            print(code)
            print(f_Date[-1])
            print(r_json)
            print(r_json.text)
            print(r_json.status_code)
            print(r_json.raise_for_status())
            if r_json.status_code == 200:
                print("Insert Successfully!")
                print(
                    '-----------------------------------------------------------------------------------'
                )

        except:
            pass

    else:
        pass
Example #9
0
    def updateDashboardTitle(self, appid, eventId):
        dashboardUrl = string.replace(self.dashboardUpdateUrl, '{dashboard}',
                                      appid) + self.updateDashboard

        #create form data for posting updated dashboard title
        urlTuple = {
            'title': self.indexedEventJson['data']['title'],
            'f': 'json',
            'clearEmptyFields': True,
            'id': appid,
            'token': self.serverToken
        }
        formData = urllib.urlencode(urlTuple)

        #post updated dashboard title to portal
        if self.portalInfo['useNegotiateAuth'] == True:
            portalResponse = requests.post(dashboardUrl,
                                           data=formData,
                                           headers=self.tokenHeaders,
                                           verify=False,
                                           auth=HttpNegotiateAuth())
        else:
            portalResponse = requests.post(dashboardUrl,
                                           data=formData,
                                           headers=self.tokenHeaders)

        #review server POST results
        if portalResponse.ok:
            responseJSON = json.loads(portalResponse.content)
            if 'success' in responseJSON:
                success = responseJSON['success']
                if success == True:
                    self.logger.info('Dashboard updated for event: ' + eventId)
                else:
                    self.logger.warn('Unable to update dashboard for event: ' +
                                     eventId)
            elif 'error' in responseJSON:
                self.logger.error(
                    'Server error occurred when updating dashboard for event: '
                    + eventId)
            else:
                self.logger.error(
                    'Unknown error occurred when updating dashboard for event: '
                    + eventId)
        else:
            self.logger.error(
                'Server error (' + portalResponse.status_code +
                ') occurred while updating dashboard title for event: ' +
                eventId)
Example #10
0
def get_credentials(args):
    session = requests.Session()
    ##TODO default to wincertstore
    session.verify = args.sslverify
    path = '/' + args.account
    b64_account = base64.b64encode(path.encode('ascii')).decode('utf-8')
    saml_response = session.get(args
                                .endpoint, auth=HttpNegotiateAuth(), cookies={'dp-org-uri': b64_account})
    saml_assertion = get_saml_assertion(saml_response.text)
    saml_role = get_saml_role(saml_assertion, args.role)
    role_arn = saml_role.split(',')[0]
    principal_arn = saml_role.split(',')[1]
    client = boto3.client('sts', region_name=args.region, config=Config(signature_version=UNSIGNED))
    creds = client.assume_role_with_saml(RoleArn=role_arn, PrincipalArn=principal_arn, SAMLAssertion=saml_assertion)[
        'Credentials']
    creds['Expiration'] = creds['Expiration'].isoformat()
    return creds
Example #11
0
 def _build_authorization_token(user, password, namespace=None, gateway=None, **kwargs):
     """ Build the Authorization Header for CAM and Native Security
     """
     if namespace:
         if gateway:
             response = requests.get(gateway, auth=HttpNegotiateAuth())
             if not response.status_code == 200:
                 raise RuntimeError(
                     "Failed to authenticate through CAM. Expected status_code 200, received status_code: "
                     + str(response.status_code))
             elif 'cam_passport' not in response.cookies:
                 raise RuntimeError(
                     "Failed to authenticate through CAM. HTTP response does not contain 'cam_passport' cookie")
             else:
                 token = 'CAMPassport ' + response.cookies['cam_passport']
         else:
             token = 'CAMNamespace ' + b64encode(
                 str.encode("{}:{}:{}".format(user, password, namespace))).decode("ascii")
     else:
         token = 'Basic ' + b64encode(
             str.encode("{}:{}".format(user, password))).decode("ascii")
     return token
Example #12
0
    def connect(self):
        """ Establishes the connection to Elite using local credentials """
        wsdl = f"{self.protocol}://{self.wapi}:{self.port}/{self.instance}/WebUI/Transactionservice.asmx?wsdl"

        if self.domain and self.username and self.password:
            session = Session()
            session.auth = HttpNtlmAuth(f'{self.domain}\\{self.username}',
                                        self.password)
            try:
                self.client = Client(wsdl,
                                     transport=Transport(session=session))
            except:
                raise Exception(
                    "Connection to 3E has been severed or could not be established. "
                    "Check the credentials and network connection.")

        elif os.name == "nt":
            session = Session()
            session.auth = HttpNegotiateAuth()
            self.client = Client(wsdl, transport=Transport(session=session))
        else:
            raise Exception(
                "You must be on Windows in a Windows network, or provide username, password and domain"
            )
try:
    import cookielib
except ImportError:
    # python 3
    import http.cookiejar as cookielib

_auth_provider = None
_headers = {'Accept-Language': 'en'}

# Support for Kerberos SSO on Windows via requests_negotiate_sspi
# also requires tricking the server into thinking we're using IE
# so that it servers up a redirect to the IWA page.
try:
    from requests_negotiate_sspi import HttpNegotiateAuth
    _auth_provider = HttpNegotiateAuth()
    _headers[
        'User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
except ImportError:
    pass

# The initial URL that starts the authentication process.
_IDP_ENTRY_URL = 'https://{}/adfs/ls/IdpInitiatedSignOn.aspx?loginToRp={}'


def fetch_html_encoded_roles(adfs_host,
                             adfs_cookie_location,
                             ssl_verification_enabled,
                             provider_id,
                             username=None,
                             password=None):
Example #14
0
def getSPcontext(url,username,digest):
    ctxHeaders = {'Content-Type': 'application/json;odata=verbose', 'Accept' : "application/json;odata=verbose", "X-RequestDigest":digest }
    payload = {'logonName': username, }
    context = requests.post(userContextUrl,data=json.dumps(payload),headers=ctxHeaders ,auth=HttpNegotiateAuth(),verify=False)
    return context
Example #15
0
def getDigestRequest(url):
    res = requests.post(url,headers=shortHeaders ,auth=HttpNegotiateAuth(),verify=False)
    #digest = json.loads(res.content.decode('utf-8')).find("{http://schemas.microsoft.com/ado/2007/08/dataservices}FormDigestValue").text
    digest = json.loads(res.content.decode('utf-8'))
    return digest["d"]["GetContextWebInformation"]["FormDigestValue"]
Example #16
0
    cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
        "Server=" + server +";"
        "Database="+ dbname + ";"
        "Trusted_Connection=yes;")
    cursor = cnxn.cursor()
    cursor.execute(query)
    for row in cursor:
        print('row = %r' % (row,))

def getDigestRequest(url):
    res = requests.post(url,headers=shortHeaders ,auth=HttpNegotiateAuth(),verify=False)
    #digest = json.loads(res.content.decode('utf-8')).find("{http://schemas.microsoft.com/ado/2007/08/dataservices}FormDigestValue").text
    digest = json.loads(res.content.decode('utf-8'))
    return digest["d"]["GetContextWebInformation"]["FormDigestValue"]

def getSPcontext(url,username,digest):
    ctxHeaders = {'Content-Type': 'application/json;odata=verbose', 'Accept' : "application/json;odata=verbose", "X-RequestDigest":digest }
    payload = {'logonName': username, }
    context = requests.post(userContextUrl,data=json.dumps(payload),headers=ctxHeaders ,auth=HttpNegotiateAuth(),verify=False)
    return context


    
requestDigest = getDigestRequest(digetsURL)
ensureUser = getSPcontext(userContextUrl,currentUsername,requestDigest)

listItemResponse =  requests.get(listUrl,headers=shortHeaders ,auth=HttpNegotiateAuth(),verify=False)


print(json.loads(ensureUser.content.decode('utf-8'))["d"]["Email"])
Example #17
0
    def upsertWildfireEventFeatures(self, event, report, perimeter):
        eventId = self.getEventId(event)
        appid = None
        wildfireQuery = self.getPortalQuery(eventId)

        if self.portalInfo['useNegotiateAuth'] == True:
            temp = requests.get(self.wildfireBoundariesUrl + wildfireQuery,
                                verify=False,
                                auth=HttpNegotiateAuth())
        else:
            temp = requests.get(self.wildfireBoundariesUrl + wildfireQuery)
        wildfireBoundariesFs = arcpy.FeatureSet()
        wildfireBoundariesFs = arcpy.AsShape(temp.content, True)

        if wildfireBoundariesFs.JSON is not None:
            self.logger.info(
                'Successfully loaded boundary data from portal for wildfire event: '
                + eventId)
            wildfireBoundariesJson = json.loads(wildfireBoundariesFs.JSON)
        else:
            self.logger.error('Unable to load boundary data from portal!')
            return False

        arcpy.Delete_management('in_memory')

        #form a polygon feature class to encompass the new boundary geometry
        new_geometry = perimeter['geometry']
        new_boundary_polygon = arcpy.Polygon(
            arcpy.Array(
                [arcpy.Point(*coords) for coords in new_geometry['rings'][0]]),
            arcpy.SpatialReference(3857))

        if len(wildfireBoundariesJson['features']) > 0:
            #iterate through each of the wildfire boundaries associated with the event; typically there will only be one...
            boundaries = wildfireBoundariesJson['features']
            boundaryCount = 1
            for boundary in boundaries:
                dissolveBoundaryFC = "in_memory\\_wildfireBoundaryDissolved_featureClass_" + str(
                    boundaryCount)
                simplifyBoundaryFC = "in_memory\\_wildfireBoundarySimplified_featureClass_" + str(
                    boundaryCount)
                boundaryCount += 1

                #form a polygon feature class to encompass the old boundary geometry
                old_geometry = boundary['geometry']
                old_boundary_polygon = arcpy.Polygon(
                    arcpy.Array([
                        arcpy.Point(*coords)
                        for coords in old_geometry['rings'][0]
                    ]), arcpy.SpatialReference(3857))

                #dissolve together the old boundary with the new boundary
                arcpy.Dissolve_management(
                    [new_boundary_polygon, old_boundary_polygon],
                    dissolveBoundaryFC)
                dissolveWildfireBoundaryFS = arcpy.FeatureSet()
                dissolveWildfireBoundaryFS.load(dissolveBoundaryFC)

                #simplify the merged result
                arcpy.SimplifyPolygon_cartography(dissolveWildfireBoundaryFS,
                                                  simplifyBoundaryFC,
                                                  'BEND_SIMPLIFY', '5000 Feet')
                simplifiedWildfireBoundaryFS = arcpy.FeatureSet()
                simplifiedWildfireBoundaryFS.load(simplifyBoundaryFC)

                if len(boundaries) > 1:
                    #update the new boundary polygon with the current iteration of dissolved/simplifed boundary data
                    newBoundaryJSON = json.loads(
                        simplifiedWildfireBoundaryFS.JSON)
                    new_geometry = newBoundaryJSON['features'][0]['geometry']
                    new_boundary_polygon = arcpy.Polygon(
                        arcpy.Array([
                            arcpy.Point(*coords)
                            for coords in new_geometry['rings'][0]
                        ]), arcpy.SpatialReference(3857))

                #delete any old boundary data before adding the updated boundary data
                appid_temp = boundary['attributes']['appid']
                if appid_temp is not None:
                    appid = appid_temp
                if 'env' in self.portalInfo:
                    if self.portalInfo['env'] == 'D':
                        fid = boundary['attributes']['fid']
                        deleteResponse = requests.post(
                            self.wildfireBoundariesUrl + self.deleteFeatures,
                            data='where=fid=' + str(fid) +
                            '&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&gdbVersion=&rollbackOnFailure=true&f=json',
                            headers=self.tokenHeaders)
                    elif self.portalInfo['env'] == 'S' or self.portalInfo[
                            'env'] == 'P':
                        fid = boundary['attributes']['objectid1']
                        deleteResponse = requests.post(
                            self.wildfireBoundariesUrl + self.deleteFeatures,
                            verify=False,
                            auth=HttpNegotiateAuth(),
                            data='where=objectid1=' + str(fid) +
                            '&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&gdbVersion=&rollbackOnFailure=true&f=json',
                            headers=self.tokenHeaders)
                    if not deleteResponse.ok:
                        self.logger.warn(
                            'Unable to delete old boundary data for wildfire event: '
                            + eventId)
        else:
            simplifyBoundaryFC = "in_memory\\_wildfireBoundarySimplified_featureClass"
            arcpy.SimplifyPolygon_cartography(new_boundary_polygon,
                                              simplifyBoundaryFC,
                                              'BEND_SIMPLIFY', '5000 Feet')
            simplifiedWildfireBoundaryFS = arcpy.FeatureSet()
            simplifiedWildfireBoundaryFS.load(simplifyBoundaryFC)

        boundaryJSON = json.loads(simplifiedWildfireBoundaryFS.JSON)
        boundary = boundaryJSON['features']

        if len(boundary) > 0:
            #insert new wildfire boundaries data
            wildfireBoundary = WildfireBoundary.WildfireBoundary()
            wildfireBoundary.consume(self.indexedEventJson, appid,
                                     report['attributes'], perimeter,
                                     boundary[0])

            #POST data to portal
            if self.portalInfo['useNegotiateAuth'] == True:
                portalResponse = requests.post(
                    self.wildfireBoundariesUrl + self.addFeatures,
                    data=wildfireBoundary.urlEncode(),
                    headers=self.tokenHeaders,
                    verify=False,
                    auth=HttpNegotiateAuth())
            else:
                portalResponse = requests.post(
                    self.wildfireBoundariesUrl + self.addFeatures,
                    data=wildfireBoundary.urlEncode(),
                    headers=self.tokenHeaders)
            if portalResponse.ok:
                responseJSON = json.loads(portalResponse.content)
                success = responseJSON['addResults'][0]['success']
                if success == True:
                    self.logger.info(
                        'Wildfire boundary data added for event: ' + eventId)
                else:
                    self.logger.warn(
                        'Unable to add Wildfire boundary data for event: ' +
                        eventId)
            else:
                self.logger.error(
                    'Server error (' + portalResponse.status_code +
                    ') occurred while adding Wildfire boundary data for event: '
                    + eventId)

            return True
Example #18
0
    def upsertEventFeatures(self, event, report, perimeter):
        #GET location data from portal (if it exists)
        eventId = self.getEventId(event)
        self.logger.info(
            'Now processing location and boundary data for event: ' + eventId)
        #first perform a GET request to portal to see if the report was previously inserted
        eventQuery = self.getPortalQuery(eventId)
        eventLocationsFs = arcpy.FeatureSet()
        #Populate feature set with data (if any)
        eventLocationsFs.load(self.eventLocationsUrl + eventQuery)
        if eventLocationsFs.JSON is not None:
            self.logger.info(
                'Successfully loaded location data from portal for event: ' +
                eventId)
            portalLocationsJson = json.loads(eventLocationsFs.JSON)
        else:
            self.logger.error('Unable to load location data from portal!')
            return False
        if len(portalLocationsJson['features']) > 0:
            #This feature layer already exists, so this is an update
            #only need to update the boundary data
            #first remove the pre-existing boundary data
            self.logger.info('Begin updating boundary data for event: ' +
                             eventId)
            portalBoundariesFs = arcpy.FeatureSet()
            portalBoundariesFs.load(self.eventBoundariesUrl + eventQuery)
            portalBoundariesJson = json.loads(portalBoundariesFs.JSON)
            appid = None

            if len(portalBoundariesJson['features']) > 0:
                objectId = portalBoundariesJson['features'][0]['attributes'][
                    'objectid']
                appid = portalBoundariesJson['features'][0]['attributes'][
                    'appid']
                if self.portalInfo['useNegotiateAuth'] == True:
                    deleteResponse = requests.post(
                        self.eventBoundariesUrl + self.deleteFeatures,
                        data='objectIds=' + str(objectId) +
                        '&where=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&gdbVersion=&rollbackOnFailure=true&f=json',
                        headers=self.tokenHeaders,
                        verify=False,
                        auth=HttpNegotiateAuth())
                else:
                    deleteResponse = requests.post(
                        self.eventBoundariesUrl + self.deleteFeatures,
                        data='objectIds=' + str(objectId) +
                        '&where=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&gdbVersion=&rollbackOnFailure=true&f=json',
                        headers=self.tokenHeaders)
                if not deleteResponse.ok:
                    self.logger.warn(
                        'Unable to delete old boundary data for event: ' +
                        eventId)

            #insert the updated boundary data
            boundaryFeature = BoundaryFeature.BoundaryFeature()
            boundaryFeature.consume(self.indexedEventJson,
                                    perimeter['geometry'], appid)
            if self.portalInfo['useNegotiateAuth'] == True:
                portalResponse = requests.post(
                    self.eventBoundariesUrl + self.addFeatures,
                    data=boundaryFeature.urlEncode(),
                    headers=self.tokenHeaders,
                    verify=False,
                    auth=HttpNegotiateAuth())
            else:
                portalResponse = requests.post(
                    self.eventBoundariesUrl + self.addFeatures,
                    data=boundaryFeature.urlEncode(),
                    headers=self.tokenHeaders)
            if portalResponse.ok:
                self.logger.info('Boundary data updated for event: ' + eventId)
            else:
                self.logger.warn('Unable to update boundary data for event: ' +
                                 eventId)
        else:
            #This is a new feature layer
            #populate location and boundary objects for POST to portal
            self.logger.info(
                'Begin adding new location and boundary data for event: ' +
                eventId)
            locationFeature = LocationFeature.LocationFeature()
            locationFeature.consume(self.indexedEventJson, report['geometry'])
            boundaryFeature = BoundaryFeature.BoundaryFeature()
            boundaryFeature.consume(self.indexedEventJson,
                                    perimeter['geometry'], None)

            #POST data to portal
            if self.portalInfo['useNegotiateAuth'] == True:
                portalResponse = requests.post(
                    self.eventLocationsUrl + self.addFeatures,
                    data=locationFeature.urlEncode(),
                    headers=self.tokenHeaders,
                    verify=False,
                    auth=HttpNegotiateAuth())
                portalResponse = requests.post(
                    self.eventLocationsUrl + self.addFeatures,
                    data=locationFeature.urlEncode(),
                    headers=self.tokenHeaders)
            if portalResponse.ok:
                self.logger.info('Location data added for event: ' + eventId)
                if self.portalInfo['useNegotiateAuth'] == True:
                    portalResponse = requests.post(
                        self.eventBoundariesUrl + self.addFeatures,
                        data=boundaryFeature.urlEncode(),
                        headers=self.tokenHeaders,
                        verify=False,
                        auth=HttpNegotiateAuth())
                else:
                    portalResponse = requests.post(
                        self.eventBoundariesUrl + self.addFeatures,
                        data=boundaryFeature.urlEncode(),
                        headers=self.tokenHeaders)
                if portalResponse.ok:
                    self.logger.info('Boundary data added for event: ' +
                                     eventId)
                else:
                    self.logger.warn(
                        'Unable to add boundary data for event: ' + eventId)
            else:
                self.logger.warn('Unable to add location data for event: ' +
                                 eventId)
        return True
Example #19
0
    def upsertHurricaneEventFeatures(self, event, position, forecast,
                                     pathBuffer):
        eventId = self.getEventId(event)
        appid = None
        hurricaneQuery = self.getPortalQuery(eventId)

        if self.portalInfo['useNegotiateAuth'] == True:
            temp = requests.get(self.hurricaneBoundariesUrl + hurricaneQuery,
                                verify=False,
                                auth=HttpNegotiateAuth())
        else:
            temp = requests.get(self.hurricaneBoundariesUrl + hurricaneQuery)
        hurricaneBoundariesFs = arcpy.FeatureSet()
        hurricaneBoundariesFs = arcpy.AsShape(temp.content, True)

        if hurricaneBoundariesFs.JSON is not None:
            self.logger.info(
                'Successfully loaded boundary data from portal for hurricane event: '
                + eventId)
            hurricaneBoundariesJson = json.loads(hurricaneBoundariesFs.JSON)
        else:
            self.logger.error('Unable to load boundary data from portal!')
            return False

        if len(hurricaneBoundariesJson['features']) > 0:
            #delete any old boundary data before adding the updated boundary data
            boundaries = hurricaneBoundariesJson['features']
            for boundary in boundaries:
                appid_temp = boundary['attributes']['appid']
                if appid_temp is not None:
                    appid = appid_temp
                fid = boundary['attributes']['fid']
                if self.portalInfo['useNegotiateAuth'] == True:
                    deleteResponse = requests.post(
                        self.hurricaneBoundariesUrl + self.deleteFeatures,
                        data='where=fid=' + str(fid) +
                        '&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&gdbVersion=&rollbackOnFailure=true&f=json',
                        headers=self.tokenHeaders,
                        verify=False,
                        auth=HttpNegotiateAuth())
                else:
                    deleteResponse = requests.post(
                        self.hurricaneBoundariesUrl + self.deleteFeatures,
                        data='where=fid=' + str(fid) +
                        '&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&gdbVersion=&rollbackOnFailure=true&f=json',
                        headers=self.tokenHeaders)
                if not deleteResponse.ok:
                    self.logger.warn(
                        'Unable to delete old boundary data for hurricane event: '
                        + eventId)

        #insert new hurricane boundaries data
        hurricaneBoundary = HurricaneBoundary.HurricaneBoundary()
        hurricaneBoundary.consume(self.indexedEventJson, appid, position,
                                  forecast, pathBuffer)

        #POST data to portal
        if self.portalInfo['useNegotiateAuth'] == True:
            portalResponse = requests.post(self.hurricaneBoundariesUrl +
                                           self.addFeatures,
                                           data=hurricaneBoundary.urlEncode(),
                                           headers=self.tokenHeaders,
                                           verify=False,
                                           auth=HttpNegotiateAuth())
        else:
            portalResponse = requests.post(self.hurricaneBoundariesUrl +
                                           self.addFeatures,
                                           data=hurricaneBoundary.urlEncode(),
                                           headers=self.tokenHeaders)
        if portalResponse.ok:
            responseJSON = json.loads(portalResponse.content)
            if 'addResults' in responseJSON:
                success = responseJSON['addResults'][0]['success']
                if success == True:
                    self.logger.info(
                        'Hurricane boundary data added for event: ' + eventId)

                    #Update the title of the associated dashboard (app) if any
                    if appid:
                        self.updateDashboardTitle(appid, eventId)
                else:
                    self.logger.warn(
                        'Unable to add Hurricane boundary data for event: ' +
                        eventId)
            elif 'error' in responseJSON:
                self.logger.error(
                    'Server error occurred when adding boundary data for event: '
                    + eventId)
            else:
                self.logger.error(
                    'Unknown error occurred when adding boundary data for event: '
                    + eventId)
        else:
            self.logger.error(
                'Server error (' + portalResponse.status_code +
                ') occurred while adding Hurricane boundary data for event: ' +
                eventId)
        return True