0.2.1 • Published 1 year ago

we.sdk v0.2.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

We Api

we.sdk is an open source package for JavaScript that acts as an Api or an SDK for We Telecom Egypt (We TE) website can only run in Egypt and with Node.js i am aiming to make this package as robust as possible and to cover all the features that the website provides please feel free to contribute to this project and make it better

DECLAIMER

⚠️⚠️⚠️⚠️⚠️⚠️⚠️ THIS PACKAGE IS NOT ASSOCIATED WITH WE TELECOM EGYPT IN ANY WAY, SHAPE OR FORM WHATSO EVER IT JUST BASED ON THE PUBLIC API THAT "WE TE" PROVIDES ON THERE WEBSITE

هذا المشروع ليس له اي علاقة نهائيا بشركة we المشروع ببساطة مبني علي الapi المتاح من خلال موقع we فقط وليس لwe اي علاقة بهاذا المشروع ⚠️⚠️⚠️⚠️⚠️⚠️⚠️

Installation

npm install we.sdk
yarn add we.sdk

Usage

there are two ways to use this package 1. only using the plain api 2. using the cached api (recommended and optimized)

Normal API usage

const { WeApi, WeApiError } = require('we.sdk')

const main = async (number, password) => {
   try {
      const session = await WeApi.userAuthenticate(number, password)
      const balance = await WeApi.getBalance(session)
      const quota = await WeApi.getFreeUnits(session)

      console.log(JSON.stringify(session, null, 2))
      console.log(JSON.stringify(balance, null, 2))
      console.log(JSON.stringify(quota, null, 2))
   } catch (error) {
      if (error instanceof WeApiError) {
         return console.log('We Api Error:', error.message)
      }
   }
}

main(process.argv[2], process.argv[3])

Cached API usage

const { WeCachedApi, WeApiError, CacheProviderInMemory, CacheProviderInFile } = require('we.sdk')

const main = async (number, password) => {
   try {
      const weCachedApi = new WeCachedApi({
         customer: {
            number: number,
            password: password,
         },
         CacheProvider: CacheProviderInFile, // or CacheProviderInMemory
         cachePath: './cache.json', // only needed if you are using CacheProviderInFile
         ttlInMs: {
            session: 3.5 * 60 * 60 * 1000, // 3.5 hours before the session expires
            balance: 10 * 60 * 1000, // 10 minutes before the balance expires
            freeUnit: 10 * 60 * 1000, // 10 minutes before the freeUnit expires
         },
         hooks: {
            beforeRequest: (key) => {
               console.log('beforeRequest', key) // will be called before the request if not cached
            },
            afterRequest: (key) => {
               console.log('afterRequest', key) // will be called after the request if not cached
            },
         },
      })

      const session = await weCachedApi.userAuthenticate()
      const balance = await weCachedApi.getBalance() // if session is not cached it will call userAuthenticate first
      const quota = await weCachedApi.getFreeUnits() // if session is not cached it will call userAuthenticate first

      console.log(JSON.stringify(session, null, 2))
      console.log(JSON.stringify(balance, null, 2))
      console.log(JSON.stringify(quota, null, 2))
   } catch (error) {
      if (error instanceof WeApiError) {
         return console.log('We Api Error:', error.message)
      }
   }
}

Todo List

  • userAuthenticate - login user with phone number and password
  • getBalance - get user balance
  • getBalanceInfo - get user balance info
  • checkIfCanRenew - check if user can renew his main subscription
  • renewMainSubscription - renew user main subscription
  • ApiCaching version

Api

WeApi (class)

const { WeApi } = require('we.sdk')

WeApi.userAuthenticate

WeApi.userAuthenticate(number: string, password: string): Promise<Session>

const session = await WeApi.userAuthenticate(number, password)
parametertyperequiredexamplenote
numberstringyes0223123456It can be in any valid Egyptian landline number ex: +20223123456, 0020223123456,+20-2-2312-3456, 68-312-3456, 02-2312-3456any Egyptian number that can be parsed with libphonenumber-js is validyou can check the logic behind it in phoneParser.js
passwordstringyesmyPasswordYour WE TE account password (this is not being stored, sent, or modified in any sort)

Return: Promise<Session> Errors: Possible Errors

WeApi.getBalance

WeApi.getBalance(session: Session): Promise<UserBalanceInfo>

const balance = await WeApi.getBalance(session)
parametertyperequiredexamplenote
sessionSessionyesonly parts needed from the Session Object not all for more details check the input from more info check the method input here

Return: Promise<UserBalanceInfo> Errors: Possible Errors

WeApi.getFreeUnits

WeApi.getFreeUnits(session: Session): Promise<FreeUnit[]>

const quota = await WeApi.getFreeUnits(session)
parametertyperequiredexamplenote
sessionSessionyesonly parts needed from the Session Object not all for more details check the input from more info check the method input here

Return: Promise<FreeUnit[]> Errors: Possible Errors

WeCachedApi (class)

const { WeCachedApi } = require('we.sdk')

WeCachedApi.constructor

new WeCachedApi(options: object)

const WeCachedApi = new WeCachedApi({
   customer: {
      number: number,
      password: password,
   },
   CacheProvider: CacheProviderInFile, // or CacheProviderInMemory
   cachePath: './cache.json', // only needed if you are using CacheProviderInFile
   ttlInMs: {
      session: 3.5 * 60 * 60 * 1000, // 3.5 hours before the session expires
      balance: 10 * 60 * 1000, // 10 minutes before the balance expires
      freeUnit: 10 * 60 * 1000, // 10 minutes before the freeUnit expires
   },
   hooks: {
      beforeRequest: (key) => {
         console.log('beforeRequest', key) // will be called before the request if not cached
      },
      afterRequest: (key) => {
         console.log('afterRequest', key) // will be called after the request if not cached
      },
   },
})
parametertyperequiredexamplenote
optionsobjectyes
customerobjectyes
customer.numberstringyes0223123456It can be in any valid Egyptian landline number ex: +20223123456, 0020223123456,+20-2-2312-3456, 68-312-3456, 02-2312-3456any Egyptian number that can be parsed with libphonenumber-js is validyou can check the logic behind it in phoneParser.js
customer.passwordstringyesmyPasswordYour WE TE account password (this is not being stored, sent, or modified in any sort)
CacheProviderclassno, default: CacheProviderInMemoryCacheProviderInFile or CacheProviderInMemorythe class that will be used to cache the data you can create your own cache provider by extending the CacheProviderInterface and implementing the methods
cachePathstringno, default: './cache.json''./cache.json'only needed if you are using CacheProviderInFile
ttlInMsobjectno, default: { session: 3.5 60 60 1000, balance: 10 60 1000, freeUnit: 10 60 * 1000 }{ session: 3.5 60 60 1000, balance: 10 60 1000, freeUnit: 10 60 * 1000 }the time to live for each cached item in milliseconds
hooksobjectnohooks that will be called before and after the request if not cached

WeCachedApi.*

the same as WeApi but with caching

CacheProviderInMemory (class)

used as a cache provider for the WeCachedApi class

const { CacheProviderInMemory } = require('we.sdk')

CacheProviderInFile (class)

used as a cache provider for the WeCachedApi class requires the cachePath parameter in the WeCachedApi.constructor

const { CacheProviderInFile } = require('we.sdk')

CacheProviderInterface (interface)

used to create your own cache provider for the WeCachedApi class

const { CacheProviderInterface } = require('we.sdk')

WeApiError (class) inherits from Error

const { WeApiError } = require('we.sdk')
  • has the same properties as the Error class
  • has an additional property code which is the error code
  • has name property set to WeApiError

Error Codes

Accessed by WeApiError instances error.code

const { WeApiError } = require('we.sdk')

try {
   // some code
} catch (error) {
   if (error instanceof WeApiError) {
      console.log('We Api Error:', error.message)
      console.log('We Api Error Code:', error.code)
   }
   //or

   if (error.name === 'WeApiError') {
      console.log('We Api Error:', error.message)
      console.log('We Api Error Code:', error.code)
   }
}
  • WE_INVALID_CREDENTIALS
  • WE_INVALID_SESSION
  • WE_INVALID_PHONE_NUMBER
  • WE_INVALID_RESPONSE
  • WE_RATE_LIMITED
  • WE_SERVER_ERROR
  • WE_INVALID_CACHE_PROVIDER

types

IndividualInfo

Customer

Account

Subscriber

UserResponse (session)

BalanceDetail

BalanceInfo

CreditInfo

UserBalanceInfo

FreeUnitBeanDetail

FreeUnit

Contributing

this project is open for contributions, feel free to open an issue or a pull request you can complete the todo list or add new features, optimize the code, or fix bugs make sure you write the code as clean as possible and add comments

License

MIT

0.2.1

1 year ago

0.2.0

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago