Skip to content

Instantly share code, notes, and snippets.

@hayabusabusa
Last active February 18, 2020 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hayabusabusa/84832be9b426546bf0439ed26666e9ea to your computer and use it in GitHub Desktop.
Save hayabusabusa/84832be9b426546bf0439ed26666e9ea to your computer and use it in GitHub Desktop.
Flutter: Network Layer
/// HttpMethod:
/// リクエストのメソッドを定義
/// Enum は Swift っぽくかける Util を使用
class HttpMethod extends Enum<String> {
const HttpMethod(String val): super(val);
static const HttpMethod GET = const HttpMethod('GET');
static const HttpMethod POST = const HttpMethod('POST');
static const HttpMethod PUT = const HttpMethod('PUT');
static const HttpMethod DELETE = const HttpMethod('DELETE');
}
/// ContentEncoding:
/// リクエストのエンコード
enum ContentEncoding { url, json }
/// HttpRequestProtocol:
/// HTTP リクエストに必要な情報をここに定義する
abstract class HttpRequestProtocol {
String get baseUrl;
String get path;
HttpMethod get method;
Map<String, String> get headers;
Map<String, dynamic> get parameters;
ContentEncoding get contentEncoding;
/// このメソッドはオーバーライドしない
/// メソッドが GET の時は常にパラメーターをクエリとして追加する
String get queryParameters {
if (method == HttpMethod.GET) {
final jsonString = Uri(queryParameters: parameters);
return '?${jsonString.query}';
}
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment