FileMakerでURLをパースするカスタム関数を作りました

Teruhiro Komaki(Archive)
frudens Tech Blog
Published in
10 min readDec 27, 2022
FileMaker Custom Function: ParseUrl ( _url )

AWS の API を利用する際に、リクエストのヘッダーに署名を含める必要があります。

詳細は、以下のリンクを参照して頂きたいです。

海外のサンプルファイルを色々と見ましたが、やはり、自分で手を動かして、 コードを書かないと理解できないので、ドキュメントを見て実装しました。

手が空いたら、会社のブログで、AWS の API を実行する方法については、記事を書きたいと思います…

上記の AWS のドキュメントの通りですが、AWS の API をリクエストする際に、host とかパスとかクエリーが必要になります。

そのためにタイトルの通り、カスタム関数を作りました。

余談ですが…

公開しましたら Nice!! とコメントを頂きました。

一言コメントって、頂くと意外に嬉しいものなので、私も Nice!! などコメントするように心がけるようにしたいと感じました。

Comment

カスタム関数について

ParseUrl ( _url )

試したい方は、以下の URL を参照して、自分のカスタム App に追加して頂ければと思います。

コード

/* ---------------------------------------------------------------------------
ParseUrl ( _url )

Description:
Parse the URL and return the result as JSON.

Note:
- If urlIsEmpty is 'false', the URL may have failed to parse.
- Please let me know if there is anything I have overlooked.
- For the structure of the URL, I referred to the URL below.
https://en.wikipedia.org/wiki/URL

Example:
ParseUrl ( "https://username:password@bucketname.s3.ap-northeast-1.amazonaws.com:80/dir1/file.txt?q1=v1&q2=v2#fragment" )
-> {"fileName":"file.txt","fragment":"fragment","host":"bucketname.s3.ap-northeast-1.amazonaws.com","password":"password","path":"/dir1/file.txt","port":"80","query":"q1=v1&q2=v2","scheme":"https","urlIsEmpty":true,"userName":"username"}

ParseUrl ( "https://en.wikipedia.org/wiki/URL" )
-> {"fileName":"","fragment":"","host":"en.wikipedia.org","password":"","path":"/wiki/URL","port":"","query":"","scheme":"https","urlIsEmpty":true,"userName":""}

History:
2022-11-24, Teruhiro Komaki <komaki@frudens.jp>
--------------------------------------------------------------------------- */

Let ( [
// _url = "https://username:password@bucketname.s3.ap-northeast-1.amazonaws.com:80/dir1/file.txt?q1=v1&q2=v2#fragment" ;
~url = _url ;

~scheme = GetValue ( Substitute ( ~url ; "://" ; "¶" ) ; 1 ) ;
~url = Substitute ( ~url ; ~scheme & "://" ; "" ) ;

~userinfo = If ( PatternCount ( ~url ; "@" ) ; GetValue ( Substitute ( ~url ; "@" ; "¶" ) ; 1 ) ) ;
~userinfoList = Substitute ( ~userinfo ; ":" ; "¶" ) ;
~userName = GetValue ( ~userinfoList ; 1 ) ;
~password = GetValue ( ~userinfoList ; 2 ) ;
~url = If ( not IsEmpty ( ~userinfo ) ; Substitute ( ~url ; ~userinfo & "@" ; "" ) ; ~url ) ;

~host = GetValue ( Substitute ( ~url ; [ ":" ; "¶" ] ; [ "/" ; "¶" ] ; [ "?" ; "¶" ] ; [ "#" ; "¶" ] ) ; 1 ) ;
~url = Substitute ( ~url ; ~host ; "" ) ;

~port = GetAsNumber ( GetValue ( Substitute ( ~url ; [ "/" ; "¶" ] ; [ "?" ; "¶" ] ; [ "#" ; "¶" ] ) ; 1 ) ) ;
~url = If ( not IsEmpty ( ~port ) ; Substitute ( ~url ; ":" & ~port ; "" ) ; ~url ) ;

~path = GetValue ( Substitute ( ~url ; [ "?" ; "¶" ] ; [ "#" ; "¶" ] ) ; 1 ) ;
~url = If ( not IsEmpty ( ~path ) ; Substitute ( ~url ; ~path ; "" ) ; ~url ) ;

~pathList = Substitute ( ~path ; "/" ; "¶" ) ;
~pathListLast = GetValue ( ~pathList ; ValueCount ( ~pathList ) ) ;
~fileName = If ( GetAsBoolean ( PatternCount ( ~pathListLast ; "." ) ) ; ~pathListLast ; "" ) ;

~fragment = GetValue ( Substitute ( ~url ; [ "#" ; "¶" ] ) ; 2 ) ;
~url = If ( not IsEmpty ( ~fragment ) ; Substitute ( ~url ; "#" & ~fragment ; "" ) ; ~url ) ;

~query = GetValue ( Substitute ( ~url ; [ "?" ; "¶" ] ) ; 2 ) ;
~url = If ( not IsEmpty ( ~query ) ; Substitute ( ~url ; "?" & ~query ; "" ) ; ~url ) ;

~urlIsEmpty = If ( IsEmpty ( ~url ) ; True ; False )

] ;

JSONSetElement ( "" ;
[ "scheme" ; ~scheme ; JSONString ] ;
[ "userName" ; ~userName ; JSONString ] ;
[ "password" ; ~password ; JSONString ] ;
[ "host" ; ~host ; JSONString ] ;
[ "port" ; ~port ; JSONString ] ;
[ "path" ; ~path ; JSONString ] ;
[ "fileName" ; ~fileName ; JSONString ] ;
[ "fragment" ; ~fragment ; JSONString ] ;
[ "query" ; ~query ; JSONString ] ;
[ "urlIsEmpty" ; ~urlIsEmpty ; JSONBoolean ]
)

) /*Let*/

サンプル

  • “https://username:password@bucketname.s3.ap-northeast-1.amazonaws.com:80/dir1/file.txt?q1=v1&q2=v2#fragment”
ParseUrl ( "https://username:password@bucketname.s3.ap-northeast-1.amazonaws.com:80/dir1/file.txt?q1=v1&q2=v2#fragment" )

----------

{
"fileName" : "file.txt",
"fragment" : "fragment",
"host" : "bucketname.s3.ap-northeast-1.amazonaws.com",
"password" : "password",
"path" : "/dir1/file.txt",
"port" : "999",
"query" : "q1=v1&q2=v2",
"scheme" : "https",
"urlIsEmpty" : true,
"userName" : "username"
}
  • “https://www.briandunning.com/cf/2627”
ParseUrl ( "https://www.briandunning.com/cf/2627" )

----------

{
"fileName" : "",
"fragment" : "",
"host" : "www.briandunning.com",
"password" : "",
"path" : "/cf/2627",
"port" : "",
"query" : "",
"scheme" : "https",
"urlIsEmpty" : true,
"userName" : ""
}

バグなど見つかりましたら、コメント頂ければと思います。

--

--