Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/error_reporting/_gapic.py: 50%

14 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:45 +0000

1# Copyright 2016 Google LLC All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15"""GAX wrapper for Error Reporting API requests.""" 

16import json 

17 

18import google.cloud.errorreporting_v1beta1 

19 

20 

21def make_report_error_api(client): 

22 """Create an instance of the gapic Logging API. 

23 

24 :type client::class:`google.cloud.error_reporting.Client` 

25 :param client: Error Reporting client. 

26 

27 :rtype: :class:_ErrorReportingGapicApi 

28 :returns: An Error Reporting API instance. 

29 """ 

30 gapic_api = google.cloud.errorreporting_v1beta1.ReportErrorsServiceClient( 

31 credentials=client._credentials, 

32 client_info=client._client_info, 

33 client_options=client._client_options, 

34 ) 

35 return _ErrorReportingGapicApi(gapic_api, client.project) 

36 

37 

38class _ErrorReportingGapicApi(object): 

39 """Helper mapping Error Reporting-related APIs 

40 

41 :type gapic: 

42 :class:`google.cloud.errorreporting_v1beta1.ReportErrorsServiceClient` 

43 :param gapic: API object used to make RPCs. 

44 

45 :type project: str 

46 :param project: Google Cloud Project ID 

47 """ 

48 

49 def __init__(self, gapic_api, project): 

50 self._gapic_api = gapic_api 

51 self._project = project 

52 

53 def report_error_event(self, error_report): 

54 """Uses the gapic client to report the error. 

55 

56 :type error_report: dict 

57 :param error_report: 

58 payload of the error report formatted according to 

59 https://cloud.google.com/error-reporting/docs/formatting-error-messages 

60 This object should be built using 

61 Use 

62 :meth:~`google.cloud.error_reporting.client._build_error_report` 

63 """ 

64 project_name = f"projects/{self._project}" 

65 

66 # Since error_report uses camel case for key names (like serviceContext), 

67 # but ReportedErrorEvent uses snake case for key names (like service_context), 

68 # we need to route throught json. 

69 error_report_payload = ( 

70 google.cloud.errorreporting_v1beta1.ReportedErrorEvent.from_json( 

71 json.dumps(error_report) 

72 ) 

73 ) 

74 

75 self._gapic_api.report_error_event( 

76 project_name=project_name, event=error_report_payload 

77 )