~feng-kylin/youker-assistant/youker-assistant

« back to all changes in this revision

Viewing changes to backends/youker-assistant-daemon/src/weather/piston.py

  • Committer: kobe
  • Date: 2015-02-13 07:37:10 UTC
  • Revision ID: xiangli@ubuntukylin.com-20150213073710-0jyp02ilyi5njj10
Qt Version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# -*- coding: utf-8 -*-
3
 
 
4
 
### BEGIN LICENSE
5
 
# Copyright (C) 2013 ~ 2014 National University of Defense Technology(NUDT) & Kylin Ltd
6
 
# Author: Kobe Lee
7
 
#
8
 
# This program is free software: you can redistribute it and/or modify it
9
 
# under the terms of the GNU General Public License version 3, as published
10
 
# by the Free Software Foundation.
11
 
#
12
 
# This program is distributed in the hope that it will be useful, but
13
 
# WITHOUT ANY WARRANTY; without even the implied warranties of
14
 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15
 
# PURPOSE.  See the GNU General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU General Public License along
18
 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
### END LICENSE
20
 
 
21
 
"""This module provides the RatingsAndReviewsAPI class for talking to the
22
 
ratings and reviews API, plus a few helper classes.
23
 
"""
24
 
 
25
 
import sys
26
 
reload(sys)
27
 
sys.setdefaultencoding('utf8')
28
 
from urllib import quote_plus
29
 
from piston_mini_client import (
30
 
    PistonAPI,
31
 
    PistonResponseObject,
32
 
    PistonSerializable,
33
 
    returns,
34
 
    returns_json,
35
 
    returns_list_of,
36
 
    )
37
 
from piston_mini_client.validators import validate_pattern, validate
38
 
from piston_mini_client import APIError
39
 
import httplib2
40
 
 
41
 
# These are factored out as constants for if you need to work against a
42
 
# server that doesn't support both schemes (like http-only dev servers)
43
 
PUBLIC_API_SCHEME = 'http'
44
 
AUTHENTICATED_API_SCHEME = 'https'
45
 
 
46
 
 
47
 
class WeatherPistonAPI(PistonAPI):
48
 
    """A client for talking to the reviews and ratings API.
49
 
 
50
 
    If you pass no arguments into the constructor it will try to connect to
51
 
    localhost:8000 so you probably want to at least pass in the
52
 
    ``service_root`` constructor argument.
53
 
    """
54
 
    default_service_observe = 'observe'
55
 
    default_content_type = 'application/x-www-form-urlencoded'
56
 
 
57
 
    @validate_pattern('cityid', r'[0-9a-z+-.:/]+', required=False)
58
 
    @returns_json
59
 
    def get_cma_observe_weather(self, cityid):
60
 
        url = '%s/%s/' % (self.default_service_observe, cityid)
61
 
        return self._get(url, scheme=PUBLIC_API_SCHEME)