![]() |
API Calls for my app - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: API Calls for my app (/thread-39377.html) |
API Calls for my app - newby77 - Feb-08-2023 Hi everyone, I'm looking for API calls to fetch information for my EV Charging App and how to merge them into the app code. My code has dummy entries and I need to swap them to relevant API Thank you so much import UIKit import CoreLocation class ViewController: UIViewController { let locationManager = CLLocationManager() let networkRequest = NetworkRequest() let chargerDataSource = ChargerDataSource() override func viewDidLoad() { super.viewDidLoad() // Request location authorization from the user locationManager.requestWhenInUseAuthorization() locationManager.delegate = self // Start updating the user's location locationManager.startUpdatingLocation() } } extension ViewController: CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { // Get the latest user location let userLocation = locations.last // Use the user's location to find EV charger locations near them networkRequest.fetchChargerLocations(near: userLocation) { [weak self] result in switch result { case .success(let chargerLocations): self?.chargerDataSource.update(with: chargerLocations) case .failure(let error): print("Failed to fetch charger locations: \(error)") } } } } class ChargerDataSource { private var chargerLocations: [ChargerLocation] = [] func update(with chargerLocations: [ChargerLocation]) { self.chargerLocations = chargerLocations } func charger(at index: Int) -> ChargerLocation { return chargerLocations[index] } } struct ChargerLocation { let location: CLLocation let type: ChargerType let isInUse: Bool } enum ChargerType { case level1 case level2 case level3 } class NetworkRequest { func fetchChargerLocations(near location: CLLocation?, completion: @escaping (Result<[ChargerLocation], Error>) -> Void) { // Replace this with a network request to an API that returns EV charger locations let chargerLocations = [ ChargerLocation(location: CLLocation(), type: .level2, isInUse: false), ChargerLocation(location: CLLocation(), type: .level3, isInUse: true), ] completion(.success(chargerLocations)) } } RE: API Calls for my app - buran - Feb-08-2023 Well, this is not python code (dummy or not). Do you have question(s) about python/python code you wrote? RE: API Calls for my app - newby77 - Feb-08-2023 (Feb-08-2023, 08:42 AM)buran Wrote: Well, this is not python code (dummy or not). Do you have question(s) about python/python code you wrote? Here the Python code i have, apologies for the previous one import requests import json class NetworkRequest: def fetch_charger_locations(self, user_location): # Replace this with a real API call that returns the charger locations # near the user's location response = requests.get('https://api.example.com/charger_locations', params={'latitude': user_location.latitude, 'longitude': user_location.longitude}) if response.status_code != 200: raise Exception('Failed to fetch charger locations') return json.loads(response.text) class ChargerDataSource: def __init__(self): self.charger_locations = [] def update(self, charger_locations): self.charger_locations = charger_locations def charger_at_index(self, index): return self.charger_locations[index] class ChargerLocation: def __init__(self, location, type, is_in_use): self.location = location self.type = type self.is_in_use = is_in_use class ViewController: def __init__(self): self.location_manager = LocationManager() self.network_request = NetworkRequest() self.charger_data_source = ChargerDataSource() def view_did_load(self): self.location_manager.request_when_in_use_authorization() self.location_manager.delegate = self self.location_manager.start_updating_location() def location_manager_did_update_locations(self, locations): user_location = locations[-1] charger_locations = self.network_request.fetch_charger_locations(user_location) self.charger_data_source.update(charger_locations) class LocationManager: def request_when_in_use_authorization(self): # Implement the code to request location authorization from the user pass def start_updating_location(self): # Implement the code to start updating the user's location pass RE: API Calls for my app - buran - Feb-08-2023 I would say this very much looks like [homework] assignment RE: API Calls for my app - noisefloor - Feb-08-2023 Hallo, there's certainly a misuse of classes in the code. Python is not Java - you are not enforced to use classes. And classes with one method only or even no methods at all can be a function, respectively a suitable data structure. NetworkRequest has one method only and no attributes -> write as a function. ChargerDataSource is an completely unnessary class, this can be simple done by a list and the methods of a list. ChargerLocaction can be either a dict or a DataClass. Except this, type is a build-in Python function. Yes, you can name your variable type , but you over-write the build-in function, which can lead to all types of errors or weird behavious of your code.Regards, noisefloor |