2016-07-10 5 views
-1

ライセンスプレート認識のAPIを使用しています。私はこのカールコマンドを取得します:CURLを使用したCALLとPythonによるAPI

PYTHONのカールでそのような呼び出しを実装する方法は?

curl "https://api.havenondemand.com/1/api/async/recognizelicenseplates/v1?url=https%3A%2F%2Fwww.havenondemand.com%2Fsample-content%2Fvideos%2Fgb-plates.mp4&source_location=GB&apikey=695e513c-xxxx-xxxx-xxxx-xxxxxxxxxx" 

curl -X POST --form "url=https://www.havenondemand.com/sample-content/videos/gb-plates.mp4" --form "source_location=GB" --form "apikey=695e513c-xxxx-xxxx-a666-xxxxxxxxxx" https://api.havenondemand.com/1/api/async/recognizelicenseplates/v1 

答えて

0

Pythonでは、requestsモジュールを使用する方がはるかに良い選択肢です。それを最初にインストールします。

pip install requests 

は次に、この操作を行います。

import requests 

API_URL = "https://api.havenondemand.com/1/api/async/recognizelicenseplates/v1" 

data = { 
    "url": "https://www.havenondemand.com/sample-content/videos/gb-plates.mp4", 
    "source_location": "GB", 
    "apikey": "695e513c-xxxx-xxxx-a666-xxxxxxxxxx" 
} 

response = requests.post(API_URL, data) 
print(response.json()) 

基本的には、任意のフォームフィールドは、キーと値のペアとしてdata辞書の内側に行く必要があります。ここではrequests.post()関数を使用します。この関数は、ターゲットURLを最初のパラメータとして使用します。そして、フォームの値は2番目のパラメータとなります。

応答オブジェクトが返されます。生応答は、response.contentの値を出力することで確認できます。しかし、応答がJSONであることがわかっている場合は、json()メソッドを使用してレスポンスを解析し、Pythonデータ型(ディクショナリ)を取得できます。

+0

おかげで多くのことを、havenondemandインストールしてインストールします。 :) – rohit

0

複数のオプションがあります。 urllib2(またはリクエストなどの他のHTTPライブラリ)から始めることができます。より良い選択肢は、Pythonクライアントライブラリを直接使用することですhavenondemand

0

Haven OnDemand APIを呼び出す最も速い方法は、HPE公式ライブラリを使用することです。あなたはHODのPythonのlibをインストールし、次のようにそれを使用することができます。

libに

ピップは、この作品

# put these in your file 
from havenondemand.hodclient import * 
from havenondemand.hodresponseparser import * 

client = HODClient("API_KEY", version="v1") 
parser = HODResponseParser() 

# callback function 
def asyncRequestCompleted(response): 
    jobID = parser.parse_jobid(response) 
    if jobID is None: 
     errorObj = hodParser.get_last_error() 
     for err in errorObj.errors: 
      print ("Error code: %d \nReason: %s \nDetails: %s\n" % (err.error,err.reason, err.detail)) 
    else: 
     client.get_job_status(jobID, requestCompleted) 

def requestCompleted(response): 
    payloadObj = parser.parse_payload(response) 
    resp = "" 
    if payloadObj is None: 
     errorObj = parser.get_last_error() 
     for err in errorObj.errors: 
      if err.error == ErrorCode.QUEUED: 
       time.sleep(2) 
       client.get_job_status(err.jobID, requestCompleted) 
       return 
      elif err.error == ErrorCode.IN_PROGRESS: 
       time.sleep(10) 
       client.get_job_status(err.jobID, requestCompleted) 
       return 
      else: 
       resp += "Error code: %d \nReason: %s \nDetails: %s\n" % (err.error,err.reason) 
    else: 
     print(payloadObj) 

params = {} 
params['url'] = 'https://www.havenondemand.com/sample-content/videos/gb-plates.mp4' 
params['source_location'] = 'GB' 
hodApp = HODApps.RECOGNIZE_LICENSE_PLATE 
client.post_request(params, hodApp, True, callback=asyncRequestCompleted) 
関連する問題