Add update() method

This commit is contained in:
AKFish
2013-08-21 23:01:40 +08:00
parent b71329e560
commit bd7abb5877
3 changed files with 90 additions and 1 deletions
+49
View File
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
############################ Copyrights and license ############################
# #
# Copyright 2013 AKFish <akfish@gmail.com> #
# #
# This file is part of PyGithub. http://jacquev6.github.com/PyGithub/ #
# #
# PyGithub is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Lesser General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
# details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################
# TODO: As of Thu Aug 21 22:40:13 (BJT) Chinese Standard Time 2013
# lots of consts in this project are explict
# should realy round them up and reference them by consts
################################################################################
# Helper Function #
################################################################################
def get(dic, key):
if key in dic:
return key[dic]
return None
################################################################################
# Request Header #
# (Case sensitive) #
################################################################################
REQ_IF_NONE_MATCH = "If-None-Match"
REQ_IF_MODIFIED_SINCE = "If-Modified-Since"
################################################################################
# Response Header #
# (Lower Case) #
################################################################################
RES_ETAG = "etag"
RES_LAST_MODIFED = "last-modified"
+40 -1
View File
@@ -32,6 +32,8 @@ import GithubException
import pickle
import Consts
class _NotSetType:
def __repr__(self):
return "NotSet"
@@ -118,8 +120,45 @@ class GithubObject(object):
'''
with open(file_name, 'rb') as f:
return pickle.load(f)
@property
def etag(self):
'''
:type str
'''
return Consts.get(self._headers, Consts.RES_ETAG)
@property
def last_modified(self):
'''
:type str
'''
return Consts.get(self._headers, Consts.RES_LAST_MODIFED)
def update(self):
'''
Check and update the object with conditional request
:rtype: Boolean value indicating whether the object is changed
'''
conditionalRequestHeader = dict()
if self.etag is not None:
conditionalRequestHeader[Consts.REQ_IF_NONE_MATCH] = self.etag
if self.last_modified is not None:
conditionalRequestHeader[Consts.REQ_IF_MODIFIED_SINCE] = self.last_modified
try:
headers, data = self._requester.requestJsonAndCheck(
"GET",
self._url,
conditionalRequestHeader,
None
)
self._storeAndUseAttributes(data)
self.__completed = True
return True
except: #GithubException.NotModifiedException:
return False
class NonCompletableGithubObject(GithubObject):
def _completeIfNeeded(self):