Skip to content

Commit

Permalink
feat: upgrade to sdk 1.4.0 naming
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-zippenfenig committed Oct 30, 2023
1 parent 6cd77a6 commit 98c7473
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 100 deletions.
51 changes: 19 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
[![Build Status][build-img]][build-url]
[![Downloads][downloads-img]][downloads-url]
[![Issues][issues-img]][issues-url]
[![Code Coverage][codecov-img]][codecov-url]
[![Commitizen Friendly][commitizen-img]][commitizen-url]
[![Semantic Release][semantic-release-img]][semantic-release-url]

> My awesome module
This ia an API client to get weather data from the [Open-Meteo Weather API](https://open-meteo.com) in Typescript / Javascript.

Instead of using JSON, the API client uses FlatBuffers to transfer data. Encoding data in FlatBuffers is more efficient for long time-series data. The schema definition files can be found on [GitHub open-meteo/sdk](https://github.com/open-meteo/sdk).

## Install

Expand All @@ -19,36 +20,24 @@ npm install openmeteo
## Usage

```ts
import { openmeteo } from 'openmeteo';

openmeteo('hello');
//=> 'hello from my package'
import { fetchWeatherApi } from 'openmeteo';

const params = {
latitude: [52.54],
longitude: [13.41],
hourly: ['temperature_2m', 'precipitation'],
};
const url = 'https://api.open-meteo.com/v1/forecast';
const responses = await fetchWeatherApi(url, params);

// Process first location. Add a for-loop for multiple locations or weather models
const response = responses[0];

const hourly = response.hourly()!;
const temperature_2m = hourly.variables(0)!.valuesArray();
const precipitation = hourly.variables(1)!.valuesArray();
```

## API

### myPackage(input, options?)

#### input

Type: `string`

Lorem ipsum.

#### options

Type: `object`

##### postfix

Type: `string`
Default: `rainbows`

Lorem ipsum.


Notes:
`flatc -o src/ --ts --ts-no-import-ext ~/Documents/open-meteo/format/*.fbs`

[build-img]:https://github.com/open-meteo/typescript/actions/workflows/release.yml/badge.svg
[build-url]:https://github.com/open-meteo/typescript/actions/workflows/release.yml
Expand All @@ -58,8 +47,6 @@ Notes:
[npm-url]:https://www.npmjs.com/package/openmeteo
[issues-img]:https://img.shields.io/github/issues/open-meteo/typescript
[issues-url]:https://github.com/open-meteo/typescript/issues
[codecov-img]:https://codecov.io/gh/open-meteo/typescript/branch/main/graph/badge.svg
[codecov-url]:https://codecov.io/gh/open-meteo/typescript
[semantic-release-img]:https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-release-url]:https://github.com/semantic-release/semantic-release
[commitizen-img]:https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
]
},
"dependencies": {
"@openmeteo/sdk": "^1.3.0",
"@openmeteo/sdk": "^1.4.0",
"flatbuffers": "^23.5.26"
}
}
18 changes: 11 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import { ByteBuffer } from 'flatbuffers';
import { ApiResponse } from '@openmeteo/sdk/api-response';
import { WeatherApiResponse } from '@openmeteo/sdk/weather-api-response';

async function weather_api(
async function fetchWeatherApi(
url: string,
params: any
): Promise<ApiResponse[]> {
): Promise<WeatherApiResponse[]> {
const urlParams = new URLSearchParams(params);
urlParams.set('format', 'flatbuffers');
console.log(`${url}?${urlParams.toString()}`);
//console.log(`${url}?${urlParams.toString()}`);
const response = await fetch(`${url}?${urlParams.toString()}`);
const bb = await response.arrayBuffer();
const fb = new ByteBuffer(new Uint8Array(bb));

// TODO: retry, error handling

const results: ApiResponse[] = [];
const results: WeatherApiResponse[] = [];
let pos = 0;
while (pos < fb.capacity()) {
fb.setPosition(pos);
const len = fb.readInt32(fb.position());
results.push(
ApiResponse.getSizePrefixedRootAsApiResponse(fb)
WeatherApiResponse.getSizePrefixedRootAsWeatherApiResponse(fb)
);
pos += len + 4;
}
return results;
}

export { weather_api };
// Future endpoints
// - fetchElevationApi
// - fetchGeocodingApi

export { fetchWeatherApi };
125 changes: 69 additions & 56 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,62 @@
import { weather_api } from '../src';
import { Variable } from '@openmeteo/sdk/variable';
import { fetchWeatherApi } from '../src';

describe('index', () => {
describe('myPackage', () => {
test('aaaaa', async () => {
describe('openmeteo', () => {
describe('client', () => {
test('fetch_2_locations', async () => {
// 2 location data, with hourly temp and precip
global.fetch = jest.fn(() =>
Promise.resolve({
arrayBuffer: () =>
Promise.resolve(
new Uint8Array([
116, 1, 0, 0, 32, 0, 0, 0, 28, 0, 28, 0, 24, 0, 20, 0, 16, 0, 12,
0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 28, 0, 0, 0,
36, 0, 0, 0, 0, 0, 0, 26, 0, 78, 109, 61, 0, 0, 112, 66, 112, 102,
86, 65, 0, 0, 82, 66, 12, 0, 32, 0, 20, 0, 12, 0, 8, 0, 4, 0, 12,
0, 0, 0, 28, 0, 0, 0, 16, 14, 0, 0, 128, 156, 201, 100, 0, 0, 0,
0, 0, 75, 200, 100, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 148, 0, 0,
0, 16, 0, 0, 0, 12, 0, 12, 0, 11, 0, 10, 0, 0, 0, 4, 0, 12, 0, 0,
0, 8, 0, 0, 0, 0, 0, 32, 24, 24, 0, 0, 0, 154, 153, 217, 63, 102,
102, 6, 64, 0, 0, 128, 63, 205, 204, 76, 62, 205, 204, 204, 61, 0,
0, 128, 63, 102, 102, 38, 64, 205, 204, 76, 64, 0, 0, 0, 64, 102,
102, 102, 63, 154, 153, 153, 62, 205, 204, 44, 64, 154, 153, 153,
63, 205, 204, 76, 62, 0, 0, 0, 0, 205, 204, 204, 61, 154, 153, 153,
62, 102, 102, 102, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 16, 0, 15, 0, 11, 0, 0, 0, 4,
0, 0, 0, 12, 0, 16, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 47,
24, 0, 0, 0, 104, 145, 135, 65, 207, 247, 135, 65, 207, 247, 135,
65, 156, 196, 134, 65, 104, 145, 133, 65, 207, 247, 133, 65, 207,
247, 135, 65, 207, 247, 135, 65, 156, 196, 136, 65, 53, 94, 140,
65, 207, 247, 145, 65, 53, 94, 144, 65, 156, 196, 144, 65, 104,
145, 151, 65, 207, 247, 161, 65, 156, 196, 166, 65, 207, 247, 163,
65, 104, 145, 145, 65, 53, 94, 144, 65, 2, 43, 137, 65, 53, 94, 134,
65, 53, 94, 134, 65, 207, 247, 131, 65, 207, 247, 129, 65, 124, 1,
0, 0, 32, 0, 0, 0, 28, 0, 36, 0, 32, 0, 28, 0, 24, 0, 20, 0, 12, 0,
11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 28, 0, 0, 0, 44, 0, 0,
0, 0, 0, 0, 26, 1, 0, 0, 0, 0, 0, 0, 0, 0, 78, 237, 60, 0, 0, 33,
68, 208, 204, 20, 65, 104, 102, 64, 66, 12, 0, 32, 0, 20, 0, 12,
0, 8, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 16, 14, 0, 0, 128, 156,
201, 100, 0, 0, 0, 0, 0, 75, 200, 100, 0, 0, 0, 0, 0, 0, 0, 0, 2,
0, 0, 0, 148, 0, 0, 0, 16, 0, 0, 0, 12, 0, 12, 0, 11, 0, 10, 0, 0,
0, 4, 0, 12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 32, 24, 24, 0, 0, 0, 154,
153, 153, 62, 0, 0, 0, 63, 154, 153, 153, 62, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 205, 204, 204, 61, 0, 0, 128, 63, 154, 153, 153, 63,
205, 204, 12, 64, 154, 153, 217, 63, 51, 51, 179, 63, 154, 153, 153,
62, 205, 204, 76, 63, 0, 0, 0, 64, 205, 204, 76, 64, 205, 204, 204,
62, 205, 204, 204, 61, 205, 204, 204, 61, 0, 0, 0, 0, 205, 204, 204,
61, 205, 204, 204, 61, 205, 204, 76, 62, 205, 204, 204, 61, 16, 0,
16, 0, 15, 0, 11, 0, 0, 0, 4, 0, 0, 0, 12, 0, 16, 0, 0, 0, 12, 0, 0,
0, 0, 0, 0, 1, 2, 0, 0, 47, 24, 0, 0, 0, 180, 72, 134, 65, 26, 175,
132, 65, 231, 123, 133, 65, 26, 175, 136, 65, 26, 175, 136, 65, 129,
21, 137, 65, 129, 21, 137, 65, 129, 21, 131, 65, 180, 72, 132, 65,
78, 226, 131, 65, 78, 226, 133, 65, 129, 21, 135, 65, 231, 123, 143,
65, 231, 123, 151, 65, 26, 175, 142, 65, 129, 21, 145, 65, 180, 72,
142, 65, 129, 21, 139, 65, 78, 226, 133, 65, 26, 175, 128, 65, 206,
247, 118, 65, 2, 43, 110, 65, 155, 196, 103, 65, 206, 247, 94, 65
116, 1, 0, 0, 32, 0, 0, 0, 28, 0, 28, 0, 24, 0, 20, 0, 16, 0,
12, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 28, 0,
0, 0, 36, 0, 0, 0, 0, 0, 0, 26, 0, 78, 109, 61, 0, 0, 112, 66,
112, 102, 86, 65, 0, 0, 82, 66, 12, 0, 32, 0, 20, 0, 12, 0, 8,
0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 16, 14, 0, 0, 128, 156, 201,
100, 0, 0, 0, 0, 0, 75, 200, 100, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0,
0, 0, 148, 0, 0, 0, 16, 0, 0, 0, 12, 0, 12, 0, 11, 0, 10, 0, 0,
0, 4, 0, 12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 32, 24, 24, 0, 0, 0,
154, 153, 217, 63, 102, 102, 6, 64, 0, 0, 128, 63, 205, 204, 76,
62, 205, 204, 204, 61, 0, 0, 128, 63, 102, 102, 38, 64, 205,
204, 76, 64, 0, 0, 0, 64, 102, 102, 102, 63, 154, 153, 153, 62,
205, 204, 44, 64, 154, 153, 153, 63, 205, 204, 76, 62, 0, 0, 0,
0, 205, 204, 204, 61, 154, 153, 153, 62, 102, 102, 102, 63, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 16, 0, 16, 0, 15, 0, 11, 0, 0, 0, 4, 0, 0, 0, 12, 0, 16,
0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 47, 24, 0, 0, 0, 104,
145, 135, 65, 207, 247, 135, 65, 207, 247, 135, 65, 156, 196,
134, 65, 104, 145, 133, 65, 207, 247, 133, 65, 207, 247, 135,
65, 207, 247, 135, 65, 156, 196, 136, 65, 53, 94, 140, 65, 207,
247, 145, 65, 53, 94, 144, 65, 156, 196, 144, 65, 104, 145, 151,
65, 207, 247, 161, 65, 156, 196, 166, 65, 207, 247, 163, 65,
104, 145, 145, 65, 53, 94, 144, 65, 2, 43, 137, 65, 53, 94, 134,
65, 53, 94, 134, 65, 207, 247, 131, 65, 207, 247, 129, 65, 124,
1, 0, 0, 32, 0, 0, 0, 28, 0, 36, 0, 32, 0, 28, 0, 24, 0, 20, 0,
12, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 28, 0, 0, 0,
44, 0, 0, 0, 0, 0, 0, 26, 1, 0, 0, 0, 0, 0, 0, 0, 0, 78, 237,
60, 0, 0, 33, 68, 208, 204, 20, 65, 104, 102, 64, 66, 12, 0, 32,
0, 20, 0, 12, 0, 8, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 16, 14,
0, 0, 128, 156, 201, 100, 0, 0, 0, 0, 0, 75, 200, 100, 0, 0, 0,
0, 0, 0, 0, 0, 2, 0, 0, 0, 148, 0, 0, 0, 16, 0, 0, 0, 12, 0, 12,
0, 11, 0, 10, 0, 0, 0, 4, 0, 12, 0, 0, 0, 8, 0, 0, 0, 0, 0, 32,
24, 24, 0, 0, 0, 154, 153, 153, 62, 0, 0, 0, 63, 154, 153, 153,
62, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 205, 204, 204, 61, 0, 0,
128, 63, 154, 153, 153, 63, 205, 204, 12, 64, 154, 153, 217, 63,
51, 51, 179, 63, 154, 153, 153, 62, 205, 204, 76, 63, 0, 0, 0,
64, 205, 204, 76, 64, 205, 204, 204, 62, 205, 204, 204, 61, 205,
204, 204, 61, 0, 0, 0, 0, 205, 204, 204, 61, 205, 204, 204, 61,
205, 204, 76, 62, 205, 204, 204, 61, 16, 0, 16, 0, 15, 0, 11, 0,
0, 0, 4, 0, 0, 0, 12, 0, 16, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 1,
2, 0, 0, 47, 24, 0, 0, 0, 180, 72, 134, 65, 26, 175, 132, 65,
231, 123, 133, 65, 26, 175, 136, 65, 26, 175, 136, 65, 129, 21,
137, 65, 129, 21, 137, 65, 129, 21, 131, 65, 180, 72, 132, 65,
78, 226, 131, 65, 78, 226, 133, 65, 129, 21, 135, 65, 231, 123,
143, 65, 231, 123, 151, 65, 26, 175, 142, 65, 129, 21, 145, 65,
180, 72, 142, 65, 129, 21, 139, 65, 78, 226, 133, 65, 26, 175,
128, 65, 206, 247, 118, 65, 2, 43, 110, 65, 155, 196, 103, 65,
206, 247, 94, 65,
]).buffer
),
json: () => Promise.resolve({ test: 100 }),
Expand All @@ -68,14 +72,23 @@ describe('index', () => {
format: 'flatbuffers',
};
const url = 'https://api.open-meteo.com/v1/forecast';
const a = await weather_api(url, params);
expect(a).toHaveLength(2);
expect(a[0].latitude()).toBeCloseTo(52.5, 2);
expect(a[1].latitude()).toBeCloseTo(48.1, 2);
const temperature_2m = a[0].hourly()?.series(0)?.valuesArray();
expect(temperature_2m).toBeDefined();
expect(temperature_2m).toHaveLength(24);
expect(temperature_2m![0]).toBeCloseTo(16.9459991, 2);
const responses = await fetchWeatherApi(url, params);
expect(responses).toHaveLength(2);
expect(responses[0].latitude()).toBeCloseTo(52.5, 2);
expect(responses[1].latitude()).toBeCloseTo(48.1, 2);
const hourly = responses[0].hourly()!;

const temperature_2m = hourly.variables(0)!;
expect(temperature_2m.variable()).toBe(Variable.temperature);
expect(temperature_2m.altitude()).toBe(2);

//const hourly_series = [...Array(hourly.seriesLength()).keys()].map(i => hourly.series(i)!)
//const temperature_2m = hourly_series.find(s => s.variable() == Variable.temperature)?.valuesArray()

const temperature_2m_array = temperature_2m.valuesArray();
expect(temperature_2m_array).toBeDefined();
expect(temperature_2m_array).toHaveLength(24);
expect(temperature_2m_array![0]).toBeCloseTo(16.9459991, 2);
});
});
});

0 comments on commit 98c7473

Please sign in to comment.