Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add lacking network package #11

Merged
merged 2 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions custom_packages/network/.gitignore
Original file line number Diff line number Diff line change
@@ -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/
10 changes: 10 additions & 0 deletions custom_packages/network/.metadata
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions custom_packages/network/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions custom_packages/network/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
39 changes: 39 additions & 0 deletions custom_packages/network/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

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.
4 changes: 4 additions & 0 deletions custom_packages/network/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 14 additions & 0 deletions custom_packages/network/ios/Flutter/Generated.xcconfig
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions custom_packages/network/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 19 additions & 0 deletions custom_packages/network/ios/Runner/GeneratedPluginRegistrant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Generated file. Do not edit.
//

// clang-format off

#ifndef GeneratedPluginRegistrant_h
#define GeneratedPluginRegistrant_h

#import <Flutter/Flutter.h>

NS_ASSUME_NONNULL_BEGIN

@interface GeneratedPluginRegistrant : NSObject
+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry;
@end

NS_ASSUME_NONNULL_END
#endif /* GeneratedPluginRegistrant_h */
14 changes: 14 additions & 0 deletions custom_packages/network/ios/Runner/GeneratedPluginRegistrant.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Generated file. Do not edit.
//

// clang-format off

#import "GeneratedPluginRegistrant.h"

@implementation GeneratedPluginRegistrant

+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
}

@end
24 changes: 24 additions & 0 deletions custom_packages/network/lib/exception.dart
Original file line number Diff line number Diff line change
@@ -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');
}
6 changes: 6 additions & 0 deletions custom_packages/network/lib/network.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library network;

export './exception.dart';
export './providers.dart';
export './repository.dart';
export './request.dart';
6 changes: 6 additions & 0 deletions custom_packages/network/lib/providers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:network/repository.dart';

final apiRepoProvider = Provider<APIRepository>((ref) {
return APIRepository();
});
59 changes: 59 additions & 0 deletions custom_packages/network/lib/repository.dart
Original file line number Diff line number Diff line change
@@ -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<T> excute<T>({
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();
}
}
}
29 changes: 29 additions & 0 deletions custom_packages/network/lib/request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
enum HttpMethod { get, post, put, patch, delete }

class Request {
String host;
String path;
String endPoint;
HttpMethod method;
Map<String, String> headers;

Map<String, Object> 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 {}});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Generated file. Do not edit.
//

// clang-format off

#include "generated_plugin_registrant.h"


void fl_register_plugins(FlPluginRegistry* registry) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Generated file. Do not edit.
//

// clang-format off

#ifndef GENERATED_PLUGIN_REGISTRANT_
#define GENERATED_PLUGIN_REGISTRANT_

#include <flutter_linux/flutter_linux.h>

// Registers Flutter plugins.
void fl_register_plugins(FlPluginRegistry* registry);

#endif // GENERATED_PLUGIN_REGISTRANT_
23 changes: 23 additions & 0 deletions custom_packages/network/linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
@@ -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 $<TARGET_FILE:${plugin}_plugin>)
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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Generated file. Do not edit.
//

import FlutterMacOS
import Foundation


func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
Loading