Skip to main content
Question

Python Script to submit a job to FME Server


dewrightco
Contributor
Forum|alt.badge.img+4

I am looking for a good/best-practices example python script the can check for a token for the user, if not valid will request one and then execute a set workspace.

 

Anyone have anything they are willing to share?

4 replies

david_r
Celebrity
  • December 3, 2020

Unless you need to send the token several times per second, I would simply request a new, short-lived token for every request. They're very quick and easy to get.


dewrightco
Contributor
Forum|alt.badge.img+4
  • Author
  • Contributor
  • December 8, 2020
david_r wrote:

Unless you need to send the token several times per second, I would simply request a new, short-lived token for every request. They're very quick and easy to get.

Ok, I can get that; still having a hard time getting a good script to work that does it; getting a 403 constantly.

 

Trying to write something that I can encapsulate in ArcGIS Server

 

def do_getToken():
    """Call to FME Server to get a token for the GUEST user"""
    try:
        url = '{0}/fmerest/v3/tokens'.format(SERVER_URL)
 
        tokenParams = {
            "restricted""true",
            "name""guestToken",
            "description""guest token",
            "expirationTimeout"3600,
            "user""fmeadmin",
            "type""SYSTEM",
            "enabled""true"
        }
 
        # Request constructor expects bytes, so we need to encode the string
        body = json.dumps(tokenParams).encode('utf-8')
 
        headers = {
            'Content-Type''application/json',
            'Accept''application/json'
        }
 
        # This will use POST, since we are including data
        req = urllib.request.Request(url, body, headers)
 
        r = urllib.request.urlopen(req, context=context)
 
        print('Request status: ' + str(r.status))
 
        resp = r.read()
        resp = resp.decode('utf-8')
        resp = json.loads(resp)
 
        TOKEN = resp
 
    except arcpy.ExecuteError:
        print(arcpy.GetMessages(2))
    except Exception as e:
        print(e.args[0])
# End do_getToken function

But not a lot of examples for calling FME from python.


david_r
Celebrity
  • December 9, 2020
dewrightco wrote:

Ok, I can get that; still having a hard time getting a good script to work that does it; getting a 403 constantly.

 

Trying to write something that I can encapsulate in ArcGIS Server

 

def do_getToken():
    """Call to FME Server to get a token for the GUEST user"""
    try:
        url = '{0}/fmerest/v3/tokens'.format(SERVER_URL)
 
        tokenParams = {
            "restricted""true",
            "name""guestToken",
            "description""guest token",
            "expirationTimeout"3600,
            "user""fmeadmin",
            "type""SYSTEM",
            "enabled""true"
        }
 
        # Request constructor expects bytes, so we need to encode the string
        body = json.dumps(tokenParams).encode('utf-8')
 
        headers = {
            'Content-Type''application/json',
            'Accept''application/json'
        }
 
        # This will use POST, since we are including data
        req = urllib.request.Request(url, body, headers)
 
        r = urllib.request.urlopen(req, context=context)
 
        print('Request status: ' + str(r.status))
 
        resp = r.read()
        resp = resp.decode('utf-8')
        resp = json.loads(resp)
 
        TOKEN = resp
 
    except arcpy.ExecuteError:
        print(arcpy.GetMessages(2))
    except Exception as e:
        print(e.args[0])
# End do_getToken function

But not a lot of examples for calling FME from python.

I would recommend using the requests library rather than urllib, it is so much easier to work with. I suspect that requests is even installed with some ArcGIS Python interpreters by default. Here's some code that requests a token from FME Server, using the requests module:

def get_token(self):
    """
    Requests a token from FME Server using the username/password
    :return: Security token
    """
    url = '%s/fmetoken/service/generate' % (self.base_url)
    payload = {
        'user': self.username,
        'password': self.password,
        'expiration': str(self.token_ttl_secs),
        'timeframe''second'
    }
    response = requests.post(url, data=payload)
    response.raise_for_status()
    self.token = response.text
    return self.token

Granted, it uses the "old" token API, but it should be easy to adapt.

I hope the instance variables are self-explanatory, if not, let me know.


dewrightco
Contributor
Forum|alt.badge.img+4
  • Author
  • Contributor
  • December 9, 2020
dewrightco wrote:

Ok, I can get that; still having a hard time getting a good script to work that does it; getting a 403 constantly.

 

Trying to write something that I can encapsulate in ArcGIS Server

 

def do_getToken():
    """Call to FME Server to get a token for the GUEST user"""
    try:
        url = '{0}/fmerest/v3/tokens'.format(SERVER_URL)
 
        tokenParams = {
            "restricted""true",
            "name""guestToken",
            "description""guest token",
            "expirationTimeout"3600,
            "user""fmeadmin",
            "type""SYSTEM",
            "enabled""true"
        }
 
        # Request constructor expects bytes, so we need to encode the string
        body = json.dumps(tokenParams).encode('utf-8')
 
        headers = {
            'Content-Type''application/json',
            'Accept''application/json'
        }
 
        # This will use POST, since we are including data
        req = urllib.request.Request(url, body, headers)
 
        r = urllib.request.urlopen(req, context=context)
 
        print('Request status: ' + str(r.status))
 
        resp = r.read()
        resp = resp.decode('utf-8')
        resp = json.loads(resp)
 
        TOKEN = resp
 
    except arcpy.ExecuteError:
        print(arcpy.GetMessages(2))
    except Exception as e:
        print(e.args[0])
# End do_getToken function

But not a lot of examples for calling FME from python.

Awesome; and this gave me enough of a jumpstart to fix the rest of my class to get my job execution method in place.

 

Thank you so much!


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings