-
Notifications
You must be signed in to change notification settings - Fork 2
Description
So in preparation for the API, I was thinking we could create a class with a bunch of static methods that perform requests to our hypothetically running API. Making sure you're on the right branch, add the http library to your project w/ flutter pub add http.
I think we could create a new folder in features called something like api or requests, whichever you think fits most. In it we can create a file called api_client.dart with a single class APIClient.
We don't have the URL for the API set in stone, nor the endpoints, so I think the best solution is to create several static variables like static const _baseURL = "example.com"; and static const _semestersEndpoint = "/semesters. Honestly those should be enough in terms of constants.
Next just create static methods that perform get requests and return the response body. http is an asynchronous library so you have to mark the function as async (i.e. can run concurrently). Here's how:
static Future<String> getSemesters() async {
// code for performing get request and returning responses
// just return a normal string, Future means this function may complete later (hence asynchronous)
// read more: https://dart.dev/libraries/async/async-await
}Import the library at the top of the file w/ import 'package:http/http.dart' as http;. You can create URL (Uri) objects w/ Uri.https(_baseURL, <endpoint>);. Check out slide 16 in last week's meeting to see some endpoints we plan to have.
For endpoints with dynamic values (e.g. /fall25/csc) you should probably accept them as a parameter in the function and pass them into a string (something like Uri.parse(_baseURL, "/$semester/$course")).
Call await http.get(<uri>) to perform the GET request (make sure to assign this to a variable). The function will return a Response object. Simply return the body of the response and we'll work on handling that with other classes later.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status