0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

firebase-ios-sdk使ってCloud Functionsを呼ぶ

Posted at

Cloud FunctionsはHTTPリクエストで呼ぶことも可能ですが、firebase-ios-sdkを使って呼び出す方が扱いやすいです。(HTTPで呼び出す場合は以下の記事を参考に)
https://qiita.com/kitanote/items/c479230aa1bea4fbe1b0

テストコードの使い方をみると requestAs, responseAsを使っていい感じにデータの受け渡しをしてたので、その書き方を採用しました。

xxx.swift
struct DataResponse: Decodable {
    var name: String
}
struct DataResponse: Decodable {
    var name: String
}

lazy var functions = Functions.functions(region: "asia-northeast1")  
do {
    let result = try await functions.httpsCallable("function-1",
                                                   requestAs: DataResponse.self,
                                                   responseAs: DataResponse.self).call(data)                                              
}
...

1行の呼び出しコードでデータ型の変換までしてくれて非常にわかりやすいです。
ただrequest, response共に、"data"キーが自動で付与される状態になるのでfunctions側では対応してください。

Request

request.json
{
	"data": {
		"name": "aaa"
	}
}

Response

response.json
{
	"data": {
		"name": "AAA"
	}
}

以上です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?