<s> import requests <EOL> import json <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . utils . encoding import force_text <EOL> from . import conf <EOL> class GCMMessage ( object ) : <EOL> def __init__ ( self ) : <EOL> self . api_key = conf . GCM_APIKEY <EOL> if not self . api_key : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" ) <EOL> def _chunks ( self , items , limit ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( <NUM_LIT:0> , len ( items ) , limit ) : <EOL> yield items [ i : i + limit ] <EOL> def send ( self , data , registration_ids = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( data , dict ) : <EOL> data = { '<STR_LIT>' : data } <EOL> registration_ids = registration_ids or [ ] <EOL> if len ( registration_ids ) > conf . GCM_MAX_RECIPIENTS : <EOL> ret = [ ] <EOL> for chunk in self . _chunks ( <EOL> registration_ids , conf . GCM_MAX_RECIPIENTS ) : <EOL> ret . append ( self . send ( data , registration_ids = chunk , ** kwargs ) ) <EOL> return ret <EOL> values = { <EOL> '<STR_LIT:data>' : data , <EOL> '<STR_LIT>' : '<STR_LIT:message>' } <EOL> if registration_ids : <EOL> values . update ( { '<STR_LIT>' : registration_ids } ) <EOL> values . update ( kwargs ) <EOL> values = json . dumps ( values ) <EOL> headers = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT>' + self . api_key } <EOL> response = requests . post ( <EOL> url = "<STR_LIT>" , <EOL> data = values , headers = headers ) <EOL> response . raise_for_status ( ) <EOL> return registration_ids , json . loads ( force_text ( response . content ) ) </s>
