2016-11-15 14 views
0

渡された引数に応じて、リクエストとともに送信するボディを構築する最善の方法を知りました。引数に基づいてリクエストボディを構築する

あなたが最初にこのように体を構築することがあります:

この

はシナリオです

data={ 
    'user': user, 
    'password': password, 
    'somethingelse': somethingelse, 
    } 

わかりましたので、それらを掲示するために必要なフィールドになります。場合によっては、API呼び出しに5~10の他のフィールドがあり、必要に応じて追加できますか?

投稿機能に* argsを渡すと、オプションのフィールドを空のままにしてargsを設定することができますが、argsが空の場合は常に失敗します。また、コンテンツを持たないオプションのフィールドを投稿しても、APIは値を期待するため、エラーを返すことがあります。

更新 もう少し情報が必要でしたので、説明してみましょう。

私は最近、さまざまなREST APIを使って作業しています。私は自分自身で議論を始めることがあります。どのようにPOST/GET機能をすべて設計するのですか?

この例を取る:彼らの両方は、それらが使用される場合に存在しなければならないもののパラメータは、オプションの両方であるように、この例で

def search(self, query, ??): 
    """ 
    Executes a searchquery, that is then stored and needs 
    to be called again to get results, using the returned 
    search_id. 
    :param query: Query to be run with the search 
    :return: Array of the current searchid, which is needed 
      for other functions, and the content of HTTP response. 
    """ 
    search_id = int(round(time.time() * 1000)) 
    if len(args) > 0: 
     response = self._post(
      '/server/search', data={ 
       'query': query, 
       'search_session_id': search_id, 
       'user_session_id': self.token, 
      }) 

を、start_timeend_timeは、この要求に含めることができます。

次に、私はこれをどのように設計するのかを考えているので、オプションのパラメータを使用するか、デフォルトを維持するかのどちらかを選択できます。

ここで、質問が必要な場合や必要な場合にのみ、ここにオプションのパラメータを挿入する方法はありますか?最初に関数にキーパラメータを格納しないでください。代わりに関数を呼び出すときに常にすべてのキーと値の要素を解析しますか?誰かがこれを達成するためのいくつかの異なる方法について私に知らせてくれるように親切な人ですか?

誰かが私の考えについてのさらなる情報を必要とする場合は、私に知らせてください。

+4

あなたは 'def function(** kwargs)'の構文に精通していますか? –

+0

@パトリックちょっと、あなたはすべてのデータとキーをkwargs **を使って転送する必要がありますか?これを落とすべき理由はないと思うが、大丈夫だ。 – Marius

+0

あなたがやろうとしていることはかなり曖昧です。あなたからの詳細がなくても、具体的な答えがたくさんあるわけではありません。 –

答えて

0

この回答はPythonのチャットルームで@Antti Haapalaによって提供されました。

ポストリクエスト自体に静的データを残しても問題ありません。リクエストにキーワード引数(** kwargs)を追加するには、Python 2.7で2つの方法があります。ポストに送る前にデータをマージするかそのような要求:

def search(self, query, **kwargs): 
    """ 
    Executes a searchquery, that is then stored and needs 
    to be called again to get results, using the returned 
    search_id. 
    :param query: Query to be run with the search 
    :return: Array of the current searchid, which is needed 
      for other functions, and the content of HTTP response. 
    """ 
    search_id = int(round(time.time() * 1000)) 
    data ={ 
     'query': query, 
     'search_session_id': search_id, 
     'user_session_id': self.token, 
    } 
    data.update(kwargs) 
    response = self._post('/server/search', data) 
    return search_id, response.json() 

または辞書にデータを変更することで、** kwarg値追加する前に:のpython 3では

def search(self, query, **kwargs): 
    """ 
    Executes a searchquery, that is then stored and needs 
    to be called again to get results, using the returned 
    search_id. 
    :param query: Query to be run with the search 
    :return: Array of the current searchid, which is needed 
      for other functions, and the content of HTTP response. 
    """ 
    search_id = int(round(time.time() * 1000)) 
    if len(args) > 0: 
     response = self._post(
      '/server/search', data=dict(
       query=query, 
       search_session_id=search_id, 
       user_session_id=self.token, 
       **kwargs 
      )) 
    return search_id, response.json() 

を、あなたが直接それを追加することができるだろう、代わりにマージ、または変数をdictに変更することができます。

関連する問題