2015-05-19 9 views
8

मैं आधिकारिक ट्यूटोरियल द्वारा प्रदान उदाहरण यहां पाया की कोशिश की है:आपूर्ति चर गतिशील

http://saratoga.readthedocs.org/en/latest/serviceclasses.html

मैं कोड के लिए कुछ परिवर्तन किए हैं और इस लिए कि यह कैसे की तरह लग रहा है:

http://23.21.167.60:8094/v1/yearlength?name=earth 

मेरी समस्या यह है कि मुझे खाता = 211829 यूआरएल में नाम = पृथ्वी प्रदान करने की आवश्यकता है।

जो मैंने नीचे लिखा है वह काम कर रहा है क्योंकि मैंने कक्षा में खाता संख्या प्रदान की है। मैं इसे गतिशील तरीके से कैसे करूं?

import json 
from saratoga.api import SaratogaAPI, DefaultServiceClass 

class PlanetServiceClass(DefaultServiceClass): 
    def __init__(self, myaccount): 
     self.yearLength = { 
      "earth": self.myquery(myaccount), 
      "pluto": {"seconds": 7816176000} 
     } 

    def myquery(self, myaccount): 
     import pandas as pd 

     query = ('select * from mydata198 where account = %s ') % (myaccount) 

     import sqlalchemy 
     engine = sqlalchemy.create_engine('mysql+pymysql://dba:[email protected]/test') 
     conn = engine.raw_connection() 

     df=pd.read_sql(query, conn) 
     return df.to_json() 

class PlanetAPI(object): 
    class v1(object): 
     def yearlength_GET(self, request, params): 
      planetName = params["params"]["name"].lower() 
      return self.yearLength.get(planetName) 

APIDescription = json.load(open("planets.json")) 
myAPI = SaratogaAPI(PlanetAPI, APIDescription, serviceClass=PlanetServiceClass('211829')) 
myAPI.run(port=8094) 

मैं PlanetServiceClass करने के लिए PlanetAPI वर्ग से account_num चर कैसे पारित करते हैं?

उत्तर

3

आप तब कोड में आप द्वारा ही पहुंच सकते

http://23.21.167.60:8094/v1/yearlength?name=earth&account_num=12345 

करने के लिए अपने यूआरएल को बदलने के लिए की आवश्यकता होगी

account_num = params["params"]["account_num"] 

संपादित करें:

समस्या यह है कि आप वर्तमान में कर रहे हैं सर्वर चलाने से पहले सर्वर को आरंभ करने के लिए account_num का उपयोग करना। इसलिए इसे चलाने के बाद आपको इसे पास करने की आवश्यकता है।

class PlanetAPI(object): 
    class v1(object): 
     def yearlength_GET(self, request, params): 
      planetName = params["params"]["name"].lower() 
      account_num = params["params"]["account_num"] 

      mysql_result_json = self.myquery(account_num) 
      self.yearLength['earth'] = mysql_result_json # assign to earth if you want, as in your question 
      return self.yearLength.get(planetName) 

फिर वापस क्या यह ट्यूटोरियल में किया गया था करने के लिए कोड के बाकी बदलने के लिए:

myAPI = SaratogaAPI(PlanetAPI, APIDescription, serviceClass=PlanetServiceClass()) 

और

class PlanetServiceClass(DefaultServiceClass): 
    def __init__(self): 
+0

और मैं इस के लिए myAPI बदलना चाहिए? serviceClass = PlanetServiceClass (account_num) – shantanuo

+0

ठीक उत्तर को अपडेट किया गया। आपको "अनुरोध" समय पर 'account_num' असाइन करने की आवश्यकता है, सर्वर प्रारंभिक समय नहीं। –

+0

धन्यवाद। इसने काम कर दिया। लेकिन यह केवल तभी काम करता है जब मैं दोनों वर्गों में मैक्वायरी फ़ंक्शन कॉपी करता हूं। क्यूं कर? – shantanuo

संबंधित मुद्दे