I working at grabbing a comprehensive list of all my models and their costs (
material basePrice), so that I can run it through my product configuration database which will calculate the wholesale and retail prices of the finished products based on the
basePrice returned by the API. I have been able to script the appropriate calls to get the required information (see code below), however; it takes a considerable amount of time due to the number of API calls required.
Code:
import requests
class Product:
def __init__(self, id, name, material, price):
self.id = id
self.name = name
self.material = material
self.price = price
def getAccessToken(cliendId,clientSecret):
api_url = 'https://api.shapeways.com/oauth2/token'
post_data = {
'grant_type': 'client_credentials'
}
response = requests.post(url=api_url, data=post_data, auth=(cliendId,cliendSecret))
return response.json()['access_token']
def getHeaders(accessToken):
headers = {
'Authorization': 'Bearer ' + accessToken
}
return headers
def getModelIds(clientId,clientSecret=''):
if clientSecret == '':
accessToken = clientId
else:
accessToken = getAccessToken(clientId, clientSecret)
page = 1
modelIds = []
while True:
api_url = 'https://api.shapeways.com/models/v1?page=' + str(page)
response = requests.get(url=api_url, headers=getHeaders(accessToken))
if response.json()['models'] == None:
break
for model in response.json()['models']:
modelIds.append(model['modelId'])
page +=1
return modelIds
def getProduct(response,modelId,material):
modelName = response.json()['title']
if ':' in material['name']:
mat, ot = material['name'].split(':')
else:
mat = material['name']
return Product(modelId,modelName,mat,material['basePrice'])
def getModelDetail(clientId,clientSecret='',modelIds=[])
if clientSecret == '':
accessToken = clientId
else:
accessToken = getAccessToken(clientId,clientSecret)
if modelIds == []:
modelIds = getModelIds(accessToken)
modelDetail = []
for modelId in modelIds:
api_url = 'https://api.shapeways.com/models/' + modelId + '/v1'
response = requests.get(url=api_url +'?', headers = getHeaders(accessToken))
modelName = response.json()['title']
materials = response.json()['materials']
for material in materials:
modelDetail.append(getProduct(response,modelId,materials[material]))
returl model Detail
As you can see, I have a
class called
Product which holds the
id, name, material and
price of a given
Product. Calling
getModelDetail returns an array of
Product items. One
Product for each
model/
material pair in the supplied list of
modelIds. Like I mentioned, this works as-is, but takes a considerable amount of time. Is there a more efficient way to achieve the same result? Do I really need a new API request for each page of
models? Do I absolutely need a new API request for each and every single model? Is there a method I am missing which would reduce the number of API requests, and thus reduce the run-time of the script?