Skip to content

Commit

Permalink
统一请求API
Browse files Browse the repository at this point in the history
  • Loading branch information
0xHJK committed Jun 11, 2019
1 parent 3e591ee commit f64a422
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions music_dl/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: HJK
@file: api.py
@time: 2019-06-11
"""

import requests
from . import config
from .exceptions import RequestError, ResponseError, DataError


class MusicApi:
# class property
# 子类修改时使用deepcopy
session = requests.Session()
session.headers.update(config.get("fake_headers"))
if config.get("proxies"):
session.proxies.update(config.get("proxies"))
session.headers.update({"referer": "http://www.google.com/"})

@classmethod
def request(cls, url, method="POST", data=None):
if method == "GET":
resp = cls.session.get(url, params=data, timeout=7)
else:
resp = cls.session.post(url, data=data, timeout=7)
if resp.status_code != requests.codes.ok:
raise RequestError(resp.text)
if not resp.text:
raise ResponseError("No response data.")
return resp.json()

0 comments on commit f64a422

Please sign in to comment.