Simplify Your REST API Calls with CC Rest Api in Dart

Ferhat Erdem
3 min readJun 19, 2024

--

Hey there, Dart enthusiasts!

Are you tired of dealing with the complexities of REST API calls in your Dart applications? Do you wish there was a more structured and straightforward way to handle HTTP requests and responses? Look no further! I’m thrilled to introduce you to CC Rest Api, a Dart package designed to make your life easier by transforming your REST API interactions into a more organized and manageable class structure.

In this article, I’ll walk you through everything you need to know about CC Rest Api, from initialization to creating and using modules. Let’s dive in!

Why CC Rest Api?

Before we get into the nitty-gritty, let’s talk about why you might want to use CC Rest Api. The biggest goal behind developing this package was to shift the entire architecture from traditional method-based API interactions to a class-based structure. This not only simplifies the code but also reduces complexity, making it easier to maintain and extend.

CC Rest Api currently supports GET, POST, and DELETE operations, covering the most common needs for API communication.

Getting Started

Installation

First things first, let’s get CC Rest Api into your project. You can easily add it via pub.dev. Just add the following line to your pubspec.yaml file:

dependencies:
cc_rest_api: ^1.0.0

Then run flutter pub get to install the package.

Initialization

Before you can start using CC Rest Api, you need to initialize it with your REST API configurations and logging options. Here’s an example of how you can set it up:

import 'package:cc_rest_api/cc_rest_api.dart';

void main() {
CCRestApi.init(
restOptions: CCRestOptions(
baseUrl: "httpbin.org",
defaultHeaders: {
"Access-Control-Allow-Origin": "*",
"Accept": "*",
"Content-Type": "application/json",
},
),
loggingOptions: CCRestLogging(
logEnabled: true,
onRequest: (handler) => print("Request: $handler"),
onResponse: (handler) => print("Response: $handler"),
onError: (handler) => print("Error: $handler"),
),
modules: [
GetUser(const CCApiConfig("user/get", RequestType.GET, NetworkType.HTTPS)),
// Other modules can be added here
],
);
}

In this example, we’re setting the base URL, default headers, and logging options. Logging is incredibly useful for debugging, as it allows you to see the details of each request and response directly in your console.

Creating Modules

The real power of CC Rest Api comes from its modular approach. Each API operation can be encapsulated in its own module, making it easy to manage and reuse.

Here’s how you can create a module:

class GetUser extends CCApiModule {
GetUser(CCApiConfig config) : super(config);

@override
Future<Map<String, dynamic>> request() async {
return await super.request();
}

@override
response(dynamic data) {
// Processing logic for the received data can go here
}
}

Or, if you don’t need to customize the request method, you can keep it even simpler:

class GetUser extends CCApiModule {
GetUser(CCApiConfig config) : super(config);

@override
response(dynamic data) {
// Processing logic for the received data can go here
}
}

Using Modules

Once you have your modules set up, making API requests is a breeze. Here’s an example of how to use the GetUser module:

import 'package:cc_rest_api/cc_rest_api.dart';

void main() {
GetUser getUser = CCRestApi.getModule<GetUser>();
getUser.setHeaders({
"Authorization": "Bearer your_access_token",
});
getUser.setParameters({
"param1": "value1",
"param2": "value2",
});
getUser.setBody({
"firebaseToken": "testFT",
"user_id": "test",
});

getUser.request(); //It triggers request. It can return the response value.
}

In this example, we create an instance of the GetUser module and set headers, parameters, and body as needed. Then, we call the request() method to trigger the API call. Easy, right?

Conclusion

CC Rest Api is designed to make your life as a Dart developer easier by providing a structured and flexible way to handle REST API interactions. By using this package, you can reduce code complexity, improve maintainability, and focus more on building amazing features for your app.

I hope this article has given you a clear understanding of how to get started with CC Rest Api. Give it a try, and let me know how it works for you! If you have any questions or feedback, feel free to leave a comment below. Happy coding!

--

--

Ferhat Erdem
Ferhat Erdem

Written by Ferhat Erdem

I have been actively working in the software industry for approximately seven years.

No responses yet