Skip to content

Commit

Permalink
changes in protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
zefanjajobse committed May 30, 2023
1 parent e0192fe commit 96d28fd
Show file tree
Hide file tree
Showing 19 changed files with 466 additions and 1,443 deletions.
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# apple m1:
# docker build --platform linux/amd64 . -t bfportal
# docker run --platform linux/amd64 --rm -v $(pwd):/data bfportal


FROM python:3.11

RUN pip install proto-compile
RUN apt-get update && apt-get install tree

CMD cd /data && proto-compile --clear-output-dirs --verbosity=1 ./proto ./src/proto grpc-web --grpc_web_out_options="import_style=typescript,mode=grpcweb"
134 changes: 85 additions & 49 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,80 +10,114 @@ https://pypi.org/project/bfportal-grpc/
## example

```js
import { CommunityGamesClient, communitygames } from 'bfportal-grpc';
import { CommunityGamesClient, communitygames } from 'bfportal-grpc'

const communityGames = new CommunityGamesClient('https://kingston-prod-wgw-envoy.ops.dice.se', null);
const communityGames = new CommunityGamesClient(
'https://kingston-prod-wgw-envoy.ops.dice.se',
null
)
const metadata = {
'x-dice-tenancy': 'prod_default-prod_default-kingston-common',
'x-gateway-session-id': sessionId,
'x-grpc-web': '1',
'x-user-agent': 'grpc-web-javascript/0.1',
'x-dice-tenancy': 'prod_default-prod_default-kingston-common',
'x-gateway-session-id': sessionId,
'x-grpc-web': '1',
'x-user-agent': 'grpc-web-javascript/0.1',
}

const request = new communitygames.GetPlaygroundRequest();
request.setPlaygroundid(testPlayground);
const response = await communityGames.getPlayground(request, metadata);
const modRules = response.getPlayground()?.getOriginalplayground()?.getModrules()?.getCompatiblerules()?.getRules();
const request = new communitygames.GetPlaygroundRequest()
request.setPlaygroundid(testPlayground)
const response = await communityGames.getPlayground(request, metadata)
const modRules = response
.getPlayground()
?.getOriginalplayground()
?.getModrules()
?.getCompatiblerules()
?.getRules()
if (modRules instanceof Uint8Array) {
console.log(new TextDecoder().decode(modRules))
console.log(new TextDecoder().decode(modRules))
}
const playgroundName = response.getPlayground()?.getOriginalplayground()?.getName();
const playgroundName = response
.getPlayground()
?.getOriginalplayground()
?.getName()
```
the proto files are accessable directly via "node_modules/bfportal-grpc/proto/communitygames.proto" to for example decode to json:
```js
import { load } from "protobufjs";
import { load } from 'protobufjs'

// use reponse from previous example
const root = await load("node_modules/bfportal-grpc/proto/communitygames.proto");
const AwesomeMessage = root.lookupType("web.communitygames.PlaygroundInfoResponse");
const decoded = AwesomeMessage.decode(response.serializeBinary());
const json_str = JSON.stringify(decoded, null, 4);
const root = await load('node_modules/bfportal-grpc/proto/communitygames.proto')
const AwesomeMessage = root.lookupType(
'web.communitygames.PlaygroundInfoResponse'
)
const decoded = AwesomeMessage.decode(response.serializeBinary())
const json_str = JSON.stringify(decoded, null, 4)
```
### non-async example
```js
import { CommunityGamesClient, communitygames } from 'bfportal-grpc';
import { CommunityGamesClient, communitygames } from 'bfportal-grpc'

const communityGames = new CommunityGamesClient('https://kingston-prod-wgw-envoy.ops.dice.se', null);
const communityGames = new CommunityGamesClient(
'https://kingston-prod-wgw-envoy.ops.dice.se',
null
)
const metadata = {
'x-dice-tenancy': 'prod_default-prod_default-kingston-common',
'x-gateway-session-id': sessionId,
'x-grpc-web': '1',
'x-user-agent': 'grpc-web-javascript/0.1',
'x-dice-tenancy': 'prod_default-prod_default-kingston-common',
'x-gateway-session-id': sessionId,
'x-grpc-web': '1',
'x-user-agent': 'grpc-web-javascript/0.1',
}

const request = new communitygames.GetPlaygroundRequest();
request.setPlaygroundid("bbe433c0-13fa-11ed-bc32-24a8c2c0764e");
const call = communityGames.getPlayground(request, metadata,
const request = new communitygames.GetPlaygroundRequest()
request.setPlaygroundid('bbe433c0-13fa-11ed-bc32-24a8c2c0764e')
const call = communityGames.getPlayground(
request,
metadata,
(_err: grpcWeb.Error, response: communitygames.PlaygroundInfoResponse) => {
// console.log("err:", _err)
var modRules = response.getPlayground()?.getOriginalplayground()?.getModrules()?.getCompatiblerules()?.getRules();
var modRules = response
.getPlayground()
?.getOriginalplayground()
?.getModrules()
?.getCompatiblerules()
?.getRules()
if (modRules instanceof Uint8Array) {
console.log(new TextDecoder().decode(modRules))
console.log(new TextDecoder().decode(modRules))
}

load("node_modules/bfportal-grpc/proto/communitygames.proto", function(err, root) {
if (err)
throw err;
if (root == undefined)
return

const AwesomeMessage = root.lookupType("web.communitygames.PlaygroundInfoResponse");

let decoded = AwesomeMessage.decode(response.serializeBinary());
fs.writeFile("test.json", JSON.stringify(decoded, null, 4), function(err: any) {
if (err) {
console.log(err);
}
});
})
});
load(
'node_modules/bfportal-grpc/proto/communitygames.proto',
function (err, root) {
if (err) throw err
if (root == undefined) return

const AwesomeMessage = root.lookupType(
'web.communitygames.PlaygroundInfoResponse'
)

let decoded = AwesomeMessage.decode(response.serializeBinary())
fs.writeFile(
'test.json',
JSON.stringify(decoded, null, 4),
function (err: any) {
if (err) {
console.log(err)
}
}
)
}
)
}
)
```
## python
for python you can use the 'sonora' package to do grpc-web
```py
import asyncio
import sonora.aio
Expand All @@ -92,7 +126,7 @@ from bfportal_grpc import communitygames_pb2, communitygames_pb2_grpc, access_to
async def main():
cookie = access_token.Cookie(sid="", remid="")
token = await access_token.getBf2042GatewaySession(cookie)

async with sonora.aio.insecure_web_channel(
f"https://kingston-prod-wgw-envoy.ops.dice.se"
) as channel:
Expand All @@ -102,22 +136,23 @@ async def main():
('x-grpc-web', '1'),
('x-user-agent', 'grpc-web-javascript/0.1')
))

stub = communitygames_pb2_grpc.CommunityGamesStub(channel)
response: communitygames_pb2.PlaygroundInfoResponse = await stub.getPlayground(communitygames_pb2.GetPlaygroundRequest(playgroundId="10992a10-461a-11ec-8de0-d9f491f92236"), metadata=(
('x-dice-tenancy', 'prod_default-prod_default-kingston-common'),
('x-gateway-session-id', auth_response.sessionId),
('x-grpc-web', '1'),
('x-user-agent', 'grpc-web-javascript/0.1')
))

print(response.playground.originalPlayground.name)

if __name__ == "__main__":
asyncio.run(main())
```
### current build method from proto to javascript via python
needs proto-compile, which can be installed with:
`pip3 install proto-compile`
Expand All @@ -128,11 +163,12 @@ building for python requires grpcio-tools, which can be installed with:
`pip3 install grpcio-tools`
and build with:
`python3 -m grpc_tools.protoc -I. --python_out=./grpc-python --grpc_python_out=./grpc-python ./proto/communitygames.proto ./proto/localization.proto ./proto/authentication.proto ./proto/reporting.proto`
`python3 -m grpc_tools.protoc -I. --python_out=./bfportal_grpc --grpc_python_out=./bfportal_grpc ./proto/communitygames.proto ./proto/localization.proto ./proto/authentication.proto ./proto/reporting.proto`
python package used: https://github.com/romnn/proto-compile
### Pushing your changes
package versions can be made with `npm run build` and `npm version patch` `git push --tags origin main` to release.
for python patch with `npm run build:python`, `npm run python:setimports` and `poetry build`.
Expand Down
135 changes: 27 additions & 108 deletions bfportal_grpc/proto/authentication_pb2.py

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

Loading

0 comments on commit 96d28fd

Please sign in to comment.