Last active
March 2, 2020 05:48
-
-
Save lidio601/f5d8c202d98aafd6ee547d4a1661ec2b to your computer and use it in GitHub Desktop.
API integration for macvendors.com API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
/// Wrapper around Macvendors API | |
/// https://macvendors.com/api | |
/// | |
import 'package:http/http.dart' as http; | |
import "zmac_address.dart"; | |
const BASE_URL = "https://api.macvendors.com"; | |
Future<String> lookupMacAddress(String macaddress) async { | |
print("macvendors :: input $macaddress"); | |
final normalized = normalizeMacAddress(macaddress); | |
// print("macvendors :: normalized $normalized"); | |
if (normalized == null) { | |
return ""; | |
} | |
// take only the first 3 group | |
// to save on api call | |
final prefix = normalized.substring(0, "00:11:22".length); | |
// print("macvendors :: prefix $prefix"); | |
final url = "$BASE_URL/$prefix"; | |
// print("macvendors :: url $url"); | |
final response = await http.get(url); | |
// print("macvendors :: response $response"); | |
final vendor = response.body; | |
print("macvendors :: vendor $vendor"); | |
return vendor; | |
} | |
void main () { | |
print(lookupMacAddress("F0:18:98:41:5A:52")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final d = "([0-9A-F]{2})"; | |
final s = "[:\.-]?"; | |
final RegExp re = RegExp( | |
"$d$s$d$s$d$s$d$s$d$s$d", | |
multiLine: false, | |
); | |
/// MAC addresses will come in the following shape or form: | |
/// 00-11-22-33-44-55 | |
/// 00:11:22:33:44:55 | |
/// 00.11.22.33.44.55 | |
/// 001122334455 | |
/// 0011.2233.4455 | |
String normalizeMacAddress(String macaddress) { | |
final matches = re.allMatches(macaddress); | |
if (matches.length < 1) { | |
return null; | |
} | |
final match = matches.first; | |
return "${match.group(1)}:${match.group(2)}:${match.group(3)}:${match.group(4)}:${match.group(5)}:${match.group(6)}"; | |
} | |
// void test(address) { | |
// final result = normalizeMacAddress(address); | |
// print("$address => $result"); | |
// } | |
// void main() { | |
// test("00-11-22-33-44-55"); | |
// test("00:11:22:33:44:55"); | |
// test("00.11.22.33.44.55"); | |
// test("001122334455"); | |
// test("0011.2233.4455"); | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment