同じ関数からAPIとWebフォームを作る
階乗を計算する簡単なAPIFunctionを作成する.
In[1]:=
![Click for copyable input](assets.ja/make-an-api-and-a-web-form-from-the-same-function/In_3.png)
api = APIFunction["n" -> "Integer", #n! &]
Out[1]=
![](assets.ja/make-an-api-and-a-web-form-from-the-same-function/O_3.png)
別のWebサイト用のAPIFunctionと,フォームのWebサイトを直接訪れる実際のユーザ用のFormFunctionの両方が欲しいとしよう.FormFunctionとAPIFunctionは同じシグネチャを持つので,コードを書き直す必要はない.
In[2]:=
![Click for copyable input](assets.ja/make-an-api-and-a-web-form-from-the-same-function/In_4.png)
CloudDeploy[URLDispatcher[{
"/api" -> api,
"/form" -> FormFunction @@ api
}],
"user:documentation@wolfram.com/application"
]
https://www.wolframcloud.com/objects/documentation@wolfram.com/application/api?n=10でAPIを使うことができる.
![](assets.ja/index.ja/GB_5.png)
https://www.wolframcloud.com/objects/documentation@wolfram.com/application/formでは直接フォームを使うことができる.
![](assets.ja/index.ja/GB_6.png)