diff --git a/custom_packages/network/.gitignore b/custom_packages/network/.gitignore new file mode 100644 index 0000000..96486fd --- /dev/null +++ b/custom_packages/network/.gitignore @@ -0,0 +1,30 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +.packages +build/ diff --git a/custom_packages/network/.metadata b/custom_packages/network/.metadata new file mode 100644 index 0000000..9596fae --- /dev/null +++ b/custom_packages/network/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: 796c8ef79279f9c774545b3771238c3098dbefab + channel: stable + +project_type: package diff --git a/custom_packages/network/CHANGELOG.md b/custom_packages/network/CHANGELOG.md new file mode 100644 index 0000000..41cc7d8 --- /dev/null +++ b/custom_packages/network/CHANGELOG.md @@ -0,0 +1,3 @@ +## 0.0.1 + +* TODO: Describe initial release. diff --git a/custom_packages/network/LICENSE b/custom_packages/network/LICENSE new file mode 100644 index 0000000..ba75c69 --- /dev/null +++ b/custom_packages/network/LICENSE @@ -0,0 +1 @@ +TODO: Add your license here. diff --git a/custom_packages/network/README.md b/custom_packages/network/README.md new file mode 100644 index 0000000..02fe8ec --- /dev/null +++ b/custom_packages/network/README.md @@ -0,0 +1,39 @@ + + +TODO: Put a short description of the package here that helps potential users +know whether this package might be useful for them. + +## Features + +TODO: List what your package can do. Maybe include images, gifs, or videos. + +## Getting started + +TODO: List prerequisites and provide or point to information on how to +start using the package. + +## Usage + +TODO: Include short and useful examples for package users. Add longer examples +to `/example` folder. + +```dart +const like = 'sample'; +``` + +## Additional information + +TODO: Tell users more about the package: where to find more information, how to +contribute to the package, how to file issues, what response they can expect +from the package authors, and more. diff --git a/custom_packages/network/analysis_options.yaml b/custom_packages/network/analysis_options.yaml new file mode 100644 index 0000000..a5744c1 --- /dev/null +++ b/custom_packages/network/analysis_options.yaml @@ -0,0 +1,4 @@ +include: package:flutter_lints/flutter.yaml + +# Additional information about this file can be found at +# https://dart.dev/guides/language/analysis-options diff --git a/custom_packages/network/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java b/custom_packages/network/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java new file mode 100644 index 0000000..d007606 --- /dev/null +++ b/custom_packages/network/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java @@ -0,0 +1,23 @@ +package io.flutter.plugins; + +import io.flutter.plugin.common.PluginRegistry; + +/** + * Generated file. Do not edit. + */ +public final class GeneratedPluginRegistrant { + public static void registerWith(PluginRegistry registry) { + if (alreadyRegisteredWith(registry)) { + return; + } + } + + private static boolean alreadyRegisteredWith(PluginRegistry registry) { + final String key = GeneratedPluginRegistrant.class.getCanonicalName(); + if (registry.hasPlugin(key)) { + return true; + } + registry.registrarFor(key); + return false; + } +} diff --git a/custom_packages/network/ios/Flutter/Generated.xcconfig b/custom_packages/network/ios/Flutter/Generated.xcconfig new file mode 100644 index 0000000..7d18b1b --- /dev/null +++ b/custom_packages/network/ios/Flutter/Generated.xcconfig @@ -0,0 +1,14 @@ +// This is a generated file; do not edit or check into version control. +FLUTTER_ROOT=/Users/trunghvbk/Code/tools/flutter +FLUTTER_APPLICATION_PATH=/Users/trunghvbk/Code/individual/flutter_app/packages/network +COCOAPODS_PARALLEL_CODE_SIGN=true +FLUTTER_TARGET=lib/main.dart +FLUTTER_BUILD_DIR=build +FLUTTER_BUILD_NAME=0.0.1 +FLUTTER_BUILD_NUMBER=0.0.1 +EXCLUDED_ARCHS[sdk=iphonesimulator*]=i386 +EXCLUDED_ARCHS[sdk=iphoneos*]=armv7 +DART_OBFUSCATION=false +TRACK_WIDGET_CREATION=true +TREE_SHAKE_ICONS=false +PACKAGE_CONFIG=.dart_tool/package_config.json diff --git a/custom_packages/network/ios/Flutter/flutter_export_environment.sh b/custom_packages/network/ios/Flutter/flutter_export_environment.sh new file mode 100755 index 0000000..3a83be7 --- /dev/null +++ b/custom_packages/network/ios/Flutter/flutter_export_environment.sh @@ -0,0 +1,13 @@ +#!/bin/sh +# This is a generated file; do not edit or check into version control. +export "FLUTTER_ROOT=/Users/trunghvbk/Code/tools/flutter" +export "FLUTTER_APPLICATION_PATH=/Users/trunghvbk/Code/individual/flutter_app/packages/network" +export "COCOAPODS_PARALLEL_CODE_SIGN=true" +export "FLUTTER_TARGET=lib/main.dart" +export "FLUTTER_BUILD_DIR=build" +export "FLUTTER_BUILD_NAME=0.0.1" +export "FLUTTER_BUILD_NUMBER=0.0.1" +export "DART_OBFUSCATION=false" +export "TRACK_WIDGET_CREATION=true" +export "TREE_SHAKE_ICONS=false" +export "PACKAGE_CONFIG=.dart_tool/package_config.json" diff --git a/custom_packages/network/ios/Runner/GeneratedPluginRegistrant.h b/custom_packages/network/ios/Runner/GeneratedPluginRegistrant.h new file mode 100644 index 0000000..7a89092 --- /dev/null +++ b/custom_packages/network/ios/Runner/GeneratedPluginRegistrant.h @@ -0,0 +1,19 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#ifndef GeneratedPluginRegistrant_h +#define GeneratedPluginRegistrant_h + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface GeneratedPluginRegistrant : NSObject ++ (void)registerWithRegistry:(NSObject*)registry; +@end + +NS_ASSUME_NONNULL_END +#endif /* GeneratedPluginRegistrant_h */ diff --git a/custom_packages/network/ios/Runner/GeneratedPluginRegistrant.m b/custom_packages/network/ios/Runner/GeneratedPluginRegistrant.m new file mode 100644 index 0000000..efe65ec --- /dev/null +++ b/custom_packages/network/ios/Runner/GeneratedPluginRegistrant.m @@ -0,0 +1,14 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#import "GeneratedPluginRegistrant.h" + +@implementation GeneratedPluginRegistrant + ++ (void)registerWithRegistry:(NSObject*)registry { +} + +@end diff --git a/custom_packages/network/lib/exception.dart b/custom_packages/network/lib/exception.dart new file mode 100644 index 0000000..325932d --- /dev/null +++ b/custom_packages/network/lib/exception.dart @@ -0,0 +1,24 @@ +sealed class APIException implements Exception { + APIException(this.message); + final String message; +} + +class NoInternetConnectionException extends APIException { + NoInternetConnectionException() : super('No Internet connection'); +} + +class InvalidRequestException extends APIException { + InvalidRequestException() : super('The request is not valid'); +} + +class UnauthorizedException extends APIException { + UnauthorizedException() : super('Unauthorized'); +} + +class NotFoundException extends APIException { + NotFoundException() : super('Not found 404'); +} + +class UnknownException extends APIException { + UnknownException() : super('Some error occurred'); +} diff --git a/custom_packages/network/lib/network.dart b/custom_packages/network/lib/network.dart new file mode 100644 index 0000000..49eb1a5 --- /dev/null +++ b/custom_packages/network/lib/network.dart @@ -0,0 +1,6 @@ +library network; + +export './exception.dart'; +export './providers.dart'; +export './repository.dart'; +export './request.dart'; diff --git a/custom_packages/network/lib/providers.dart b/custom_packages/network/lib/providers.dart new file mode 100644 index 0000000..0a52154 --- /dev/null +++ b/custom_packages/network/lib/providers.dart @@ -0,0 +1,6 @@ +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:network/repository.dart'; + +final apiRepoProvider = Provider((ref) { + return APIRepository(); +}); diff --git a/custom_packages/network/lib/repository.dart b/custom_packages/network/lib/repository.dart new file mode 100644 index 0000000..800783e --- /dev/null +++ b/custom_packages/network/lib/repository.dart @@ -0,0 +1,59 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:http/http.dart' as http; +import 'package:network/exception.dart'; + +import 'request.dart'; + +class APIRepository { + static final APIRepository _singleton = APIRepository._internal(); + factory APIRepository() { + return _singleton; + } + APIRepository._internal(); + + http.Client client = http.Client(); + + Future excute({ + required Request request, + required T Function(dynamic data) builder, + }) async { + try { + http.Response? response; + Uri uri = request.uri; + final body = jsonEncode(request.parameters); + if (request.method == HttpMethod.get) { + response = await client.get(uri, headers: request.headers); + } else if (request.method == HttpMethod.post) { + response = await client.post(uri, + headers: request.headers, body: body, encoding: utf8); + } else if (request.method == HttpMethod.put) { + response = await client.put(uri, + headers: request.headers, body: body, encoding: utf8); + } else if (request.method == HttpMethod.patch) { + response = await client.patch(uri, + headers: request.headers, body: body, encoding: utf8); + } else if (request.method == HttpMethod.delete) { + response = await client.delete(uri, + headers: request.headers, body: body, encoding: utf8); + } + if (response == null) { + throw InvalidRequestException(); + } + switch (response.statusCode) { + case 200: + final data = json.decode(response.body); + return builder(data); + case 401: + throw UnauthorizedException(); + case 404: + throw NotFoundException(); + default: + throw UnknownException(); + } + } on SocketException catch (_) { + throw NoInternetConnectionException(); + } + } +} diff --git a/custom_packages/network/lib/request.dart b/custom_packages/network/lib/request.dart new file mode 100644 index 0000000..cbc1533 --- /dev/null +++ b/custom_packages/network/lib/request.dart @@ -0,0 +1,29 @@ +enum HttpMethod { get, post, put, patch, delete } + +class Request { + String host; + String path; + String endPoint; + HttpMethod method; + Map headers; + + Map get parameters { + return {}; + } + + Uri get uri { + return Uri( + scheme: "https", + host: host, + path: "$path$endPoint", + queryParameters: method == HttpMethod.get ? parameters : {}, + ); + } + + Request( + {required this.host, + required this.path, + required this.endPoint, + this.method = HttpMethod.get, + this.headers = const {}}); +} diff --git a/custom_packages/network/linux/flutter/generated_plugin_registrant.cc b/custom_packages/network/linux/flutter/generated_plugin_registrant.cc new file mode 100644 index 0000000..e71a16d --- /dev/null +++ b/custom_packages/network/linux/flutter/generated_plugin_registrant.cc @@ -0,0 +1,11 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#include "generated_plugin_registrant.h" + + +void fl_register_plugins(FlPluginRegistry* registry) { +} diff --git a/custom_packages/network/linux/flutter/generated_plugin_registrant.h b/custom_packages/network/linux/flutter/generated_plugin_registrant.h new file mode 100644 index 0000000..e0f0a47 --- /dev/null +++ b/custom_packages/network/linux/flutter/generated_plugin_registrant.h @@ -0,0 +1,15 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#ifndef GENERATED_PLUGIN_REGISTRANT_ +#define GENERATED_PLUGIN_REGISTRANT_ + +#include + +// Registers Flutter plugins. +void fl_register_plugins(FlPluginRegistry* registry); + +#endif // GENERATED_PLUGIN_REGISTRANT_ diff --git a/custom_packages/network/linux/flutter/generated_plugins.cmake b/custom_packages/network/linux/flutter/generated_plugins.cmake new file mode 100644 index 0000000..2e1de87 --- /dev/null +++ b/custom_packages/network/linux/flutter/generated_plugins.cmake @@ -0,0 +1,23 @@ +# +# Generated file, do not edit. +# + +list(APPEND FLUTTER_PLUGIN_LIST +) + +list(APPEND FLUTTER_FFI_PLUGIN_LIST +) + +set(PLUGIN_BUNDLED_LIBRARIES) + +foreach(plugin ${FLUTTER_PLUGIN_LIST}) + add_subdirectory(flutter/ephemeral/.plugin_symlinks/${plugin}/linux plugins/${plugin}) + target_link_libraries(${BINARY_NAME} PRIVATE ${plugin}_plugin) + list(APPEND PLUGIN_BUNDLED_LIBRARIES $) + list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries}) +endforeach(plugin) + +foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST}) + add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/linux plugins/${ffi_plugin}) + list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries}) +endforeach(ffi_plugin) diff --git a/custom_packages/network/macos/Flutter/GeneratedPluginRegistrant.swift b/custom_packages/network/macos/Flutter/GeneratedPluginRegistrant.swift new file mode 100644 index 0000000..cccf817 --- /dev/null +++ b/custom_packages/network/macos/Flutter/GeneratedPluginRegistrant.swift @@ -0,0 +1,10 @@ +// +// Generated file. Do not edit. +// + +import FlutterMacOS +import Foundation + + +func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { +} diff --git a/custom_packages/network/macos/Flutter/ephemeral/Flutter-Generated.xcconfig b/custom_packages/network/macos/Flutter/ephemeral/Flutter-Generated.xcconfig new file mode 100644 index 0000000..4f2dd1a --- /dev/null +++ b/custom_packages/network/macos/Flutter/ephemeral/Flutter-Generated.xcconfig @@ -0,0 +1,11 @@ +// This is a generated file; do not edit or check into version control. +FLUTTER_ROOT=/Users/trunghvbk/Code/tools/flutter +FLUTTER_APPLICATION_PATH=/Users/trunghvbk/Code/individual/flutter_app/packages/network +COCOAPODS_PARALLEL_CODE_SIGN=true +FLUTTER_BUILD_DIR=build +FLUTTER_BUILD_NAME=0.0.1 +FLUTTER_BUILD_NUMBER=0.0.1 +DART_OBFUSCATION=false +TRACK_WIDGET_CREATION=true +TREE_SHAKE_ICONS=false +PACKAGE_CONFIG=.dart_tool/package_config.json diff --git a/custom_packages/network/macos/Flutter/ephemeral/flutter_export_environment.sh b/custom_packages/network/macos/Flutter/ephemeral/flutter_export_environment.sh new file mode 100755 index 0000000..46bb9b8 --- /dev/null +++ b/custom_packages/network/macos/Flutter/ephemeral/flutter_export_environment.sh @@ -0,0 +1,12 @@ +#!/bin/sh +# This is a generated file; do not edit or check into version control. +export "FLUTTER_ROOT=/Users/trunghvbk/Code/tools/flutter" +export "FLUTTER_APPLICATION_PATH=/Users/trunghvbk/Code/individual/flutter_app/packages/network" +export "COCOAPODS_PARALLEL_CODE_SIGN=true" +export "FLUTTER_BUILD_DIR=build" +export "FLUTTER_BUILD_NAME=0.0.1" +export "FLUTTER_BUILD_NUMBER=0.0.1" +export "DART_OBFUSCATION=false" +export "TRACK_WIDGET_CREATION=true" +export "TREE_SHAKE_ICONS=false" +export "PACKAGE_CONFIG=.dart_tool/package_config.json" diff --git a/custom_packages/network/pubspec.yaml b/custom_packages/network/pubspec.yaml new file mode 100644 index 0000000..7129ae8 --- /dev/null +++ b/custom_packages/network/pubspec.yaml @@ -0,0 +1,21 @@ +name: network +description: A new Flutter package project. +version: 0.0.1 +homepage: pub.dev + +environment: + sdk: '>=3.0.5 <4.0.0' + flutter: ">=1.17.0" + +dependencies: + flutter: + sdk: flutter + flutter_riverpod: ^2.3.6 + http: ^0.13.0 + +dev_dependencies: + flutter_test: + sdk: flutter + flutter_lints: ^2.0.0 + +flutter: diff --git a/custom_packages/network/test/network_test.dart b/custom_packages/network/test/network_test.dart new file mode 100644 index 0000000..9365eb3 --- /dev/null +++ b/custom_packages/network/test/network_test.dart @@ -0,0 +1,6 @@ +import 'package:flutter_test/flutter_test.dart'; + +import 'package:network/network.dart'; + +// TODO: Write tests +void main() {} diff --git a/custom_packages/network/windows/flutter/generated_plugin_registrant.cc b/custom_packages/network/windows/flutter/generated_plugin_registrant.cc new file mode 100644 index 0000000..8b6d468 --- /dev/null +++ b/custom_packages/network/windows/flutter/generated_plugin_registrant.cc @@ -0,0 +1,11 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#include "generated_plugin_registrant.h" + + +void RegisterPlugins(flutter::PluginRegistry* registry) { +} diff --git a/custom_packages/network/windows/flutter/generated_plugin_registrant.h b/custom_packages/network/windows/flutter/generated_plugin_registrant.h new file mode 100644 index 0000000..dc139d8 --- /dev/null +++ b/custom_packages/network/windows/flutter/generated_plugin_registrant.h @@ -0,0 +1,15 @@ +// +// Generated file. Do not edit. +// + +// clang-format off + +#ifndef GENERATED_PLUGIN_REGISTRANT_ +#define GENERATED_PLUGIN_REGISTRANT_ + +#include + +// Registers Flutter plugins. +void RegisterPlugins(flutter::PluginRegistry* registry); + +#endif // GENERATED_PLUGIN_REGISTRANT_ diff --git a/custom_packages/network/windows/flutter/generated_plugins.cmake b/custom_packages/network/windows/flutter/generated_plugins.cmake new file mode 100644 index 0000000..b93c4c3 --- /dev/null +++ b/custom_packages/network/windows/flutter/generated_plugins.cmake @@ -0,0 +1,23 @@ +# +# Generated file, do not edit. +# + +list(APPEND FLUTTER_PLUGIN_LIST +) + +list(APPEND FLUTTER_FFI_PLUGIN_LIST +) + +set(PLUGIN_BUNDLED_LIBRARIES) + +foreach(plugin ${FLUTTER_PLUGIN_LIST}) + add_subdirectory(flutter/ephemeral/.plugin_symlinks/${plugin}/windows plugins/${plugin}) + target_link_libraries(${BINARY_NAME} PRIVATE ${plugin}_plugin) + list(APPEND PLUGIN_BUNDLED_LIBRARIES $) + list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries}) +endforeach(plugin) + +foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST}) + add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/windows plugins/${ffi_plugin}) + list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries}) +endforeach(ffi_plugin) diff --git a/lib/magento_apis/api_keys_default.dart b/lib/magento_apis/api_keys_default.dart new file mode 100644 index 0000000..d8c539c --- /dev/null +++ b/lib/magento_apis/api_keys_default.dart @@ -0,0 +1,3 @@ +class APIKeys { + static const common = 'YOUR_API_KEY'; +} diff --git a/lib/magento_apis/services/product_service.dart b/lib/magento_apis/services/product_service.dart index 7869c8a..acc650a 100644 --- a/lib/magento_apis/services/product_service.dart +++ b/lib/magento_apis/services/product_service.dart @@ -3,8 +3,8 @@ import 'dart:convert'; import 'package:flutter_app/features/products/domain/product.dart'; import 'package:flutter_app/magento_apis/request.dart'; import 'package:flutter_app/magento_apis/models/product.dart'; -import 'package:network/network.dart'; import 'package:flutter_app/features/products/application/product_service.dart'; +import 'package:network/network.dart'; class MProductService extends ProductService { final APIRepository apiRepository; diff --git a/pubspec.yaml b/pubspec.yaml index d876bd2..3ed9c7d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,7 +38,7 @@ dependencies: cached_network_image: ^3.2.3 rxdart: ^0.27.3 network: - path: packages/network + path: custom_packages/network # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.