-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We don't have to live in ES2016 world, we can have top-level async
- Loading branch information
Showing
6 changed files
with
250 additions
and
287 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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!"); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
); |
Oops, something went wrong.