|
1 | 1 | # Code generated by oto; DO NOT EDIT.
|
2 |
| -# |
3 |
| -# As you can see fellow developer, this is not ready. |
4 |
| -# I don't know how to write a good Python SDK - can you help? |
5 |
| -# |
6 |
| -# We need to: |
7 |
| -# |
8 |
| -# 1. Allow users to create a client where they can specify their |
9 |
| -# firesearch endpoint. e.g. http://localhost:8888/api |
10 |
| -# The client should also take in the Secret API key (string), |
11 |
| -# which it will pass as the X-API-Key header. |
12 |
| -# |
13 |
| -# 2. Allow users to create the instance of each service, passing in |
14 |
| -# the client. |
15 |
| -# |
16 |
| -# 3. Put all the methods on the services, and use the strong types |
17 |
| -# for input and output. |
18 |
| -# |
19 |
| -# 4. Make real HTTP requests inside the methods, parsing all the |
20 |
| -# JSON. |
21 | 2 |
|
22 | 3 | import requests
|
| 4 | +import json |
23 | 5 |
|
24 |
| -<%= for (service) in def.Services { %> |
25 |
| -class <%= service.Name %>: |
26 |
| - '''<%= service.Comment %>''' |
27 |
| - pass |
| 6 | +class Client: |
| 7 | + """Client provides access to the Firesearch API.""" |
28 | 8 |
|
29 |
| - <%= for (method) in service.Methods { %> |
30 |
| -// method: <%= method.Name %> |
31 |
| - <% } %> |
| 9 | + def __init__(self, endpoint="http://localhost:8888/api", apiKey=""): |
| 10 | + self.endpoint = endpoint |
| 11 | + self.apiKey = apiKey |
| 12 | + if self.endpoint == "": |
| 13 | + raise FieldError(field="endpoint", message="endpoint missing") |
| 14 | + if self.apiKey == "": |
| 15 | + raise FieldError(field="apiKey", message="apiKey missing") |
| 16 | + |
| 17 | +<%= for (service) in def.Services { %>class <%= service.Name %>: |
| 18 | + """<%= format_comment_line(service.Comment) %>""" |
| 19 | + |
| 20 | + def __init__(self, client): |
| 21 | + self.client = client |
| 22 | + <%= for (method) in service.Methods { %> |
| 23 | + def <%= method.NameLowerCamel %>(<%= method.InputObject.ObjectNameLowerCamel %>): |
| 24 | + """<%= format_comment_line(method.Comment) %>""" |
| 25 | + url = "{}/<%= service.Name %>.<%= method.Name %>".format(self.client.endpoint) |
| 26 | + headers = { |
| 27 | + 'Accept': 'application/json; charset=utf8', |
| 28 | + 'Content-Type': 'application/json; charset=utf8', |
| 29 | + 'X-API-Key': self.client.apiKey, |
| 30 | + } |
| 31 | + r = requests.post(url, json=<%= method.InputObject.ObjectNameLowerCamel %>, headers=headers) |
| 32 | + if r.status_code != 200: |
| 33 | + raise OtoError(message="status code: {}".format(r.status_code)) |
| 34 | + j = r.json() |
| 35 | + if j.error != "": |
| 36 | + raise OtoError(message=j.error) |
| 37 | + return j |
| 38 | + <% } %> |
32 | 39 | <% } %>
|
| 40 | + |
| 41 | +<%= for (object) in def.Objects { %> |
| 42 | +class <%= object.Name %>: |
| 43 | + """<%= format_comment_line(object.Comment) %> |
| 44 | + Attributes |
| 45 | + ---------- |
| 46 | + <%= for (field) in object.Fields { %> |
| 47 | + <%= field.NameLowerCamel %> |
| 48 | + <%= format_comment_line(field.Comment) %> |
| 49 | + <% } %> |
| 50 | + """ |
| 51 | + |
| 52 | + def __init__(self): |
| 53 | + <%= for (field) in object.Fields { %>self.<%= field.NameLowerCamel %> = None |
| 54 | + <% } %> |
| 55 | + def json(): |
| 56 | + """"get a JSON representation of this object""" |
| 57 | + return json.puts(self) |
| 58 | + |
| 59 | +<% } %> |
| 60 | + |
| 61 | +class Error(Exception): |
| 62 | + """Base class for exceptions in this module.""" |
| 63 | + pass |
| 64 | + |
| 65 | +class OtoError(Error): |
| 66 | + """Exception raised for an error making the request. |
| 67 | + |
| 68 | + Attributes: |
| 69 | + message -- explanation of the error |
| 70 | + """ |
| 71 | + |
| 72 | + def __init__(self, message): |
| 73 | + self.message = message |
| 74 | + |
| 75 | +class FieldError(Error): |
| 76 | + """Exception raised for missing fields. |
| 77 | + |
| 78 | + Attributes: |
| 79 | + field -- field which the error occurred |
| 80 | + message -- explanation of the error |
| 81 | + """ |
| 82 | + |
| 83 | + def __init__(self, field, message): |
| 84 | + self.field = field |
| 85 | + self.message = message |
0 commit comments