def do_cache_purge(cf, zone_id, params): try: if (params.files != []): data = { 'files': params.files, } elif (params.purge_everything == True): data = { 'purge_everything': True, } else: raise HidoveException( status_code=400, message= 'The purge_everything and the files can\'t be empty at the same time!' ) res = cf.zones.purge_cache.post(zone_id, data=data) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException( status_code=400, message='/zones.purge_cache.post %s - %d %s - api call failed' % (zone_id, e, e)) except Exception as e: raise HidoveException(status_code=400, message=e) return msg(200, 'success', res)
def do_zone_pagerule_create(cf, zone_id, data): try: res = cf.zones.pagerules.post(zone_id, data=data) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException(status_code=400, message=e) return msg(200, 'success', res)
def do_zone_settings_update(cf, zone_id, data): try: settings_get = cf.zones.settings.patch(zone_id, data={'items': data}) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException(status_code=400, message=e) return msg(200, 'success', settings_get)
def get_zone_setting(cf, zone_id, type='always_online'): try: settings_get = getattr(cf.zones.settings, type).get(zone_id) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException(status_code=400, message=e) return msg(200, 'success', settings_get)
def get_dns_record_info(cf, zone_id: str, dns_record_id: str): try: dns_record_info = cf.zones.dns_records.get(zone_id, dns_record_id) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException(status_code=400, message=e) return msg(200, 'success', dns_record_info)
def get_zones(cf): try: zones_get = cf.zones.get() except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException(status_code=400, message=e) return msg(200, 'success', zones_get)
def get_zone_pagerule_details(cf, zone_id, pagerule_id): try: pagerule = cf.zones.pagerules.get(zone_id, pagerule_id) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException(status_code=400, message=e) return msg(200, 'success', pagerule)
def get_all_zone_settings(cf, zone_id): try: settings_get = cf.zones.settings.get(zone_id) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException(status_code=400, message=e) return msg(200, 'success', settings_get)
def do_zone_create(host_key: str, user_key: str, zone_name: str): try: requests_post = requests.post( 'https://api.cloudflare.com/host-gw.html', { 'act': 'zone_set', 'host_key': host_key, 'user_key': user_key, 'zone_name': zone_name, 'resolve_to': 'hidove.cn', 'subdomains': '@,www', }) res_json = requests_post.json() if res_json.get('result') == 'success': return msg(200, 'success', res_json.get('response')) raise HidoveException(status_code=400, message=res_json.get('msg')) except Exception as e: raise HidoveException(status_code=400, message=e)
def do_zone_setting_update(cf, zone_id, type='always_online', data=None): if data is None: data = {} try: settings_get = getattr(cf.zones.settings, type).patch(zone_id, data={'value': data}) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException(status_code=400, message=e) return msg(200, 'success', settings_get)
def do_dns_record_delete(cf, zone_id, dns_record_id): try: res = cf.zones.dns_records.delete(zone_id, dns_record_id) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException( status_code=400, message='/zones.dns_records.delete %s - %d %s - api call failed' % (dns_record_id, e, e)) return msg(200, 'success', res)
def do_zone_pagerule_update(cf, zone_id, pagerule_id, pagerule): try: data = { 'targets': pagerule.targets, 'actions': pagerule.actions, 'priority': pagerule.priority, 'status': pagerule.status, } res = cf.zones.pagerules.patch(zone_id, pagerule_id, data=data) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException(status_code=400, message=e) return msg(200, 'success', res)
def get_zone_info(cf, zone_name: str = None, zone_id: str = None): try: if zone_name: zone_info = cf.zones.get(params={ 'name': zone_name, 'match': 'all' }) else: zone_info = cf.zones.get(identifier1=zone_id, params={'match': 'all'}) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException(status_code=400, message=e) info = {} if type(zone_info) == dict: info = zone_info elif zone_info: info = zone_info.pop() else: raise HidoveException(status_code=400, message='This zone was not found') return msg(200, 'success', info)
async def register(Item: Item): requests_post = requests.post( 'https://api.cloudflare.com/host-gw.html', { 'act': 'user_create', 'host_key': setting.HOST_KEY, 'cloudflare_email': Item.email, 'cloudflare_pass': Item.password, }) res_json = requests_post.json() if res_json.get('result') != 'success': raise HidoveException(status_code=400, message=res_json.get('msg'), data=res_json) return msg(200, 'success')
async def login(form_data: OAuth2PasswordRequestForm = Depends()): requests_post = requests.post( 'https://api.cloudflare.com/host-gw.html', { 'act': 'user_auth', 'host_key': setting.HOST_KEY, 'cloudflare_email': form_data.username, 'cloudflare_pass': form_data.password, }) res_json = requests_post.json() if res_json.get('result') != 'success': raise HidoveException(status_code=400, message=res_json.get('msg'), data=res_json) access_token_expires = timedelta( minutes=setting.ACCESS_TOKEN_EXPIRE_MINUTES) access_token = create_access_token(data=res_json.get('response'), expires_delta=access_token_expires) return {"access_token": access_token, "token_type": "bearer"}
def do_dns_record_update(cf, zone_id, dns_record_id, dns_record: Dns_record): data = { 'name': dns_record.name, 'type': dns_record.type, 'content': dns_record.content, 'proxied': dns_record.proxied, 'ttl': int(dns_record.ttl), } try: res = cf.zones.dns_records.put(zone_id, dns_record_id, data=data) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException( status_code=400, message='/zones.dns_records.put %s - %d %s - api call failed' % (dns_record_id, e, e)) return msg(200, 'success', res)
def do_dns_record_create(cf, zone_id, dns_record: Dns_record): if dns_record.ttl == None: dns_record.ttl = 1 dns_record = { 'name': dns_record.name, 'type': dns_record.type, 'content': dns_record.content, 'proxied': dns_record.proxied, 'ttl': dns_record.ttl, } try: res = cf.zones.dns_records.post(zone_id, data=dns_record) except CloudFlare.exceptions.CloudFlareAPIError as e: raise HidoveException( status_code=400, message='/zones.dns_records.post %s - %d %s - api call failed' % (dns_record.get('name'), e, e)) return msg(200, 'success', res)