问题
I am trying to use the Stava API in a Flask project. I have seen the following stackoverflow
and installed swagger_client
swagger-codegen generate -i https://developers.strava.com/swagger/swagger.json -l python -o ./StravaPythonClient
as per their instructions. However when i run the app i still get import swagger_client
ModuleNotFoundError: No module named 'swagger_client'
My code is here
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint
# Configure OAuth2 access token for authorization: strava_oauth
swagger_client.configuration.access_token = 'fe931c21b503a46b61b1000000000000000000000'
# create an instance of the API class
api_instance = swagger_client.StreamsApi()
id = 2284367626 # Long | The identifier of the activity.
#keys = # array[String] | Desired stream types.
keyByType = true # Boolean | Must be true. (default to true)
try:
# Get Activity Streams
api_response = api_instance.getActivityStreams(id, keys, keyByType)
pprint(api_response)
except ApiException as e:
print("Exception when calling StreamsApi->getActivityStreams: %s\n" % e)
not sure what packages i should be installing to get this working now.
回答1:
First install swagger-codegen and check that it's working, this example is for linux. Easier with mac where you can use homebrew.
wget http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.7/swagger-codegen-cli-2.4.7.jar -O swagger-codegen-cli.jar
java -jar swagger-codegen-cli.jar help
After that go in your project and generate the swagger-client. The code below tells that it's for python and should be stored in a folder within the project called generated
java -jar swagger-codegen-cli.jar generate -i https://developers.strava.com/swagger/swagger.json -l python -o generated
Go into the generated folder and install the requirements
cd generated && python setup.py install --user && cd ..
Change your import statements to refer to the generated folder.
from generated import swagger_client
from generated.swagger_client.rest import ApiException
from pprint import pprint
# Configure OAuth2 access token for authorization: strava_oauth
swagger_client.Configuration.access_token = 'fe931c21b503a46b61b1000000000000000000000'
# create an instance of the API class
api_instance = swagger_client.StreamsApi()
id = 2284367626 # Long | The identifier of the activity.
#keys = # array[String] | Desired stream types.
keyByType = true # Boolean | Must be true. (default to true)
try:
# Get Activity Streams
api_response = api_instance.getActivityStreams(id, keys, keyByType)
pprint(api_response)
except ApiException as e:
print("Exception when calling StreamsApi->getActivityStreams: %s\n" % e)
Now you can run the file. Ps when you set the access token: configuration needs to be written with upper case C.
来源:https://stackoverflow.com/questions/55657275/swagger-client-in-python-trying-to-use-strava-api