Skip to content

Commit

Permalink
add initial python package
Browse files Browse the repository at this point in the history
  • Loading branch information
zefanjajobse committed Sep 24, 2022
1 parent 65ff27b commit 05d889c
Show file tree
Hide file tree
Showing 14 changed files with 1,968 additions and 3 deletions.
29 changes: 28 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,43 @@ const call = communityGames.getPlayground(request, metadata,
});
```
## python
for python you can use the 'sonora' package to do grpc-web
```py
import sonora.aio
import sys
from proto import communitygames_pb2, communitygames_pb2_grpc

async def main():
async with sonora.aio.insecure_web_channel(
f"https://kingston-prod-wgw-envoy.ops.dice.se"
) as channel:
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', 'web-c6b312c9-2520-4fde-958d-60ae71840a65'),
('x-grpc-web', '1'),
('x-user-agent', 'grpc-web-javascript/0.1')
))
```
### current build method from proto to javascript via python
needs proto-compile, which can be installed with:
`pip3 install proto-compile`
and build with:
`proto-compile --clear-output-dirs --verbosity=1 ./proto ./src/proto grpc-web --grpc_web_out_options="import_style=typescript,mode=grpcweb"`
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`
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
package versions can be made with `npm run build` and `npm version patch` `git push --tags origin main` to release.
example library used: https://github.com/tomchen/example-typescript-package
Empty file.
22 changes: 22 additions & 0 deletions bfportal_grpc_python/access_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import asyncio
import aiohttp
import urllib.parse

class Cookie:
sid: str
remid: str

async def getBf2042GatewaySession(cookie: Cookie):
async with aiohttp.ClientSession() as session:
url = "https://accounts.ea.com/connect/auth?client_id=KINGSTON_COMP_APP&locale=en_US&redirect_uri=https%3A%2F%2Fportal.battlefield.com%2F&response_type=code"
headers = {
"Cookie": f"sid={cookie.sid}; remid={cookie.remid};"
}
async with session.get(url=url, headers=headers, allow_redirects=False) as r:
redirect = r.headers["Location"]
access_code = urllib.parse.urlparse(
redirect).query.split("=")[1]
return access_code

if __name__ == "__main__":
asyncio.run(getBf2042GatewaySession())
126 changes: 126 additions & 0 deletions bfportal_grpc_python/proto/authentication_pb2.py

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

99 changes: 99 additions & 0 deletions bfportal_grpc_python/proto/authentication_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

from proto import authentication_pb2 as proto_dot_authentication__pb2


class ClientAuthenticationStub(object):
"""Missing associated documentation comment in .proto file."""

def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.viaAuthCode = channel.unary_unary(
'/web.authentication.ClientAuthentication/viaAuthCode',
request_serializer=proto_dot_authentication__pb2.AuthRequest.SerializeToString,
response_deserializer=proto_dot_authentication__pb2.AuthResponse.FromString,
)
self.logout = channel.unary_unary(
'/web.authentication.ClientAuthentication/logout',
request_serializer=proto_dot_authentication__pb2.Empty.SerializeToString,
response_deserializer=proto_dot_authentication__pb2.Empty.FromString,
)


class ClientAuthenticationServicer(object):
"""Missing associated documentation comment in .proto file."""

def viaAuthCode(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def logout(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')


def add_ClientAuthenticationServicer_to_server(servicer, server):
rpc_method_handlers = {
'viaAuthCode': grpc.unary_unary_rpc_method_handler(
servicer.viaAuthCode,
request_deserializer=proto_dot_authentication__pb2.AuthRequest.FromString,
response_serializer=proto_dot_authentication__pb2.AuthResponse.SerializeToString,
),
'logout': grpc.unary_unary_rpc_method_handler(
servicer.logout,
request_deserializer=proto_dot_authentication__pb2.Empty.FromString,
response_serializer=proto_dot_authentication__pb2.Empty.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'web.authentication.ClientAuthentication', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))


# This class is part of an EXPERIMENTAL API.
class ClientAuthentication(object):
"""Missing associated documentation comment in .proto file."""

@staticmethod
def viaAuthCode(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/web.authentication.ClientAuthentication/viaAuthCode',
proto_dot_authentication__pb2.AuthRequest.SerializeToString,
proto_dot_authentication__pb2.AuthResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)

@staticmethod
def logout(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/web.authentication.ClientAuthentication/logout',
proto_dot_authentication__pb2.Empty.SerializeToString,
proto_dot_authentication__pb2.Empty.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
987 changes: 987 additions & 0 deletions bfportal_grpc_python/proto/communitygames_pb2.py

Large diffs are not rendered by default.

Loading

0 comments on commit 05d889c

Please sign in to comment.