Skip to content

Commit

Permalink
Merge pull request #14 from optimalstrategy/http-headers
Browse files Browse the repository at this point in the history
(feat): HTTP Headers
  • Loading branch information
optimalstrategy authored Nov 27, 2021
2 parents fa8f793 + 2b6f3e8 commit 11d873c
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This app can forward SMS messages to a Telegram bot or an HTTP endpoint.

# Installation
1. Simply download and install `sms_forwarder.apk` from the lateset [release](https://github.com/OptimalStrategy/sms_forwarder_app/releases).
1. Simply download and install `sms_forwarder.apk` from the latest [release](https://github.com/OptimalStrategy/sms_forwarder_app/releases).

## Development and Building
1. Install [flutter](https://flutter.io/docs/get-started/install)
Expand Down
6 changes: 3 additions & 3 deletions ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/opt/flutter"
export "FLUTTER_APPLICATION_PATH=/home/george/projects/sms_forwarder_app"
export "COCOAPODS_PARALLEL_CODE_SIGN=true"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
export "FLUTTER_BUILD_NAME=1.4.0"
export "FLUTTER_BUILD_NUMBER=1.4.0"
export "FLUTTER_BUILD_NAME=1.5.0"
export "FLUTTER_BUILD_NUMBER=1.5.0"
export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=false"
export "TREE_SHAKE_ICONS=false"
Expand Down
2 changes: 1 addition & 1 deletion lib/app_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class _AppSettingsScreenState extends State<AppSettingsScreen> {

void _testForwarders() async {
// ignore: invalid_use_of_visible_for_testing_member
final results = await fwd.mgr.forward(SmsMessage.fromMap({
final results = await fwd.mgr.forward(SmsMessage.fromMap({
"address": "SmsForwarder",
"body": _testMessageController.text,
"date": DateTime.now().millisecondsSinceEpoch.toString(),
Expand Down
3 changes: 1 addition & 2 deletions lib/background_forwarder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class BackgroundForwarder {
BackgroundForwarder(Telephony telephony) {
telephony.listenIncomingSms(
onNewMessage: (msg) async => await mgr.forward(msg),
onBackgroundMessage: onBackgroundMessage
);
onBackgroundMessage: onBackgroundMessage);
}

static void onBackgroundMessage(SmsMessage msg) async {
Expand Down
17 changes: 12 additions & 5 deletions lib/forwarding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,16 @@ class HttpCallbackForwarder extends AbstractForwarder with HttpForwarder {
/// The optional JSON payload (only used when performing PUT and POST requests).
Map<String, String> jsonPayload = {};

/// The HTTP headers to include in the request
Map<String, String> httpHeaders = {};

/// Returns the url of the callback.
String get callbackUrl => _callbackUrl;

/// Initializes the forwarder.
/// The caller is responsible to make sure that the protocol is valid.
HttpCallbackForwarder(this._callbackUrl,
{this.method, this.uriPayload, this.jsonPayload});
{this.method, this.uriPayload, this.jsonPayload, this.httpHeaders});

/// Creates a new HttpCallbackForwarder from the given [json] object.
@override
Expand Down Expand Up @@ -173,6 +176,7 @@ class HttpCallbackForwarder extends AbstractForwarder with HttpForwarder {

uriPayload = Map.from(json["uriPayload"] ?? {});
jsonPayload = Map.from(json["jsonPayload"] ?? {});
httpHeaders = Map.from(json["httpHeaders"] ?? {});
}

/// Dumps the forwarder's configuration to json
Expand All @@ -182,7 +186,8 @@ class HttpCallbackForwarder extends AbstractForwarder with HttpForwarder {
"callbackUrl": _callbackUrl,
"method": method.name,
"uriPayload": uriPayload,
"jsonPayload": jsonPayload
"jsonPayload": jsonPayload,
"httpHeaders": httpHeaders,
});
return '{"HttpCallbackForwarder": $fields}';
}
Expand All @@ -199,7 +204,7 @@ class HttpCallbackForwarder extends AbstractForwarder with HttpForwarder {
smsData.addAll(uriPayload);
// Then URI encode the map and perform the request
final uriParams = HttpForwarder.mapToUri(smsData);
return http.get("$_callbackUrl$uriParams");
return http.get("$_callbackUrl$uriParams", headers: httpHeaders);

case HttpMethod.POST:
case HttpMethod.PUT:
Expand All @@ -211,8 +216,10 @@ class HttpCallbackForwarder extends AbstractForwarder with HttpForwarder {
final url = "$_callbackUrl$uriParams";
// Perform the request using the required method
return method == HttpMethod.POST
? http.post(url, body: HttpForwarder.mapToJson(payload))
: http.put(url, body: HttpForwarder.mapToJson(payload));
? http.post(url,
body: HttpForwarder.mapToJson(payload), headers: httpHeaders)
: http.put(url,
body: HttpForwarder.mapToJson(payload), headers: httpHeaders);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/key_value_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';

/// A screen with a vertical list of key-value pairs. Pairs may be removed
/// and created dynamically. The widget uses the provided kvMap as its baking storage,
/// modifying it in the process.
/// modifying it according to the user's actions.
class KeyValuePairSettingsScreen extends StatefulWidget {
const KeyValuePairSettingsScreen(this.title, this.kvMap, {Key key})
: super(key: key);
Expand Down
17 changes: 15 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class _HttpCallbackForwarderState
HttpMethod _method = HttpMethod.POST;
Map<String, String> _uriParams;
Map<String, String> _jsonParams;
Map<String, String> _httpHeaders;

/// Input textbox controller
TextEditingController _controller;
Expand All @@ -293,6 +294,7 @@ class _HttpCallbackForwarderState
_method = fwd?.method ?? HttpMethod.POST;
_uriParams = Map.from(fwd?.uriPayload ?? {});
_jsonParams = Map.from(fwd?.jsonPayload ?? {});
_httpHeaders = Map.from(fwd?.httpHeaders ?? {});
_controller.addListener(_onTextChanged);
setState(_onTextChanged);
}
Expand Down Expand Up @@ -373,7 +375,14 @@ class _HttpCallbackForwarderState
onPressed: () => showDialog(
context: context,
builder: (_) => KeyValuePairSettingsScreen(
"JSON Payload", _jsonParams)))
"JSON Payload", _jsonParams))),
Padding(padding: EdgeInsets.symmetric(horizontal: 5)),
ElevatedButton(
child: Text('Headers'),
onPressed: () => showDialog(
context: context,
builder: (_) => KeyValuePairSettingsScreen(
"HTTP Headers", _httpHeaders)))
]),
Padding(padding: EdgeInsets.symmetric(vertical: 2)),
ElevatedButton(
Expand Down Expand Up @@ -404,7 +413,10 @@ class _HttpCallbackForwarderState
@override
void _saveSettings() async {
widget?.fwd?.httpCallbackForwarder = HttpCallbackForwarder(_controller.text,
method: _method, uriPayload: _uriParams, jsonPayload: _jsonParams);
method: _method,
uriPayload: _uriParams,
jsonPayload: _jsonParams,
httpHeaders: _httpHeaders);
widget?.fwd?.dumpToPrefs();
}

Expand All @@ -413,6 +425,7 @@ class _HttpCallbackForwarderState
void _resetSettings() async {
_uriParams.clear();
_jsonParams.clear();
_httpHeaders.clear();
widget?.fwd?.httpCallbackForwarder = null;
widget?.fwd?.dumpToPrefs();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/update_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import 'dart:io';

import 'package:http/http.dart' as http;

const String APP_VERSION = "v1.5.0";
const String APP_VERSION = "v1.6.0";
const String GITHUB_URL = "https://github.com/optimalstrategy/"
"sms_forwarder_app/releases/latest";
const String GITHUB_API_URL = "https://api.github.com/repos/"
+ "optimalstrategy/sms_forwarder_app/releases/latest";
const String GITHUB_API_URL = "https://api.github.com/repos/" +
"optimalstrategy/sms_forwarder_app/releases/latest";

Future<bool> isUpdateAvailable() async {
try {
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sms_forwarder
description: An SMS forwarding application.
version: 1.5.0
version: 1.6.0

environment:
sdk: '>=2.10.0 <3.0.0'
Expand All @@ -11,8 +11,8 @@ dependencies:

telephony: ^0.1.0
http: ^0.12.0
shared_preferences: ^0.4.3
url_launcher: ^4.0.1
shared_preferences: ^2.0.9
url_launcher: ^6.0.15
cupertino_icons: ^0.1.0

dev_dependencies:
Expand Down

0 comments on commit 11d873c

Please sign in to comment.