17
17
This module lets admins get statistics for a Firebase dynamic link.
18
18
"""
19
19
20
+ import requests
20
21
from six .moves import urllib
21
22
import six
22
23
@@ -118,14 +119,14 @@ def __init__(self, platform, event, count):
118
119
"""Create new instance of EventStats(platform, event, count)"""
119
120
if isinstance (platform , six .string_types ) and platform in self ._platforms .keys ():
120
121
raise ValueError (('Raw string "{}" detected. Use a dynamic_links.PLATFORM_* constant' +
121
- ' or the make_event_stat () method.' ).format (platform ))
122
+ ' or the make_from_strings () method.' ).format (platform ))
122
123
if not isinstance (platform , six .string_types ) or platform not in self ._platforms .values ():
123
124
raise ValueError ('platform {}, not recognized' .format (platform ))
124
125
self ._platform = platform
125
126
126
127
if isinstance (event , six .string_types ) and event in self ._event_types .keys ():
127
128
raise ValueError (('Raw string {} detected. Use one of the dynamic_links.EVENT_TYPES_' +
128
- ' constants, or the make_event_stat () method.' ).format (event ))
129
+ ' constants, or the make_from_strings () method.' ).format (event ))
129
130
if not isinstance (event , six .string_types ) or event not in self ._event_types .values ():
130
131
raise ValueError ('event_type {}, not recognized' .format (event ))
131
132
self ._event = event
@@ -134,14 +135,10 @@ def __init__(self, platform, event, count):
134
135
raise ValueError ('Count: {} must be a non negative int' .format (count ))
135
136
self ._count = count
136
137
137
- def __repr__ (self ):
138
- return "EventStats(platform: '{}', event: '{}', count: '{}')" .format (
139
- self .platform , self .event , self .count )
140
-
141
138
@classmethod
142
- def make_event_stat (cls , platform , event , count ):
143
- """make_event_stat creates an EventStat object given the appropriate constants. e.g:
144
- make_event_stat (platform=PLATFORM_DESKTOP, event=EVENT_TYPE_REDIRECT, count=4)"""
139
+ def make_from_strings (cls , platform , event , count ):
140
+ """make_from_strings creates an EventStat object given the appropriate constants. e.g:
141
+ make_from_strings (platform=PLATFORM_DESKTOP, event=EVENT_TYPE_REDIRECT, count=4)"""
145
142
return EventStats (cls ._platforms [platform ],
146
143
cls ._event_types [event ],
147
144
int (count ))
@@ -178,6 +175,9 @@ def duration_days(self, duration_days):
178
175
179
176
class _LinksService (object ):
180
177
"""Provides methods for the Firebase dynamic links interaction"""
178
+
179
+ INTERNAL_ERROR = 'internal-error'
180
+
181
181
def __init__ (self , app ):
182
182
self ._client = _http_client .JsonHttpClient (
183
183
credential = app .credential .get_credential (),
@@ -202,7 +202,20 @@ def get_stats(self, short_link, stat_options):
202
202
raise ValueError ('stat_options must be of type StatOptions.' )
203
203
204
204
request_string = self ._format_request_string (short_link , stat_options )
205
- resp = self ._client .body ('get' , request_string )
206
- link_event_stats_dict = resp .get ('linkEventStats' , [])
207
- event_stats = [EventStats .make_event_stat (** es ) for es in link_event_stats_dict ]
205
+ try :
206
+ resp = self ._client .body ('get' , request_string )
207
+ link_event_stats_dict = resp .get ('linkEventStats' , [])
208
+ except requests .exceptions .RequestException as error :
209
+ msg = 'Failed to call dynamic links API: {0}' .format (error )
210
+ raise ApiCallError (self .INTERNAL_ERROR , msg , error )
211
+ event_stats = [EventStats .make_from_strings (** es ) for es in link_event_stats_dict ]
208
212
return LinkStats (event_stats )
213
+
214
+
215
+ class ApiCallError (Exception ):
216
+ """Represents an Exception encountered while invoking the Firebase dynamic links API."""
217
+
218
+ def __init__ (self , code , message , error = None ):
219
+ Exception .__init__ (self , message )
220
+ self .code = code
221
+ self .detail = error
0 commit comments