Skip to content

Commit

Permalink
We don't have to live in ES2016 world, we can have top-level async
Browse files Browse the repository at this point in the history
  • Loading branch information
slahtine committed Sep 9, 2024
1 parent aa0fda7 commit 34f8548
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 287 deletions.
80 changes: 38 additions & 42 deletions examples/device-status-connectivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,41 @@

import { NetworkAsCodeClient } from "network-as-code";

import { Device, DeviceIpv4Addr } from "network-as-code/models/device";

const main = async () => {

const client = new NetworkAsCodeClient("<your-application-key-here>");

// Create a device object for the mobile device we want to use
const myDevice = client.devices.get({
networkAccessIdentifier: "[email protected]",
ipv4Address: {
publicAddress: "233.252.0.2",
privateAddress: "192.0.2.25",
publicPort: 80,
},
Ipv6Address: "2041:0000:140F::875B:131B",
// The phone number accepts the "+" sign, but not spaces or "()" marks
phoneNumber: "36721601234567"
});

// Simply change the event_type to "ROAMING_STATUS" whenever needed.
const mySubscription = await client.deviceStatus.subscribe(
myDevice,
// When necessary, change it to:
// "org.camaraproject.device-status.v0.roaming-status"
"org.camaraproject.device-status.v0.connectivity-data",
// Use HTTPS to send notifications
"https://example.com/notify",
{
maxNumberOfReports: 5,
notificationAuthToken: "replace-with-your-auth-token"
}
);

// Use this to show the roaming subscription status
console.log(mySubscription);

// Get the subscription by its ID
const subscription = await client.deviceStatus.get(mySubscription.eventSubscriptionId);

// Then, delete the subscription whenever needed
subscription.delete();
};

const client = new NetworkAsCodeClient("<your-application-key-here>");

// Create a device object for the mobile device we want to use
const myDevice = client.devices.get({
networkAccessIdentifier: "[email protected]",
ipv4Address: {
publicAddress: "233.252.0.2",
privateAddress: "192.0.2.25",
publicPort: 80,
},
Ipv6Address: "2041:0000:140F::875B:131B",
// The phone number accepts the "+" sign, but not spaces or "()" marks
phoneNumber: "36721601234567"
});

// Simply change the event_type to "ROAMING_STATUS" whenever needed.
const mySubscription = await client.deviceStatus.subscribe(
myDevice,
// When necessary, change it to:
// "org.camaraproject.device-status.v0.roaming-status"
"org.camaraproject.device-status.v0.connectivity-data",
// Use HTTPS to send notifications
"https://example.com/notify",
{
maxNumberOfReports: 5,
notificationAuthToken: "replace-with-your-auth-token"
}
);

// Use this to show the roaming subscription status
console.log(mySubscription);

// Get the subscription by its ID
const subscription = await client.deviceStatus.get(mySubscription.eventSubscriptionId);

// Then, delete the subscription whenever needed
subscription.delete();
29 changes: 12 additions & 17 deletions examples/identity-security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@

import { NetworkAsCodeClient } from 'network-as-code';

import { Device } from ".network-as-code/models/device";
const client = new NetworkAsCodeClient("<your-application-key-here>");
// Then, create a device object for the phone number you want to check

// Initialize the client object with your application key
const main = async () => {
const client = new NetworkAsCodeClient("<your-application-key-here>");
// Then, create a device object for the phone number you want to check
const myDevice = client.devices.get({
// The phone number accepts the "+" sign, but not spaces or "()" marks
phoneNumber: "36721601234567"
});

const myDevice = client.devices.get({
// The phone number accepts the "+" sign, but not spaces or "()" marks
phoneNumber: "36721601234567"
});
// Check the latest SIM-Swap date
const simSwapDate = myDevice.getSimSwapDate()

// Check the latest SIM-Swap date
const simSwapDate = myDevice.getSimSwapDate()

// Check SIM-Swap events within specified time spans
// The maxAge parameter is not mandatory
// This method also checks if SIM swap occurred within an undefined age
const simSwapCheck = myDevice.verifySimSwap(360)
};
// Check SIM-Swap events within specified time spans
// The maxAge parameter is not mandatory
// This method also checks if SIM swap occurred within an undefined age
const simSwapCheck = myDevice.verifySimSwap(360)
59 changes: 27 additions & 32 deletions examples/location-retrieval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,35 @@
// Location verification examples:

import { NetworkAsCodeClient } from "network-as-code";
import { Device, DeviceIpv4Addr } from "network-as-code/models/device";
import { CivicAddress, Location } from "network-as-code/models/location";

