'How to call an API constantly with a function?
I have a customer management system. I am using an API to get all of the customers. It works perfectly. I am using an ID, if there is no customer who has this ID, then it creates a new customer. Here is my code for that:
def handle_get_customers(company):
customer_service = requests.get(settings.API_ADDRESS + "customer/get/all",
headers={'Authorization': "Bearer " + token_key}).json()
service_customers = []
for customer in customer_service["data"]:
service_customers.append(customer)
for person in service_customers:
new_customer, obj = Customer.objects.get_or_create(api_id=person["id"])
new_customer.customer_name = person["name"]
country, d = Country.objects.get_or_create(country_name=person["countryName"])
new_customer.country = country
new_customer.address = person["fullAddress"]
new_customer.phone_number = person["phoneNumber"]
new_customer.email_address = person["email"]
new_customer.currency_choice = person["currency"]
new_customer.api_id = person["id"]
new_customer.company = company
new_customer.save()
return service_customers
And I am listing all customers on a page. It has a basic listing function like;
def customer_list(request):
current_user = request.user
handle_get_customers(current_user.company)
customer_list = Customer.objects.filter(company=current_user.company)
context = {
'customer_list': customer_list,
}
return render(request, 'customer_list.html', context)
I want to get all new companies without refreshing the page. I don't know if I can use Ajax for that but I guess the handle_get_customers function should work constantly but I cannot figure out how can I do it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
