How to use generate_service_api method in localstack

Best Python code snippet using localstack_python

scaffold.py

Source:scaffold.py Github

copy

Full Screen

...207 else:208 stack.append(name)209 stack.extend(dependencies)210 visited.add(name)211def generate_service_api(output, service: ServiceModel, doc=True):212 service_name = service.service_name.replace("-", "_")213 class_name = service_name + "_api"214 class_name = snake_to_camel_case(class_name)215 output.write(f"class {class_name}:\n")216 output.write("\n")217 output.write(f' service = "{service.service_name}"\n')218 output.write(f' version = "{service.api_version}"\n')219 for op_name in service.operation_names:220 operation: OperationModel = service.operation_model(op_name)221 fn_name = camel_to_snake_case(op_name)222 if operation.output_shape:223 output_shape = operation.output_shape.name224 else:225 output_shape = "None"226 output.write("\n")227 parameters = OrderedDict()228 param_shapes = OrderedDict()229 input_shape = operation.input_shape230 if input_shape is not None:231 members = list(input_shape.members)232 for m in input_shape.required_members:233 members.remove(m)234 m_shape = input_shape.members[m]235 parameters[xform_name(m)] = m_shape.name236 param_shapes[xform_name(m)] = m_shape237 for m in members:238 m_shape = input_shape.members[m]239 param_shapes[xform_name(m)] = m_shape240 parameters[xform_name(m)] = f"{m_shape.name} = None"241 if any(map(is_bad_param_name, parameters.keys())):242 # if we cannot render the parameter name, don't expand the parameters in the handler243 param_list = f"request: {input_shape.name}" if input_shape else ""244 output.write(f' @handler("{operation.name}", expand=False)\n')245 else:246 param_list = ", ".join([f"{k}: {v}" for k, v in parameters.items()])247 output.write(f' @handler("{operation.name}")\n')248 output.write(249 f" def {fn_name}(self, context: RequestContext, {param_list}) -> {output_shape}:\n"250 )251 # convert html documentation to rst and print it into to the signature252 if doc:253 html = operation.documentation254 import pypandoc255 doc = pypandoc.convert_text(html, "rst", format="html")256 output.write(' """')257 output.write(f"{doc.strip()}\n")258 output.write("\n")259 # parameters260 for param_name, shape in param_shapes.items():261 # FIXME: this doesn't work properly262 pdoc = pypandoc.convert_text(shape.documentation, "rst", format="html")263 pdoc = pdoc.strip().split(".")[0] + "."264 output.write(f":param {param_name}: {pdoc}\n")265 # return value266 if operation.output_shape:267 output.write(f":returns: {operation.output_shape.name}\n")268 # errors269 for error in operation.error_shapes:270 output.write(f":raises {error.name}:\n")271 output.write(' """\n')272 output.write(" raise NotImplementedError\n")273@click.command()274@click.argument("service", type=str)275@click.option("--doc/--no-doc", default=False, help="whether or not to generate docstrings")276@click.option(277 "--save/--print",278 default=False,279 help="whether or not to save the result into the api directory",280)281def generate(service: str, doc: bool, save: bool):282 """283 Generate types and API stubs for a given AWS service.284 SERVICE is the service to generate the stubs for (e.g., sqs, or cloudformation)285 """286 from click import ClickException287 try:288 model = load_service(service)289 except UnknownServiceError:290 raise ClickException("unknown service %s" % service)291 output = io.StringIO()292 generate_service_types(output, model, doc=doc)293 generate_service_api(output, model, doc=doc)294 code = output.getvalue()295 try:296 # try to format with black297 from black import FileMode, format_str298 code = format_str(code, mode=FileMode())299 except Exception:300 pass301 if not save:302 # either just print the code to stdout303 click.echo(code)304 return305 # or find the file path and write the code to that location306 here = os.path.dirname(__file__)307 path = os.path.join(here, "api", service)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful