Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cesarferreira/91ba790f91f7ff13458e130e2035b21c to your computer and use it in GitHub Desktop.
Save cesarferreira/91ba790f91f7ff13458e130e2035b21c 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