Skip to main content
Solved

Using Webhook in Python Script?

  • July 1, 2022
  • 8 replies
  • 466 views

gkvoelkl
Contributor
Forum|alt.badge.img+2

On my fme server is a workspace for download which I can start with a webhook. I want to use that webhook in a Python Script. Are there any examples for calling the webhook and receiving the data in python?

Β 

Best regards Gerhard

Best answer by markwatsafe

HiΒ @gkvoelkl​!Β Β ThisΒ isΒ certainlyΒ possible,Β butΒ theΒ workflowΒ willΒ dependΒ onΒ theΒ typeΒ ofΒ dataΒ you'reΒ outputtingΒ fromΒ theΒ webhookΒ workspace.Β Β ForΒ example,Β areΒ youΒ planningΒ toΒ receiveΒ JSONΒ orΒ XMLΒ inΒ Python?Β Β HereΒ areΒ myΒ findingsΒ withΒ differentΒ FMEΒ ServerΒ services:

Β 

DataΒ StreamingΒ Service

  • InΒ Python,Β you'llΒ wantΒ toΒ useΒ aΒ moduleΒ likeΒ "requests"Β toΒ makeΒ theΒ requestΒ toΒ yourΒ FMEΒ ServerΒ webhookΒ URL.Β Β MoreΒ detailsΒ onΒ W3schools.
  • IfΒ yourΒ webhookΒ workspaceΒ outputsΒ aΒ dataΒ formatΒ likeΒ JSONΒ orΒ XML,Β youΒ canΒ registerΒ theΒ DataΒ StreamingΒ serviceΒ onΒ thisΒ workspaceΒ soΒ thatΒ theΒ responseΒ isΒ theΒ outputΒ data.Β 
  • InΒ yourΒ Python,Β useΒ requestsΒ toΒ makeΒ aΒ GETΒ callΒ toΒ theΒ webhookΒ andΒ storeΒ theΒ responseΒ inΒ aΒ variable.Β Β YouΒ canΒ thenΒ setΒ anΒ attributeΒ valueΒ toΒ theΒ responseΒ body.Β Β ForΒ example,Β IΒ addedΒ thisΒ codeΒ belowΒ inΒ aΒ PythonCreatorΒ inΒ theΒ inputΒ methodΒ andΒ theΒ "response"Β attributeΒ needsΒ toΒ beΒ exposed.
webhook_callΒ =Β requests.get('https://fme-server.com/fmedatastreaming/repository/workspace.fmw?PARAMETER=value&token=tokenvalue')
newFeatureΒ =Β fmeobjects.FMEFeature()
newFeature.setAttribute("response",webhook_call.text)

DataΒ DownloadΒ Service

  • IΒ haven'tΒ testedΒ outΒ aΒ scenarioΒ whereΒ you'reΒ runningΒ aΒ DataΒ DownloadΒ serviceΒ workspace,Β butΒ itΒ wouldΒ beΒ fairlyΒ similarΒ toΒ theΒ approachΒ above.Β Β TheΒ responseΒ bodyΒ willΒ beΒ HTMLΒ thatΒ includesΒ aΒ downloadΒ linkΒ toΒ theΒ outputΒ ZIPΒ file.Β Β YouΒ canΒ eitherΒ useΒ BeautifulΒ SoupΒ inΒ PythonΒ toΒ scrapeΒ outΒ theΒ URL,Β orΒ useΒ ourΒ transformersΒ likeΒ theΒ HTMLExtractor.Β Β 
  • DependingΒ onΒ theΒ data,Β youΒ mayΒ beΒ ableΒ toΒ justΒ passΒ theΒ URLΒ valueΒ intoΒ aΒ FeatureReaderΒ sinceΒ itΒ canΒ readΒ zippedΒ datasetsΒ fromΒ URLs.Β Β 

Β 

PleaseΒ letΒ meΒ knowΒ ifΒ youΒ haveΒ anyΒ furtherΒ questionsΒ aboutΒ this!

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

8 replies

markwatsafe
Safer
Forum|alt.badge.img+11
  • Safer
  • Best Answer
  • July 5, 2022

HiΒ @gkvoelkl​!Β Β ThisΒ isΒ certainlyΒ possible,Β butΒ theΒ workflowΒ willΒ dependΒ onΒ theΒ typeΒ ofΒ dataΒ you'reΒ outputtingΒ fromΒ theΒ webhookΒ workspace.Β Β ForΒ example,Β areΒ youΒ planningΒ toΒ receiveΒ JSONΒ orΒ XMLΒ inΒ Python?Β Β HereΒ areΒ myΒ findingsΒ withΒ differentΒ FMEΒ ServerΒ services:

Β 

DataΒ StreamingΒ Service

  • InΒ Python,Β you'llΒ wantΒ toΒ useΒ aΒ moduleΒ likeΒ "requests"Β toΒ makeΒ theΒ requestΒ toΒ yourΒ FMEΒ ServerΒ webhookΒ URL.Β Β MoreΒ detailsΒ onΒ W3schools.
  • IfΒ yourΒ webhookΒ workspaceΒ outputsΒ aΒ dataΒ formatΒ likeΒ JSONΒ orΒ XML,Β youΒ canΒ registerΒ theΒ DataΒ StreamingΒ serviceΒ onΒ thisΒ workspaceΒ soΒ thatΒ theΒ responseΒ isΒ theΒ outputΒ data.Β 
  • InΒ yourΒ Python,Β useΒ requestsΒ toΒ makeΒ aΒ GETΒ callΒ toΒ theΒ webhookΒ andΒ storeΒ theΒ responseΒ inΒ aΒ variable.Β Β YouΒ canΒ thenΒ setΒ anΒ attributeΒ valueΒ toΒ theΒ responseΒ body.Β Β ForΒ example,Β IΒ addedΒ thisΒ codeΒ belowΒ inΒ aΒ PythonCreatorΒ inΒ theΒ inputΒ methodΒ andΒ theΒ "response"Β attributeΒ needsΒ toΒ beΒ exposed.
webhook_callΒ =Β requests.get('https://fme-server.com/fmedatastreaming/repository/workspace.fmw?PARAMETER=value&token=tokenvalue')
newFeatureΒ =Β fmeobjects.FMEFeature()
newFeature.setAttribute("response",webhook_call.text)

DataΒ DownloadΒ Service

  • IΒ haven'tΒ testedΒ outΒ aΒ scenarioΒ whereΒ you'reΒ runningΒ aΒ DataΒ DownloadΒ serviceΒ workspace,Β butΒ itΒ wouldΒ beΒ fairlyΒ similarΒ toΒ theΒ approachΒ above.Β Β TheΒ responseΒ bodyΒ willΒ beΒ HTMLΒ thatΒ includesΒ aΒ downloadΒ linkΒ toΒ theΒ outputΒ ZIPΒ file.Β Β YouΒ canΒ eitherΒ useΒ BeautifulΒ SoupΒ inΒ PythonΒ toΒ scrapeΒ outΒ theΒ URL,Β orΒ useΒ ourΒ transformersΒ likeΒ theΒ HTMLExtractor.Β Β 
  • DependingΒ onΒ theΒ data,Β youΒ mayΒ beΒ ableΒ toΒ justΒ passΒ theΒ URLΒ valueΒ intoΒ aΒ FeatureReaderΒ sinceΒ itΒ canΒ readΒ zippedΒ datasetsΒ fromΒ URLs.Β Β 

Β 

PleaseΒ letΒ meΒ knowΒ ifΒ youΒ haveΒ anyΒ furtherΒ questionsΒ aboutΒ this!


gkvoelkl
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • July 6, 2022

Hi @Mark Warren​ 

thanks.

I am using Data Download Service. I send the request in Python and get a response.

But there is no link in the response. When I do the web hook in a browser, there is also no link.

E8847F4D-B55F-48E9-9671-EDA0751B1F38Β 

Best regards Gerhard


markwatsafe
Safer
Forum|alt.badge.img+11

Hi @Mark Warren​ 

thanks.

I am using Data Download Service. I send the request in Python and get a response.

But there is no link in the response. When I do the web hook in a browser, there is also no link.

E8847F4D-B55F-48E9-9671-EDA0751B1F38Β 

Best regards Gerhard

Hi @gkvoelkl! The Data Download result with no link means the workspace ran successfully but there were no output features received at the writer(s). I'd suggest taking a look through the job log to see why no features are being received and to compare this with running the workspace in FME Desktop (with the same parameters).


gkvoelkl
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • July 7, 2022

markwatsafe
Safer
Forum|alt.badge.img+11

gkvoelkl
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • July 8, 2022

Hi @Mark Warren​ ,

I tested the queries in the browser and it was the same result.

The first query wrote into the job log file

Β 

2022-7-7 15:27:23 | A JSON syntax error was found at line 1, column 2

2022-7-7 15:27:23 | Unexpected character: 't'. One of the following characters was expected: '"', '}'

2022-7-7 15:27:23 | The value of the import attribute '__GeometryReplacerTemp__' contained invalid JSON, and could not be parsed

Β 

Perhaps I have to change something in my workspace.

Β 

I am using a separate Python script. I want to integrate it into QGIS.

I will change the geometry parameter into four float parameter (xmin, xmax,ymin,ymax).

That will be easier.

Β 

Best Regards Gerhard


markwatsafe
Safer
Forum|alt.badge.img+11

Hi @Mark Warren​ ,

I tested the queries in the browser and it was the same result.

The first query wrote into the job log file

Β 

2022-7-7 15:27:23 | A JSON syntax error was found at line 1, column 2

2022-7-7 15:27:23 | Unexpected character: 't'. One of the following characters was expected: '"', '}'

2022-7-7 15:27:23 | The value of the import attribute '__GeometryReplacerTemp__' contained invalid JSON, and could not be parsed

Β 

Perhaps I have to change something in my workspace.

Β 

I am using a separate Python script. I want to integrate it into QGIS.

I will change the geometry parameter into four float parameter (xmin, xmax,ymin,ymax).

That will be easier.

Β 

Best Regards Gerhard

I'd suggest verifying that the GeoJSON is correct if you still want to try this approach. In your previous example, I noticed both URLs have invalid JSON (with a missing comma between the 3rd and 4th set of coordinates). Was this GeoJSON polygon generated outside of FME?


gkvoelkl
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • July 12, 2022

Hi @Mark Warren​ ,

thank you very much for your help.

with four parameters and without json it is much easier.

Β 

Best Regards Gerhard