질문에서 올바른 용어를 사용했는지 확실하지 않습니다.** kwargs를 사용하는 동안 키워드를 강제로 사용하는 가장 좋은 방법은 무엇입니까?
현재 Google의 Blogger API (블로그 서비스)에서 래퍼/인터페이스를 만들려고합니다. [I가 이미 완료되었습니다,하지만 난 OOP/Python을 배울 수있는 프로젝트로이를 사용하고 알고 있습니다.] 나는 블로그에서 25 개 게시물의 세트를 얻는 방법 만든
:
def get_posts(self, **kwargs):
""" Makes an API request. Returns list of posts. """
api_url = '/blogs/{id}/posts'.format(id=self.id)
return self._send_request(api_url, kwargs)
def _send_request(self, url, parameters={}):
""" Sends an HTTP GET request to the Blogger API.
Returns JSON decoded response as a dict. """
url = '{base}{url}?'.format(base=self.base, url=url)
# Requests formats the parameters into the URL for me
try:
r = requests.get(url, params=parameters)
except:
print "** Could not reach url:\n", url
return
api_response = r.text
return self._jload(api_response)
을
someblog = BloggerClient(url='http://someblog.blogger.com', key='0123')
someblog.get_posts(key=self.key)
모든 API 호출이 키가 URL에 매개 변수로 전송해야합니다 : 문제는
, 나는 API 키를 나는 get_posts
함수를 호출 할 때마다 지정해야합니다. 그런 다음 가장 좋은 방법은 무엇입니까?
나는 (?없는 가장 좋은 방법은 아마도하지만) 가능한 방법을 생각하고는 _send_request()
에 kwargs로 사전에 키를 추가하는 것입니다
def _send_request(self, url, parameters={}):
""" Sends an HTTP get request to Blogger API.
Returns JSON decoded response. """
# Format the full API URL:
url = '{base}{url}?'.format(base=self.base, url=url)
# The api key will be always be added:
parameters['key']= self.key
try:
r = requests.get(url, params=parameters)
except:
print "** Could not reach url:\n", url
return
api_response = r.text
return self._jload(api_response)
정말 주위에 내 머리를 얻을 수 없다 가장 좋은 방법은 무엇입니까 (또는 대부분의 파이썬 방법)입니다. 문제는