取得上次更新後威脅清單的差異
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
Java
如要驗證 Web Risk,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
import com.google.cloud.webrisk.v1.WebRiskServiceClient;
import com.google.protobuf.ByteString;
import com.google.webrisk.v1.CompressionType;
import com.google.webrisk.v1.ComputeThreatListDiffRequest;
import com.google.webrisk.v1.ComputeThreatListDiffRequest.Constraints;
import com.google.webrisk.v1.ComputeThreatListDiffResponse;
import com.google.webrisk.v1.ThreatType;
import java.io.IOException;
public class ComputeThreatListDiff {
public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
// The threat list to update. Only a single ThreatType should be specified per request.
ThreatType threatType = ThreatType.MALWARE;
// The current version token of the client for the requested list. If the client does not have
// a version token (this is the first time calling ComputeThreatListDiff), this may be
// left empty and a full database snapshot will be returned.
ByteString versionToken = ByteString.EMPTY;
// The maximum size in number of entries. The diff will not contain more entries
// than this value. This should be a power of 2 between 2**10 and 2**20.
// If zero, no diff size limit is set.
int maxDiffEntries = 1024;
// Sets the maximum number of entries that the client is willing to have in the local database.
// This should be a power of 2 between 2**10 and 2**20. If zero, no database size limit is set.
int maxDatabaseEntries = 1024;
// The compression type supported by the client.
CompressionType compressionType = CompressionType.RAW;
computeThreatDiffList(threatType, versionToken, maxDiffEntries, maxDatabaseEntries,
compressionType);
}
// Gets the most recent threat list diffs. These diffs should be applied to a local database of
// hashes to keep it up-to-date.
// If the local database is empty or excessively out-of-date,
// a complete snapshot of the database will be returned. This Method only updates a
// single ThreatList at a time. To update multiple ThreatList databases, this method needs to be
// called once for each list.
public static void computeThreatDiffList(ThreatType threatType, ByteString versionToken,
int maxDiffEntries, int maxDatabaseEntries, CompressionType compressionType)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `webRiskServiceClient.close()` method on the client to safely
// clean up any remaining background resources.
try (WebRiskServiceClient webRiskServiceClient = WebRiskServiceClient.create()) {
Constraints constraints = Constraints.newBuilder()
.setMaxDiffEntries(maxDiffEntries)
.setMaxDatabaseEntries(maxDatabaseEntries)
.addSupportedCompressions(compressionType)
.build();
ComputeThreatListDiffResponse response = webRiskServiceClient.computeThreatListDiff(
ComputeThreatListDiffRequest.newBuilder()
.setThreatType(threatType)
.setVersionToken(versionToken)
.setConstraints(constraints)
.build());
// The returned response contains the following information:
// https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#computethreatlistdiffresponse
// Type of response: DIFF/ RESET/ RESPONSE_TYPE_UNSPECIFIED
System.out.println(response.getResponseType());
// List of entries to add and/or remove.
// System.out.println(response.getAdditions());
// System.out.println(response.getRemovals());
// New version token to be used the next time when querying.
System.out.println(response.getNewVersionToken());
// Recommended next diff timestamp.
System.out.println(response.getRecommendedNextDiff());
System.out.println("Obtained threat list diff.");
}
}
}
Python
如要驗證 Web Risk,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
from google.cloud import webrisk_v1
from google.cloud.webrisk_v1 import ComputeThreatListDiffResponse
def compute_threatlist_diff(
threat_type: webrisk_v1.ThreatType,
version_token: bytes,
max_diff_entries: int,
max_database_entries: int,
compression_type: webrisk_v1.CompressionType,
) -> ComputeThreatListDiffResponse:
"""Gets the most recent threat list diffs.
These diffs should be applied to a local database of hashes to keep it up-to-date.
If the local database is empty or excessively out-of-date,
a complete snapshot of the database will be returned. This Method only updates a
single ThreatList at a time. To update multiple ThreatList databases, this method needs to be
called once for each list.
Args:
threat_type: The threat list to update. Only a single ThreatType should be specified per request.
threat_type = webrisk_v1.ThreatType.MALWARE
version_token: The current version token of the client for the requested list. If the
client does not have a version token (this is the first time calling ComputeThreatListDiff),
this may be left empty and a full database snapshot will be returned.
max_diff_entries: The maximum size in number of entries. The diff will not contain more entries
than this value. This should be a power of 2 between 2**10 and 2**20.
If zero, no diff size limit is set.
max_diff_entries = 1024
max_database_entries: Sets the maximum number of entries that the client is willing to have in the local database.
This should be a power of 2 between 2**10 and 2**20. If zero, no database size limit is set.
max_database_entries = 1024
compression_type: The compression type supported by the client.
compression_type = webrisk_v1.CompressionType.RAW
Returns:
The response which contains the diff between local and remote threat lists. In addition to the threat list,
the response also contains the version token and the recommended time for next diff.
"""
webrisk_client = webrisk_v1.WebRiskServiceClient()
constraints = webrisk_v1.ComputeThreatListDiffRequest.Constraints()
constraints.max_diff_entries = max_diff_entries
constraints.max_database_entries = max_database_entries
constraints.supported_compressions = [compression_type]
request = webrisk_v1.ComputeThreatListDiffRequest()
request.threat_type = threat_type
request.version_token = version_token
request.constraints = constraints
response = webrisk_client.compute_threat_list_diff(request)
# The returned response contains the following information:
# https://cloud.google.com/web-risk/docs/reference/rpc/google.cloud.webrisk.v1#computethreatlistdiffresponse
# Type of response: DIFF/ RESET/ RESPONSE_TYPE_UNSPECIFIED
print(response.response_type)
# New version token to be used the next time when querying.
print(response.new_version_token)
# Recommended next diff timestamp.
print(response.recommended_next_diff)
return response
後續步驟
如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽器。