// We are executing inside an async function to have access to await
const main = async () => {
// We initialize the client object with your application key
const client = new NetworkAsCodeClient("<your-application-key-here>");
// We initialize the client object with your application key
const client = new NetworkAsCodeClient("<your-application-key-here>");

// Create a device object for the mobile device we want to use
const myDevice = client.devices.get({
networkAccessIdentifier: "[email protected]",
ipv4Address: {
publicAddress: "233.252.0.2",
privateAddress: "192.0.2.25",
publicPort: 80,
},
Ipv6Address: "2041:0000:140F::875B:131B",
// The phone number accepts the "+" sign, but not spaces or "()" marks
phoneNumber: "36721601234567"
});
// Create a device object for the mobile device we want to use
const myDevice = client.devices.get({
networkAccessIdentifier: "[email protected]",
ipv4Address: {
publicAddress: "233.252.0.2",
privateAddress: "192.0.2.25",
publicPort: 80,
},
Ipv6Address: "2041:0000:140F::875B:131B",
// The phone number accepts the "+" sign, but not spaces or "()" marks
phoneNumber: "36721601234567"
});

// Specify the maximum amount of time accepted
// to get location information, it's a mandatory parameter.
// The value (integer in seconds) can be passed directly.
const location = await myDevice.getLocation(3600);
// Specify the maximum amount of time accepted
// to get location information, it's a mandatory parameter.
// The value (integer in seconds) can be passed directly.
const location = await myDevice.getLocation(3600);

console.log(location.latitude);
console.log(location.longitude);
console.log(location.civicAddress);
console.log(location.latitude);
console.log(location.longitude);
console.log(location.civicAddress);

// For estimations, use the `verifyLocation()` method
// with the geo-coordinates and maximum age in seconds.
// Integers can be passed directly in TypeScript.
if (await myDevice.verifyLocation(60.252, 25.227, 1_000, 3600)) {
console.log("Our device is near Helsinki!");
}
};
// For estimations, use the `verifyLocation()` method
// with the geo-coordinates and maximum age in seconds.
// Integers can be passed directly in TypeScript.
if (await myDevice.verifyLocation(60.252, 25.227, 1_000, 3600)) {
console.log("Our device is near Helsinki!");
}
101 changes: 48 additions & 53 deletions examples/network-insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,51 @@

import { NetworkAsCodeClient } from 'network-as-code';

import { Device, DeviceIpv4Addr } from "network-as-code/models/device";

// We are executing inside an async function to have access to await
const main = async () => {
// Begin by creating a client for Network as Code:
const client = new NetworkAsCodeClient("<your-application-key-here>");

// The "[email protected]" should be replaced
// with a test device copied from your Developer Sandbox
// Or you can identify a device with its ID,
// IP address(es) and optionally, a phone number
const myDevice = client.devices.get({
networkAccessIdentifier: "[email protected]",
ipv4Address: {
publicAddress: "233.252.0.2",
privateAddress: "192.0.2.25",
publicPort: 80,
},
Ipv6Address: "2041:0000:140F::875B:131B",
// The phone number accepts the "+" sign, but not spaces or "()" marks
phoneNumber: "36721601234567"
});

// Set the duration of your subscription to congestion insights,
// e.g.: it can end in `n` days starting from now.
const tomorrowDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
tomorrowDate.setMilliseconds(0);

// Subscribe your device to Congestion notifications
const congestionSubscription = await client.insights.subscribeToCongestionInfo(
myDevice,
tomorrowDate,
"https://example.com/notify",
"my-secret-token"
);

// Subscriptions are identified by id, for management
// Use this to show the subscription:
console.log(congestionSubscription.subscriptionId());

// Or check when your subscription starts/expires:
console.log(congestionSubscription.startsAt);
console.log(congestionSubscription.expiresAt);

// Get congestion predictions and historical data
// between two timestamps with ISO 8601 formatted date strings.
const congestion = await myDevice.getCongestion(
// start date
"2025-04-15T05:11:30.961136Z",
// end date
"2025-04-16T05:11:30Z"
);
}
// Begin by creating a client for Network as Code:
const client = new NetworkAsCodeClient("<your-application-key-here>");

// The "[email protected]" should be replaced
// with a test device copied from your Developer Sandbox
// Or you can identify a device with its ID,
// IP address(es) and optionally, a phone number
const myDevice = client.devices.get({
networkAccessIdentifier: "[email protected]",
ipv4Address: {
publicAddress: "233.252.0.2",
privateAddress: "192.0.2.25",
publicPort: 80,
},
Ipv6Address: "2041:0000:140F::875B:131B",
// The phone number accepts the "+" sign, but not spaces or "()" marks
phoneNumber: "36721601234567"
});

// Set the duration of your subscription to congestion insights,
// e.g.: it can end in `n` days starting from now.
const tomorrowDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
tomorrowDate.setMilliseconds(0);

// Subscribe your device to Congestion notifications
const congestionSubscription = await client.insights.subscribeToCongestionInfo(
myDevice,
tomorrowDate,
"https://example.com/notify",
"my-secret-token"
);

// Subscriptions are identified by id, for management
// Use this to show the subscription:
console.log(congestionSubscription.subscriptionId());

// Or check when your subscription starts/expires:
console.log(congestionSubscription.startsAt);
console.log(congestionSubscription.expiresAt);

// Get congestion predictions and historical data
// between two timestamps with ISO 8601 formatted date strings.
const congestion = await myDevice.getCongestion(
// start date
"2025-04-15T05:11:30.961136Z",
// end date
"2025-04-16T05:11:30Z"
);
Loading

0 comments on commit 34f8548

Please sign in to comment.