def storage_get(account, recipe_name): # converst to storage class to get helpers such as uid # fetch recipe from storage ( always base bucket name on account, never pass full bucket path ) path = '%s:%s' % (account.get_bucket(full_path=False), recipe_name) project.initialize(_project=settings.CLOUD_PROJECT, _service=settings.CLOUD_SERVICE) recipe = Storage(path) data = json.loads(object_get('service', path)) # ensure there is a setup auth section data['setup'] = data.get('setup', {}) data['setup']['auth'] = data['setup'].get('auth', {}) # add a UUID to the setup section ( overwritten by UI for logging purposes ) data['setup']['uuid'] = recipe.uid() # add user credentials to recipe ( always override if given ) data['setup']['auth']['user'] = account.get_credentials_safe() # add service credentials to recipe or clear them if invalid ( prevent path injection ) try: data['setup']['auth']['service'] = account.project_set.get( identifier=data['setup']['auth'].get('service', '')).service except: try: del data['setup']['auth']['service'] except: pass return data
def _insert(self, new_item, feed_item): """Handles the upload of creative assets to DCM and the creation of the associated entity. This method makes a call to the DCM API to create a new entity. Args: new_item: The item to insert into DCM. feed_item: The feed item representing the creative asset from the Bulkdozer feed. Returns: The newly created item in DCM. """ filename = feed_item.get(FieldMap.CREATIVE_ASSET_FILE_NAME, None) file_buffer = object_get( self.config, 'user', '%s:%s' % (feed_item.get( FieldMap.CREATIVE_ASSET_BUCKET_NAME, None), filename)) file_mime = mimetypes.guess_type(filename, strict=False)[0] media = MediaIoBaseUpload(BytesIO(file_buffer), mimetype=file_mime, chunksize=CHUNKSIZE, resumable=True) result = self._api().insert(profileId=self.profile_id, advertiserId=feed_item.get( FieldMap.ADVERTISER_ID, None), media_body=media, body=new_item).execute() return result