├── .github └── workflows │ └── pythonpackage.yml ├── .gitignore ├── COPYING ├── README.md ├── etsy2 ├── __init__.py ├── _core.py ├── _v2.py ├── etsy_env.py └── oauth.py ├── pytest.ini ├── setup.py ├── test ├── __init__.py ├── test_core.py └── util.py └── test_v2.py /.github/workflows/pythonpackage.yml: -------------------------------------------------------------------------------- 1 | # This workflow will install Python dependencies, run tests and lint with a variety of Python versions 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions 3 | 4 | name: Python package 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | strategy: 17 | matrix: 18 | python-version: [3.5, 3.6, 3.7, 3.8] 19 | 20 | steps: 21 | - uses: actions/checkout@v2 22 | - name: Set up Python ${{ matrix.python-version }} 23 | uses: actions/setup-python@v1 24 | with: 25 | python-version: ${{ matrix.python-version }} 26 | - name: Install dependencies 27 | run: | 28 | python -m pip install --upgrade pip 29 | # TODO: Move to proper dependency management that includes test deps 30 | pip install flake8 pytest 31 | python setup.py build 32 | python setup.py install 33 | - name: Lint with flake8 34 | run: | 35 | # stop the build if there are Python syntax errors or undefined names 36 | flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics 37 | # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide 38 | flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics 39 | - name: Test with pytest 40 | run: | 41 | pytest 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *egg-info* 3 | build/ 4 | dist/ 5 | *pyc 6 | etsy/config.py* 7 | simplejson*egg 8 | .vscode/ 9 | .env/ 10 | 11 | main.py 12 | release-procedure.txt 13 | todo.txt 14 | out.json 15 | api.json 16 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | etsy-python - Python access to the Etsy API 635 | Copyright (C) 2010 Dan McKinley 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | etsy-python Copyright (C) 2010 Dan McKinley 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Intro 2 | Updated version of [etsy-python](https://github.com/mcfunley/etsy-python) with python3 3 | compatibility and support for the modern etsy api. Please file any bugs, suggestions, 4 | or usage questions as github issues and I will get to them as soon as possible. 5 | 6 | See changelog at bottom of the readme for differences between etsy-python and etsy-python2. The 7 | last release for etsy-python was 0.3.1. 8 | 9 | ## etsy-python2 10 | Python access to the Etsy API 11 | 12 | Originally by Dan McKinley - dan@etsy.com - [http://mcfunley.com](http://mcfunley.com) 13 | 14 | Forked by Sean Scheetz 15 | 16 | **Other Contributors** 17 | 18 | 19 | [James Tatum](https://github.com/jtatum) 20 | 21 | ## Installation 22 | 23 | The simplest way to install the module is pip 24 | 25 |
 26 | $ pip install etsy2
 27 | 
28 | 29 | To install from source, extract the tarball and use the following commands. 30 | 31 |
 32 | $ python setup.py build
 33 | $ sudo python setup.py install
 34 | 
35 | 36 | ## Overview 37 | 38 | This library works by retrieving the api metadata from https://api.etsy.com/v2/?api_key= and patching the methods onto the `Etsy` object at runtime. This happens during 39 | construction of the `Etsy` object (the api client). Refer to the [Etsy API docs](https://www.etsy.com/developers/documentation/reference/apimethod) to see what method names you can call from the `Etsy` object. Every parameter can be passed in as a named parameter to the method call. e.g. 40 | 41 | ```python 42 | etsy.findAllShopReceipts(shop_id=) 43 | ``` 44 | 45 | ## Usage 46 | 47 | There are two types of etsy endpoints: Those that require OAuth and those that don't. For endpoints that don't require OAuth you can use the regular etsy client provided by this library as shown below. 48 | 49 | ```python 50 | from etsy2 import Etsy 51 | 52 | etsy = Etsy(api_key=) 53 | etsy.findAllFeaturedListings() 54 | ``` 55 | 56 | For endpoints that do require OAuth you must pass an `EtsyOAuthClient` to the `Etsy` constructor. 57 | 58 | ```python 59 | etsy_oauth = EtsyOAuthClient(client_key=api_key, 60 | client_secret=shared_secret, 61 | resource_owner_key=oauth_token, 62 | resource_owner_secret=oauth_token_secret) 63 | etsy = Etsy(etsy_oauth_client=etsy_oauth) 64 | ``` 65 | 66 | The `EtsyOAuthClient` requires a client_key, client_secret, resource_owner_key, and resource_owner_secret to be constructed. The client_key and the client_secret are the keystring and shared secret given to you by etsy upon registering your app. The resource_owner_key and resource_owner_secret are the oauth_token and oauth_token_secret that must be retrieved by working through etsy's oauth workflow. See the "Obtaining Etsy OAuthCredentials" section to learn how to get the oauth_token and oauth_token_secret used by the EtsyOAuthClient. 67 | 68 | ## Obtaining Etsy OAuthCredentials 69 | 70 | The `EtsyOAuthHelper` exists to simplify the retrieval of the oauth_token and oauth_token_secret. The first step of the process will always be generating the login_url to which you will redirect the resource owner (user of your application). Usage is shown below. 71 | 72 | ```python 73 | # define permissions scopes as defined in the 'OAuth Authentication' section of the docs 74 | # https://www.etsy.com/developers/documentation/getting_started/oauth#section_permission_scopes 75 | permission_scopes = ['transactions_r', 'listings_r'] 76 | 77 | # In this case, user will be redirected to page on etsy that shows the verification code. 78 | login_url, temp_oauth_token_secret = \ 79 | EtsyOAuthHelper.get_request_url_and_token_secret(api_key, shared_secret, permission_scopes) 80 | 81 | # In this case, user will be redirected to the callback_url after telling etsy to allow access to their data. 82 | login_url, temp_oauth_token_secret = \ 83 | EtsyOAuthHelper.get_request_url_and_token_secret(api_key, shared_secret, permission_scopes, callback_url) 84 | 85 | # Note, 86 | # login_url is the url to redirect the user to have them authenticate with etsy. 87 | # temp_oauth_token_secret is the secret used in the get_ouath_token methods to retrieve permanent oauth credentials. 88 | ``` 89 | 90 | After your user has told Etsy they want to give you access to their data, the next step is to trade your temp_oauth_token and temp_oauth_token_secret for the permanent oauth_token and oauth_token_secret. There are two different paths for achieving this and which one you take depends on if you specified a callback_url in your request to `EtsyOAuthHelper.get_request_url_and_token_secret`. 91 | 92 | ### If a callback url was not specified 93 | 94 | If you did not specify a callback url, the user will be redirected to a page owned by etsy that displays the verification code. You need to pass this verification code along with the temp_oauth_token and temp_oauth_token_secret to the `EtsyOAuthHelper.get_oauth_token_via_verifier` method to retrieve the final tokens expected by the `EtsyOAuthClient`. This is done as shown below. 95 | 96 | ```python 97 | oauth_token, oauth_token_secret = \ 98 | EtsyOAuthHelper.get_oauth_token_via_verifier(api_key, shared_secret, temp_oauth_token, temp_oauth_token_secret, verifier) 99 | 100 | # Note, 101 | # temp_oauth_token is part of the login_url generated by get_request_url_and_token_secret 102 | # temp_oauth_token_secret is returned by get_request_url_and_token_secret 103 | # verifier is the verification code on the screen etsy redirects the user to 104 | ``` 105 | 106 | The oauth_token and oauth_token_secret obtained from this step are the tokens expected by the `EtsyOAuthClient`. 107 | 108 | It is important to note that temp_oauth_token is part of the login_url generated by get_request_url_and_token_secret. If you are going to use `EtsyOAuthHelper.get_oauth_token_via_verifier` you need to parse the temp_oauth_token from the login url before it can be passed as a parameter. This can be done as shown below. 109 | 110 | ```python 111 | import urllib.parse as urlparse 112 | from urllib.parse import parse_qs 113 | 114 | login_url, temp_oauth_token_secret = \ 115 | EtsyOAuthHelper.get_request_url_and_token_secret(api_key, shared_secret, permission_scopes, auth_callback_url) 116 | 117 | query = urlparse.urlparse(login_url).query 118 | temp_oauth_token = parse_qs(query)['oauth_token'][0] 119 | ``` 120 | 121 | ### If a callback url was specified 122 | 123 | If you specified a callback_url, etsy will redirect the user to that url after the user grants access to your application. Etsy will append the temp_oauth_token and the verifier as query strings to callback_url. You should pass the full url the the user was redirected to to `EtsyOAuthHelper.get_oauth_token_via_auth_url` as shown below. 124 | 125 | ```python 126 | oauth_token, oauth_token_secret = \ 127 | EtsyOAuthHelper.get_oauth_token_via_auth_url(api_key, shared_secret, temp_oauth_token_secret, auth_url) 128 | 129 | # Note, 130 | # temp_oauth_token_secret is returned from get_request_url_and_token_secret 131 | # auth_url is the url the user was redirected to by etsy 132 | ``` 133 | 134 | `EtsyOAuthHelper.get_oauth_token_via_auth_url` will obtain the temp_oauth_token and the verifier from the auth_url. The oauth_token and oauth_token_secret obtained from this step are the tokens expected by the `EtsyOAuthClient`. 135 | 136 | 137 | ## Configuration 138 | 139 | For convenience (and to avoid storing API keys in revision control 140 | systems), the package supports local configuration. You can manage 141 | your API keys in a file called $HOME/etsy/keys (or the equivalent on 142 | Windows) with the following format: 143 | 144 |
145 | v2 = 'Etsy API version 2 key goes here'
146 | 
147 | 148 | Alternatively, you can specify a different key file when creating an API object. 149 | 150 |
151 | from etsy import Etsy
152 | 
153 | api = Etsy(key_file='/usr/share/etsy/keys')
154 | 
155 | 156 | (Implementation note: the keys file can be any valid python script that defines 157 | a module-level variable for the API version you are trying to use.) 158 | 159 | ## Tests 160 | 161 | This package comes with a reasonably complete unit test suite. In order to run 162 | the tests, use: 163 | 164 |
165 | $ python setup.py test
166 | 
167 | 168 | Some tests (those that actually call the Etsy API) require your API key 169 | to be locally configured. See the Configuration section, above. 170 | 171 | 172 | ## Method Table Caching 173 | 174 | As mentioned above, this module is implemented by metaprogramming against the method table 175 | published by the Etsy API. In other words, API methods are not explicitly declared by the 176 | code in this module. Instead, the list of allowable methods is downloaded and 177 | the patched into the API objects at runtime. 178 | 179 | This has advantages and disadvantages. It allows the module to automatically 180 | receive new features, but on the other hand, this process is not as fast as 181 | explicitly declared methods. 182 | 183 | In order to speed things up, the method table json is cached locally by default. 184 | If a $HOME/etsy directory exists, the cache file is created there. Otherwise, it 185 | is placed in the machine's temp directory. By default, this cache lasts 24 hours. 186 | 187 | The cache file can be specified when creating an API object: 188 | 189 | ```python 190 | from etsy import Etsy 191 | 192 | api = Etsy(method_cache='myfile.json') 193 | ``` 194 | 195 | Method table caching can also be disabled by passing None as the cache parameter: 196 | 197 | ```python 198 | from etsy import Etsy 199 | 200 | # do not cache methods 201 | api = Etsy(method_cache=None) 202 | ``` 203 | 204 | 205 | ## Version History 206 | 207 | ### Version 0.7.0 208 | - Url parameters now sent in the url instead of the query string. 209 | - Url parameters now passed like every other parameter instead of optionally being able to be passed as positional arguments. Improves api consistency and cuts down on error cases. 210 | - Patched metadata for submitTracking during runtime to workaround inconsistency in etsy api metadata. 211 | 212 | ### Version 0.6.0 213 | - Added get_oauth_token_via_verifier to EtsyOAuthHelper. This allows users to obtain oauth credentials by manually passing the verification code 214 | rather than using a callback_url. 215 | 216 | ### Version 0.5.0 217 | - changed module name from etsy to etsy2 to match the package name on pypi (thanks to [James Tatum](https://github.com/jtatum)). 218 | 219 | ### Version 0.4.0 220 | - Added python 3 compatability 221 | - Removed EtsySandboxEnv because etsy doesn't seem to have a sandbox env anymore. 222 | - Fixed broken EtsyOauthClient because etsy now rejects calls including the api_key param when oauth is being used. 223 | - Replaced simplejson with builtin json, replaced python-oauth2 with requests-oauthlib (python-oauth2 only supports up to python 3.4). 224 | - Removed the oauth credential retrieval methods from EtsyOAuthClient to make client usage easier. 225 | - Created EtsyOAuthHelper to make retrieving the etsy oauth credentials easier. 226 | - Added helpers to make getting oauth credentials from etsy easier. 227 | - Added basic support for PUT and DELETE methods (which the etsy api didnt have when this was originally written) 228 | 229 | ### Version 0.3.1 230 | * Allowing Python Longs to be passed for parameters declared as "integers" by the API 231 | (thanks to [Marc Abramowitz](http://marc-abramowitz.com)). 232 | 233 | 234 | ### Version 0.3 235 | * Support for Etsy API v2 thanks to [Marc Abramowitz](http://marc-abramowitz.com). 236 | * Removed support for now-dead Etsy API v1. 237 | 238 | 239 | ### Version 0.2.1 240 | * Added a cache for the method table json. 241 | * Added a logging facility. 242 | 243 | 244 | ### Version 0.2 - 05-31-2010 245 | * Added local configuration (~/.etsy) to eliminate cutting & pasting of api keys. 246 | * Added client-side type checking for parameters. 247 | * Added support for positional arguments. 248 | * Added a test suite. 249 | * Began differentiation between API versions. 250 | * Added module to PyPI. 251 | 252 | ### Version 0.1 - 05-24-2010 253 | Initial release 254 | -------------------------------------------------------------------------------- /etsy2/__init__.py: -------------------------------------------------------------------------------- 1 | from ._v2 import EtsyV2 as Etsy 2 | from .etsy_env import EtsyEnvProduction 3 | 4 | 5 | __version__ = '0.7.0' 6 | __author__ = 'Sean Scheetz' 7 | __copyright__ = 'Copyright 2010, Etsy Inc.' 8 | __license__ = 'GPL v3' 9 | __email__ = 'file_an_issue_on_github@gmail.com' 10 | 11 | -------------------------------------------------------------------------------- /etsy2/_core.py: -------------------------------------------------------------------------------- 1 | from __future__ import with_statement 2 | import json 3 | from urllib.parse import urlencode, quote 4 | import os 5 | import re 6 | import tempfile 7 | import mimetypes 8 | import time 9 | import requests 10 | 11 | 12 | missing = object() 13 | 14 | 15 | 16 | 17 | class TypeChecker(object): 18 | def __init__(self): 19 | self.checkers = { 20 | 'int': self.check_int, 21 | 'float': self.check_float, 22 | 'string': self.check_string, 23 | 'boolean': self.check_boolean 24 | } 25 | 26 | 27 | def __call__(self, method, **kwargs): 28 | params = method['params'] 29 | for k, v in kwargs.items(): 30 | if k == 'includes': continue 31 | 32 | if k not in params: 33 | raise ValueError('Unexpected argument: %s=%s' % (k, v)) 34 | 35 | t = params[k] 36 | checker = self.checkers.get(t, None) or self.compile(t) 37 | ok, converted = checker(v) 38 | if not ok: 39 | raise ValueError( 40 | "Bad value for parameter %s of type '%s' - %s" % (k, t, v)) 41 | kwargs[k] = converted 42 | 43 | 44 | def compile(self, t): 45 | if t.startswith('enum'): 46 | f = self.compile_enum(t) 47 | else: 48 | # TODO write checkers for complex etsy types 49 | # https://www.etsy.com/developers/documentation/getting_started/api_basics#section_parameter_types 50 | f = self.always_ok 51 | self.checkers[t] = f 52 | return f 53 | 54 | 55 | def compile_enum(self, t): 56 | terms = [x.strip() for x in t[5:-1].split(',')] 57 | def check_enum(value): 58 | return (value in terms), value 59 | return check_enum 60 | 61 | 62 | def always_ok(self, value): 63 | return True, value 64 | 65 | 66 | def check_int(self, value): 67 | return isinstance(value, int), value 68 | 69 | 70 | def check_float(self, value): 71 | if isinstance(value, int): 72 | return True, value 73 | return isinstance(value, float), value 74 | 75 | 76 | def check_string(self, value): 77 | return isinstance(value, str), value 78 | 79 | 80 | def check_boolean(self, value): 81 | return isinstance(value, bool), value 82 | 83 | 84 | class APIMethod(object): 85 | def __init__(self, api, spec): 86 | """ 87 | Parameters: 88 | api - API object that this method is associated with. 89 | spec - dict with the method specification; e.g.: 90 | 91 | {'name': 'createListing', 'uri': '/listings', 'visibility': 92 | 'private', 'http_method': 'POST', 'params': {'description': 93 | 'text', 'tags': 'array(string)', 'price': 'float', 'title': 94 | 'string', 'materials': 'array(string)', 'shipping_template_id': 95 | 'int', 'quantity': 'int', 'shop_section_id': 'int'}, 'defaults': 96 | {'materials': None, 'shop_section_id': None}, 'type': 'Listing', 97 | 'description': 'Creates a new Listing'} 98 | """ 99 | 100 | self.api = api 101 | self.spec = spec 102 | self.type_checker = self.api.type_checker 103 | self.__doc__ = self.spec['description'] 104 | self.compiled = False 105 | 106 | # HACK: etsy api metadata isn't correct for submitTracking. 107 | # We patch the correct data here. 108 | if self.spec['name'] == 'submitTracking': 109 | self.spec['params']['shop_id'] = 'shop_id_or_name' 110 | self.spec['params']['receipt_id'] = 'int' 111 | 112 | 113 | def __call__(self, **kwargs): 114 | if not self.compiled: 115 | self.compile() 116 | return self.invoke(**kwargs) 117 | 118 | 119 | def compile(self): 120 | uri = self.spec['uri'] 121 | self.uri_params = [uri_param for uri_param in uri.split('/') if uri_param.startswith(':')] 122 | self.compiled = True 123 | 124 | 125 | def invoke(self, **kwargs): 126 | ps = {} 127 | for p in self.uri_params: 128 | # remove the starting ":" from the param 129 | kwarg_key = p[1:] 130 | if p[1:] not in kwargs: 131 | raise ValueError("Required argument '%s' not provided." % kwarg_key) 132 | # need to remove : from ps key as weel so it can be used in type_checker 133 | ps[kwarg_key] = kwargs[kwarg_key] 134 | del kwargs[kwarg_key] 135 | 136 | self.type_checker(self.spec, **ps) 137 | self.type_checker(self.spec, **kwargs) 138 | 139 | applied_url = self.spec['uri'] 140 | for key, value in ps.items(): 141 | applied_url = applied_url.replace(":" + key, quote(str(value))) 142 | return self.api._get(self.spec['http_method'], applied_url, **kwargs) 143 | 144 | 145 | 146 | 147 | class MethodTableCache(object): 148 | max_age = 60*60*24 149 | 150 | def __init__(self, api, method_cache): 151 | self.api = api 152 | self.filename = self.resolve_file(method_cache) 153 | self.used_cache = False 154 | self.wrote_cache = False 155 | 156 | 157 | def resolve_file(self, method_cache): 158 | if method_cache is missing: 159 | return self.default_file() 160 | return method_cache 161 | 162 | 163 | def etsy_home(self): 164 | return self.api.etsy_home() 165 | 166 | 167 | def default_file(self): 168 | etsy_home = self.etsy_home() 169 | d = etsy_home if os.path.isdir(etsy_home) else tempfile.gettempdir() 170 | return os.path.join(d, 'methods.%s.json' % self.api.api_version) 171 | 172 | 173 | def get(self): 174 | ms = self.get_cached() 175 | if not ms: 176 | ms = self.api.get_method_table() 177 | self.cache(ms) 178 | return ms 179 | 180 | 181 | def get_cached(self): 182 | if self.filename is None or not os.path.isfile(self.filename): 183 | self.api.log('Not using cached method table.') 184 | return None 185 | if time.time() - os.stat(self.filename).st_mtime > self.max_age: 186 | self.api.log('Method table too old.') 187 | return None 188 | with open(self.filename, 'r') as f: 189 | self.used_cache = True 190 | self.api.log('Reading method table cache: %s' % self.filename) 191 | return json.loads(f.read()) 192 | 193 | 194 | def cache(self, methods): 195 | if self.filename is None: 196 | self.api.log('Method table caching disabled, not writing new cache.') 197 | return 198 | with open(self.filename, 'w') as f: 199 | json.dump(methods, f) 200 | self.wrote_cache = True 201 | self.api.log('Wrote method table cache: %s' % self.filename) 202 | 203 | 204 | 205 | 206 | class API(object): 207 | def __init__(self, api_key='', key_file=None, method_cache=missing, 208 | log=None): 209 | """ 210 | Creates a new API instance. When called with no arguments, 211 | reads the appropriate API key from the default ($HOME/.etsy/keys) 212 | file. 213 | 214 | Parameters: 215 | api_key - An explicit API key to use. 216 | key_file - A file to read the API keys from. 217 | method_cache - A file to save the API method table in for 218 | 24 hours. This speeds up the creation of API 219 | objects. 220 | log - An callable that accepts a string parameter. 221 | Receives log messages. No logging is done if 222 | this is None. 223 | 224 | Only one of api_key and key_file may be passed. 225 | 226 | If method_cache is explicitly set to None, no method table 227 | caching is performed. If the parameter is not passed, a file in 228 | $HOME/.etsy is used if that directory exists. Otherwise, a 229 | temp file is used. 230 | """ 231 | if not getattr(self, 'api_url', None): 232 | raise AssertionError('No api_url configured.') 233 | 234 | if self.api_url.endswith('/'): 235 | raise AssertionError('api_url should not end with a slash.') 236 | 237 | if not getattr(self, 'api_version', None): 238 | raise AssertionError('API object should define api_version') 239 | 240 | if api_key and key_file: 241 | raise AssertionError('Keys can be read from a file or passed, ' 242 | 'but not both.') 243 | 244 | if api_key: 245 | self.api_key = api_key 246 | elif key_file: 247 | self.api_key = self._read_key(key_file) 248 | 249 | self.log = log or self._ignore 250 | if not callable(self.log): 251 | raise ValueError('log must be a callable.') 252 | 253 | self.type_checker = TypeChecker() 254 | 255 | self.decode = json.loads 256 | 257 | self.log('Creating %s Etsy API, base url=%s.' % ( 258 | self.api_version, self.api_url)) 259 | self._get_methods(method_cache) 260 | 261 | 262 | 263 | def _ignore(self, _): 264 | pass 265 | 266 | 267 | def _get_methods(self, method_cache): 268 | self.method_cache = MethodTableCache(self, method_cache) 269 | ms = self.method_cache.get() 270 | self._methods = dict([(m['name'], m) for m in ms]) 271 | 272 | for method in ms: 273 | setattr(self, method['name'], APIMethod(self, method)) 274 | 275 | # self.log('API._get_methods: self._methods = %r' % self._methods) 276 | 277 | 278 | def etsy_home(self): 279 | return os.path.expanduser('~/.etsy') 280 | 281 | 282 | def get_method_table(self): 283 | cache = self._get('GET', '/') 284 | return cache 285 | 286 | 287 | def _read_key(self, key_file): 288 | key_file = key_file or os.path.join(self.etsy_home(), 'keys') 289 | if not os.path.isfile(key_file): 290 | raise AssertionError( 291 | "The key file '%s' does not exist. Create a key file or " 292 | 'pass an API key explicitly.' % key_file) 293 | 294 | gs = {} 295 | with open(key_file) as kf: 296 | exec(kf.read(), gs) 297 | return gs[self.api_version] 298 | 299 | 300 | def _get_url(self, url, http_method, data): 301 | self.log("API._get_url: url = %r" % url) 302 | return requests.request(http_method, url, data=data) 303 | 304 | def _get(self, http_method, url, **kwargs): 305 | if hasattr(self, 'api_key'): 306 | kwargs.update(dict(api_key=self.api_key)) 307 | 308 | data = None 309 | if http_method == 'GET' or http_method == 'DELETE': 310 | url = '%s%s' % (self.api_url, url) 311 | if kwargs: 312 | url += '?%s' % urlencode(kwargs) 313 | elif http_method == 'POST' or http_method == 'PUT': 314 | url = '%s%s' % (self.api_url, url) 315 | 316 | data = {} 317 | for name, value in kwargs.items(): 318 | if hasattr(value, 'read'): 319 | file_mimetype = mimetypes.guess_type(value.name) or 'application/octet-stream' 320 | data[name] = (value.name, value.read(), file_mimetype) 321 | else: 322 | data[name] = (None, str(value)) 323 | 324 | self.last_url = url 325 | response = self._get_url(url, http_method, data) 326 | 327 | self.log('API._get: http_method = %r, url = %r, data = %r' % (http_method, url, data)) 328 | 329 | try: 330 | self.data = self.decode(response.text) 331 | except json.JSONDecodeError: 332 | raise ValueError('Could not decode response from Etsy as JSON: status_code: %r, text: %r, url %r' \ 333 | % (response.status_code, response.text, response.url)) 334 | 335 | self.count = self.data['count'] 336 | return self.data['results'] 337 | -------------------------------------------------------------------------------- /etsy2/_v2.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | from ._core import API, missing 3 | from .etsy_env import EtsyEnvProduction 4 | 5 | class EtsyV2(API): 6 | api_version = 'v2' 7 | 8 | def __init__(self, api_key='', key_file=None, method_cache=missing, 9 | etsy_env=EtsyEnvProduction(), log=None, etsy_oauth_client=None): 10 | self.api_url = etsy_env.api_url 11 | self.etsy_oauth_client = None 12 | 13 | if etsy_oauth_client: 14 | self.etsy_oauth_client = etsy_oauth_client 15 | # including api_key in requests when using oauth causes etsy to return 403 Forbidden 16 | api_key = None 17 | key_file = None 18 | 19 | super(EtsyV2, self).__init__(api_key, key_file, method_cache, log) 20 | 21 | def _get_url(self, url, http_method, body): 22 | if self.etsy_oauth_client is not None: 23 | return self.etsy_oauth_client.do_oauth_request(url, http_method, body) 24 | return API._get_url(self, url, http_method, body) 25 | -------------------------------------------------------------------------------- /etsy2/etsy_env.py: -------------------------------------------------------------------------------- 1 | class EtsyEnvProduction(object): 2 | request_token_url = 'https://openapi.etsy.com/v2/oauth/request_token' 3 | access_token_url = 'https://openapi.etsy.com/v2/oauth/access_token' 4 | signin_url = 'https://www.etsy.com/oauth/signin' 5 | api_url = 'https://openapi.etsy.com/v2' 6 | -------------------------------------------------------------------------------- /etsy2/oauth.py: -------------------------------------------------------------------------------- 1 | import requests 2 | from urllib.parse import quote 3 | from requests_oauthlib import OAuth1Session 4 | from .etsy_env import EtsyEnvProduction 5 | 6 | # TODO add support for generating the oauth credentials - may want to inherit from OAuth1Session 7 | class EtsyOAuthClient(): 8 | ''' 9 | You must perform the oauth authentication on your own. This client 10 | expects valid oauth crendentials for the requesting user. 11 | 12 | client_key is keystring for the etsy app. 13 | client_secret is the shared secret for the etsy app. 14 | resource_owner_key is the oauth_token for the user whose data is being retrieved. 15 | resource_owner_secret is the oauth_token_secret for the user whose data is being retrieved. 16 | ''' 17 | def __init__(self, client_key, client_secret, resource_owner_key, resource_owner_secret, logger=None): 18 | self.oauth1Session = OAuth1Session(client_key, 19 | client_secret=client_secret, 20 | resource_owner_key=resource_owner_key, 21 | resource_owner_secret=resource_owner_secret) 22 | self.logger = logger 23 | 24 | def do_oauth_request(self, url, http_method, data): 25 | # TODO data seems to work for PUT and POST /listing. See if data 26 | # can handle image/actual file data updates if so don't need to split path. 27 | if (http_method == "POST"): 28 | response = self.oauth1Session.request(http_method, url, files=data) 29 | else: 30 | response = self.oauth1Session.request(http_method, url, data=data) 31 | 32 | if self.logger: 33 | self.logger.debug('do_oauth_request: response = %r' % response) 34 | 35 | return response 36 | 37 | class EtsyOAuthHelper: 38 | ''' 39 | Used to get the oauth token for the user you want to make requests with. 40 | TODO there might be a good way to make a class out of this, but because the 41 | methods are called in separate http requests the state would be lost in between 42 | calls. 43 | ''' 44 | @staticmethod 45 | def get_request_url_and_token_secret(api_key, shared_secret, permission_scopes=[], callback_uri=None, etsy_env=EtsyEnvProduction()): 46 | ''' 47 | This method implements the first step of the Oauth1.0 3-legged work flow. 48 | 49 | Returns the the login_url for the user you wish to authenticate with your app 50 | and the oauth_token_secret which must be passed to the get_oauth_token method 51 | (the next step in the oauth_workflow). You probably want to redirect the user 52 | to the login_url after this step. 53 | 54 | api_key is the keystring from etsy 55 | shared_secret is the shared secret from etsy 56 | permission_scopes is a list of strings. one string per requested permission scope. See link below. 57 | https://www.etsy.com/developers/documentation/getting_started/oauth#section_permission_scopes 58 | callback_uri is a path in your application where the user should be redirected after login 59 | etsy_env is always prod because there is only one etsy environment as of now 60 | ''' 61 | oauth = OAuth1Session(api_key, client_secret=shared_secret, callback_uri=callback_uri) 62 | 63 | request_token_url = etsy_env.request_token_url 64 | if (permission_scopes): 65 | permissions = ' '.join(permission_scopes) 66 | request_token_url += '?scope=' + quote(permissions) 67 | 68 | request_token_response = oauth.fetch_request_token(request_token_url) 69 | 70 | login_url = request_token_response['login_url'] 71 | temp_oauth_token_secret = request_token_response['oauth_token_secret'] 72 | 73 | return (login_url, temp_oauth_token_secret) 74 | 75 | @staticmethod 76 | def get_oauth_token_via_auth_url(api_key, shared_secret, oauth_token_secret, auth_url, etsy_env=EtsyEnvProduction()): 77 | ''' 78 | Retrieves the oauth_token and oauth_token_secret for the user. These are 79 | used along with the api_key, shared_secret, and oauth_token_secret (which 80 | is returned by get_request_url_and_token_secret) to create an EtsyOAuthClient. 81 | 82 | Differs from get_oauth_token_via_verifier as the auth_url containing the verifier and temp_oauth_token 83 | is passed in as a parameter. The auth_url is the url etsy redirected the user to after they allowed your app 84 | access to their data. It is the callback_url specified in get_request_url_and_token_secret with the query string 85 | etsy appended to it. This function will get the verifier and oauth_token from the auth_url query parameters. 86 | 87 | api_key is the keystring from etsy. 88 | shared_secret is the shared secret from etsy. 89 | oauth_token_secret is the token_secret returned from get_request_url_and_token_secret. 90 | auth_url is the url etsy redirected you to e.g. it is the callback url you specified 91 | in get_request_url_and_token_secret with the query string etsy appended to it. 92 | etsy_env is always prod because there is only one etsy environment as of now. 93 | ''' 94 | oauth = OAuth1Session(api_key, shared_secret) 95 | oauth_response = oauth.parse_authorization_response(auth_url) 96 | oauth = OAuth1Session(api_key, 97 | client_secret=shared_secret, 98 | resource_owner_key=oauth_response['oauth_token'], 99 | resource_owner_secret=oauth_token_secret, 100 | verifier=oauth_response['oauth_verifier']) 101 | 102 | oauth_tokens = oauth.fetch_access_token(etsy_env.access_token_url) 103 | oauth_token = oauth_tokens['oauth_token'] 104 | oauth_token_secret = oauth_tokens['oauth_token_secret'] 105 | 106 | return (oauth_token, oauth_token_secret) 107 | 108 | @staticmethod 109 | def get_oauth_token_via_verifier(api_key, shared_secret, temp_oauth_token, temp_oauth_token_secret, verifier, etsy_env=EtsyEnvProduction()): 110 | ''' 111 | Retrieves the oauth_token and oauth_token_secret for the user. These are 112 | used along with the api_key, shared_secret, and oauth_token_secret (which 113 | is returned by get_request_url_and_token_secret) to create an EtsyOAuthClient. 114 | 115 | Differs from get_oauth_token_via_auth_url as the temp_oauth_token and verifier are passed 116 | as params rather than retrieved from the auth_url. The temp_oauth_token is the oauth_token 117 | query string param from the login_url generated in get_request_url_and_token_secret. 118 | 119 | api_key is the keystring from etsy. 120 | shared_secret is the shared secret from etsy. 121 | temp_oauth_token is the oauth_token query string param from the login_url generated 122 | in get_request_url_and_token_secret. 123 | temp_oauth_token_secret is the token_secret returned from get_request_url_and_token_secret. 124 | verifier is the verification code on the page etsy redirects you to if no callback url 125 | was specified in get_request_url_and_token_secret. 126 | etsy_env is always prod because there is only one etsy environment as of now. 127 | ''' 128 | oauth = OAuth1Session(api_key, shared_secret) 129 | oauth = OAuth1Session(api_key, 130 | client_secret=shared_secret, 131 | resource_owner_key=temp_oauth_token, 132 | resource_owner_secret=temp_oauth_token_secret, 133 | verifier=verifier) 134 | 135 | oauth_tokens = oauth.fetch_access_token(etsy_env.access_token_url) 136 | oauth_token = oauth_tokens['oauth_token'] 137 | oauth_token_secret = oauth_tokens['oauth_token_secret'] 138 | 139 | return (oauth_token, oauth_token_secret) 140 | -------------------------------------------------------------------------------- /pytest.ini: -------------------------------------------------------------------------------- 1 | [pytest] 2 | testpaths = test 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import os 3 | from setuptools import setup 4 | 5 | this_dir = os.path.realpath(os.path.dirname(__file__)) 6 | long_description = open(os.path.join(this_dir, 'README.md'), 'r').read() 7 | 8 | setup( 9 | name = 'etsy2', 10 | version = '0.7.0', 11 | author = 'Sean Scheetz', 12 | author_email = 'contact_through_github@gmail.com', 13 | description = 'Python access to the Etsy API', 14 | license = 'GPL v3', 15 | keywords = 'etsy api handmade', 16 | packages = ['etsy2'], 17 | long_description = long_description, 18 | long_description_content_type="text/markdown", 19 | test_suite = 'test', 20 | project_urls = { 21 | 'Source Code': 'https://github.com/sscheetz/etsy-python2' 22 | }, 23 | install_requires=['requests_oauthlib'], 24 | ) -------------------------------------------------------------------------------- /test/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /test/test_core.py: -------------------------------------------------------------------------------- 1 | from urllib.parse import urlparse, parse_qs 2 | import os 3 | import tempfile 4 | 5 | from etsy2._core import API, MethodTableCache, missing 6 | from .util import Test 7 | 8 | 9 | class MockResponse(): 10 | text = '{ "count": 2, "results": [1, 2] }' 11 | 12 | 13 | class MockAPI(API): 14 | api_url = 'http://host' 15 | api_version = 'v1' 16 | 17 | 18 | def etsy_home(self): 19 | return Test.scratch_dir 20 | 21 | 22 | def get_method_table(self, *args): 23 | return [{'name': 'testMethod', 24 | 'uri': '/test/:test_id', 25 | 'http_method': 'GET', 26 | 'params': { 27 | 'limit': 'int', 28 | 'test_id': 'user_id_or_name', 29 | 'offset': 'int', 30 | 'fizz': 'enum(foo, bar, baz)', 31 | 'buzz': 'float', 32 | 'blah': 'unknown type', 33 | 'kind': 'string', 34 | 'duh': 'boolean' 35 | }, 36 | 'type': 'int', 37 | 'description': 'test method.'}, 38 | {'name': 'method2', 39 | 'uri': '/blah', 40 | 'http_method': 'GET', 41 | 'params': {'foo': 'int'}, 42 | 'type': 'int', 43 | 'description': 'so we can assert against 2 for test'}] 44 | 45 | 46 | def _get_url(self, url, http_method, data): 47 | return MockResponse() 48 | 49 | 50 | 51 | class MockLog(object): 52 | def __init__(self, test): 53 | self.lines = [] 54 | self.test = test 55 | 56 | def __call__(self, msg): 57 | self.lines.append(msg) 58 | 59 | 60 | def assertLine(self, msg): 61 | failmsg = 'Could not find "%s" in the log. The log was:\n\n%s' % ( 62 | msg, '\n'.join([' %s' % x for x in self.lines])) 63 | self.test.assertTrue(msg in self.lines, failmsg) 64 | 65 | 66 | 67 | class CoreTests(Test): 68 | def setUp(self): 69 | self.api = MockAPI('apikey', log=MockLog(self), method_cache=None) 70 | 71 | 72 | def last_query(self): 73 | qs = urlparse(self.api.last_url).query 74 | return parse_qs(qs) 75 | 76 | 77 | def test_method_created(self): 78 | self.assertTrue('testMethod' in dir(self.api)) 79 | 80 | 81 | def test_url_params(self): 82 | self.api.testMethod(test_id='foo') 83 | self.assertEqual(self.api.last_url, 84 | 'http://host/test/foo?api_key=apikey') 85 | 86 | 87 | def test_count_saved(self): 88 | self.api.testMethod(test_id='foo') 89 | self.assertEqual(self.api.count, 2) 90 | 91 | 92 | def test_results_returned(self): 93 | x = self.api.testMethod(test_id='foo') 94 | self.assertEqual(x, [1,2]) 95 | 96 | 97 | def test_query_params(self): 98 | self.api.testMethod(test_id='foo', limit=1) 99 | self.assertEqual(self.last_query(), { 100 | 'api_key': ['apikey'], 101 | 'limit': ['1'], 102 | }) 103 | 104 | 105 | def test_docstring_set(self): 106 | self.assertEqual(self.api.testMethod.__doc__, 107 | 'test method.') 108 | 109 | 110 | 111 | def test_api_url_required(self): 112 | msg = self.assertRaises(AssertionError, API, '') 113 | self.assertEqual('No api_url configured.', msg) 114 | 115 | 116 | def test_api_url_cannot_end_with_slash(self): 117 | class Foo(API): 118 | api_url = 'http://host/' 119 | 120 | msg = self.assertRaises(AssertionError, Foo, '') 121 | self.assertEqual('api_url should not end with a slash.', msg) 122 | 123 | 124 | def test_api_should_define_version(self): 125 | class Foo(API): 126 | api_url = 'http://host' 127 | 128 | msg = self.assertRaises(AssertionError, Foo) 129 | self.assertEqual(msg, 'API object should define api_version') 130 | 131 | 132 | def test_key_file_does_not_exist(self): 133 | msg = self.assertRaises(AssertionError, MockAPI, 134 | key_file='this does not exist') 135 | self.assertTrue("'this does not exist' does not exist" in msg) 136 | 137 | 138 | def test_reading_api_key(self): 139 | with open('testkeys', 'w') as f: 140 | f.write("v1 = 'abcdef'") 141 | try: 142 | self.assertEqual(MockAPI(key_file='testkeys').api_key, 'abcdef') 143 | finally: 144 | os.unlink('testkeys') 145 | 146 | 147 | def test_unrecognized_kwarg(self): 148 | msg = self.assertRaises(ValueError, self.api.testMethod, 149 | test_id=1, not_an_arg=1) 150 | self.assertEqual(msg, 'Unexpected argument: not_an_arg=1') 151 | 152 | 153 | def test_unknown_parameter_type_is_passed(self): 154 | self.api.testMethod(test_id=1, blah=1) 155 | self.assertEqual(self.last_query()['blah'], ['1']) 156 | 157 | 158 | def test_missing_required_param(self): 159 | msg = self.assertRaises(ValueError, self.api.testMethod, limit=5.6) 160 | self.assertEqual(msg, "Required argument 'test_id' not provided.") 161 | 162 | 163 | def test_parameter_type_int(self): 164 | self.api.testMethod(test_id=1, limit=5) 165 | self.assertEqual(self.last_query()['limit'], ['5']) 166 | 167 | 168 | def test_parameter_type_boolean(self): 169 | self.api.testMethod(test_id=1, duh=True) 170 | self.assertEqual(self.last_query()['duh'], ['True']) 171 | 172 | 173 | def bad_value_msg(self, name, t, v): 174 | return "Bad value for parameter %s of type '%s' - %s" % (name, t, v) 175 | 176 | def test_invalid_parameter_type_int(self): 177 | msg = self.assertRaises(ValueError, self.api.testMethod, 178 | test_id=1, limit=5.6) 179 | self.assertEqual(msg, self.bad_value_msg('limit', 'int', 5.6)) 180 | 181 | 182 | def test_parameter_type_float(self): 183 | self.api.testMethod(test_id=1, buzz=42.1) 184 | self.assertEqual(self.last_query()['buzz'], ['42.1']) 185 | 186 | 187 | def test_invalid_parameter_type_float(self): 188 | msg = self.assertRaises(ValueError, self.api.testMethod, 189 | test_id=1, buzz='x') 190 | self.assertEqual(msg, self.bad_value_msg('buzz', 'float', 'x')) 191 | 192 | 193 | def test_int_accepted_as_float(self): 194 | self.api.testMethod(test_id=1, buzz=3) 195 | self.assertEqual(self.last_query()['buzz'], ['3']) 196 | 197 | 198 | def test_parameter_type_enum(self): 199 | self.api.testMethod(test_id=1, fizz='bar') 200 | self.assertEqual(self.last_query()['fizz'], ['bar']) 201 | 202 | 203 | def test_invalid_parameter_type_enum(self): 204 | msg = self.assertRaises(ValueError, self.api.testMethod, 205 | test_id=1, fizz='goo') 206 | self.assertEqual(msg, self.bad_value_msg( 207 | 'fizz', 'enum(foo, bar, baz)', 'goo')) 208 | 209 | 210 | def test_parameter_type_string(self): 211 | self.api.testMethod(test_id=1, kind='blah') 212 | self.assertEqual(self.last_query()['kind'], ['blah']) 213 | 214 | 215 | def test_invalid_parameter_type_string(self): 216 | msg = self.assertRaises(ValueError, self.api.testMethod, 217 | test_id=1, kind=Test) 218 | self.assertEqual(msg, self.bad_value_msg('kind', 'string', Test)) 219 | 220 | 221 | def test_api_key_and_key_file_both_passed(self): 222 | msg = self.assertRaises(AssertionError, MockAPI, 223 | api_key='x', key_file='y') 224 | self.assertEqual('Keys can be read from a file or passed, but not both.', 225 | msg) 226 | 227 | 228 | def test_logging_works(self): 229 | self.api.log('foo') 230 | self.api.log.assertLine('foo') 231 | 232 | 233 | def test_log_at_startup(self): 234 | self.api.log.assertLine('Creating v1 Etsy API, base url=http://host.') 235 | 236 | 237 | 238 | 239 | 240 | 241 | class MockAPI_NoMethods(MockAPI): 242 | def _get_methods(self, method_cache): 243 | pass 244 | 245 | 246 | 247 | class MethodTableCacheTests(Test): 248 | 249 | 250 | def cache(self, method_cache=missing): 251 | self.api = MockAPI_NoMethods('apikey') 252 | self._cache = MethodTableCache(self.api, method_cache) 253 | return self._cache 254 | 255 | 256 | def test_uses_etsy_home_if_exists(self): 257 | c = self.cache() 258 | self.assertEqual(os.path.dirname(c.filename), self.scratch_dir) 259 | 260 | 261 | def test_uses_temp_dir_if_no_etsy_home(self): 262 | self.delete_scratch() 263 | c = self.cache() 264 | self.assertEqual(os.path.dirname(c.filename), tempfile.gettempdir()) 265 | 266 | 267 | def test_uses_provided_file(self): 268 | fn = os.path.join(self.scratch_dir, 'foo.json') 269 | self.assertEqual(self.cache(method_cache=fn).filename, fn) 270 | 271 | 272 | def test_multiple_versions(self): 273 | c = self.cache() 274 | 275 | class MockAPI2(MockAPI): 276 | api_version = 'v3' 277 | 278 | self.assertNotEqual(MockAPI2('key').method_cache.filename, c.filename) 279 | 280 | 281 | def get_uncached(self): 282 | c = self.cache() 283 | return c.get() 284 | 285 | 286 | def test_no_cache_file_returns_results(self): 287 | self.assertEqual(2, len(self.get_uncached())) 288 | 289 | 290 | def test_no_cache_file_writes_cache(self): 291 | self.get_uncached() 292 | self.assertTrue(self._cache.wrote_cache) 293 | 294 | 295 | def test_no_cache_file(self): 296 | self.get_uncached() 297 | self.assertFalse(self._cache.used_cache) 298 | 299 | 300 | def get_cached(self): 301 | c = self.cache() 302 | c.get() 303 | c = self.cache() 304 | return c.get() 305 | 306 | 307 | def test_caching(self): 308 | self.get_cached() 309 | self.assertTrue(self._cache.used_cache) 310 | 311 | 312 | def test_caching_returns_results(self): 313 | self.assertEqual(2, len(self.get_cached())) 314 | 315 | 316 | def test_caching_doesnt_overwrite_cache(self): 317 | self.get_cached() 318 | self.assertFalse(self._cache.wrote_cache) 319 | 320 | 321 | def make_old_cache(self): 322 | self.get_cached() 323 | fn = self._cache.filename 324 | s = os.stat(fn) 325 | os.utime(fn, (s.st_atime, s.st_mtime - 48*60*60)) 326 | 327 | 328 | def test_expired(self): 329 | self.make_old_cache() 330 | c = self.cache() 331 | c.get() 332 | self.assertFalse(c.used_cache) 333 | 334 | 335 | def test_none_passed_does_not_cache(self): 336 | self.get_cached() 337 | c = self.cache(method_cache=None) 338 | c.get() 339 | self.assertFalse(c.used_cache) 340 | 341 | 342 | def log_tester(self, method_cache=missing): 343 | return MockAPI('key', method_cache=method_cache, log=MockLog(self)) 344 | 345 | 346 | def test_logs_when_not_using_cache(self): 347 | api = self.log_tester(None) 348 | api.log.assertLine('Not using cached method table.') 349 | 350 | 351 | def test_logs_when_method_table_too_old(self): 352 | self.make_old_cache() 353 | self.log_tester().log.assertLine('Method table too old.') 354 | 355 | 356 | def test_logs_when_reading_cache(self): 357 | api = MockAPI('key') 358 | self.log_tester().log.assertLine('Reading method table cache: %s' % 359 | api.method_cache.filename) 360 | 361 | 362 | def test_logs_when_not_writing_new_cache(self): 363 | api = self.log_tester(None) 364 | api.log.assertLine( 365 | 'Method table caching disabled, not writing new cache.') 366 | 367 | 368 | def test_logs_when_writing_new_cache(self): 369 | t = self.log_tester() 370 | t.log.assertLine('Wrote method table cache: %s' % 371 | t.method_cache.filename) 372 | 373 | -------------------------------------------------------------------------------- /test/util.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase 2 | import os 3 | import shutil 4 | 5 | 6 | this_dir = os.path.realpath(os.path.dirname(__file__)) 7 | 8 | 9 | class Test(TestCase): 10 | scratch_dir = os.path.join(this_dir, 'scratch') 11 | 12 | def setUp(self): 13 | if not os.path.isdir(self.scratch_dir): 14 | os.mkdir(self.scratch_dir) 15 | 16 | def tearDown(self): 17 | self.delete_scratch() 18 | 19 | def delete_scratch(self): 20 | if os.path.isdir(self.scratch_dir): 21 | shutil.rmtree(self.scratch_dir) 22 | 23 | def assertRaises(self, cls, f, *args, **kwargs): 24 | try: 25 | f(*args, **kwargs) 26 | except cls as e: 27 | try: 28 | return e.strerror 29 | except: 30 | return e.args[0] 31 | else: 32 | name = cls.__name__ if hasattr(cls, '__name__') else str(cls) 33 | raise self.failureException("%s not raised" % name) 34 | -------------------------------------------------------------------------------- /test_v2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os, sys 4 | import oauth2 as oauth 5 | import webbrowser 6 | from etsy2 import Etsy, EtsyEnvProduction 7 | from .oauth import EtsyOAuthClient 8 | 9 | logging_enabled = True 10 | etsy_env = EtsyEnvProduction() 11 | 12 | def my_log(msg): 13 | if logging_enabled: print(msg) 14 | 15 | def write_config_file(oauth_token): 16 | os.umask(0o077) 17 | config_file = open('config.py', 'w') 18 | 19 | if config: 20 | config_file.write("oauth_consumer_key = %r\n" % config.oauth_consumer_key) 21 | config_file.write("oauth_consumer_secret = %r\n" % config.oauth_consumer_secret) 22 | 23 | if oauth_token: 24 | config_file.write("oauth_token_key = %r\n" % oauth_token.key) 25 | config_file.write("oauth_token_secret = %r\n" % oauth_token.secret) 26 | 27 | try: 28 | import config 29 | except ImportError: 30 | config = None 31 | write_config_file(oauth_token=None) 32 | 33 | if hasattr(config, 'oauth_consumer_key') and hasattr(config, 'oauth_consumer_secret'): 34 | oauth_client = EtsyOAuthClient( 35 | oauth_consumer_key=config.oauth_consumer_key, 36 | oauth_consumer_secret=config.oauth_consumer_secret, 37 | etsy_env=etsy_env) 38 | else: 39 | sys.stderr.write('ERROR: You must set oauth_consumer_key and oauth_consumer_secret in config.py\n') 40 | sys.exit(1) 41 | 42 | if hasattr(config, 'oauth_token_key') and hasattr(config, 'oauth_token_secret'): 43 | oauth_client.token = oauth.Token( 44 | key=config.oauth_token_key, 45 | secret=config.oauth_token_secret) 46 | else: 47 | webbrowser.open(oauth_client.get_signin_url()) 48 | oauth_client.set_oauth_verifier(input('Enter OAuth verifier: ')) 49 | write_config_file(oauth_client.token) 50 | 51 | etsy_api = Etsy(etsy_oauth_client=oauth_client, etsy_env=etsy_env, log=my_log) 52 | 53 | # print 'oauth access token: (key=%r; secret=%r)' % (oauth_client.token.key, oauth_client.token.secret) 54 | 55 | print('findAllShopListingsActive => %r' % etsy_api.findAllShopListingsActive(shop_id=config.user_id, sort_on='created', limit=1)) 56 | 57 | # print('getListing => %r' % etsy_api.getListing(listing_id=63067548)) 58 | 59 | print('findAllUserShippingTemplates => %r' % etsy_api.findAllUserShippingTemplates(user_id=config.user_id)) 60 | 61 | # TODO write UPDATE/INSERT test that doesnt cost money 62 | # TODO write test that excerises boolean param types 63 | 64 | #def testCreateListing(): 65 | # print("Creating listing...") 66 | # 67 | # result = etsy_api.createListing( 68 | # description=config.description, 69 | # title=config.title, 70 | # price=config.price, 71 | # tags=config.tags, 72 | # materials=config.materials, 73 | # shipping_template_id=config.shipping_template_id, 74 | # shop_section_id=config.shop_section_id, 75 | # quantity=config.quantity) 76 | # 77 | # listing_id = result[0]['listing_id'] 78 | # 79 | # print("Created listing with listing id %d" % listing_id) 80 | # 81 | # result = etsy_api.uploadListingImage(listing_id=listing_id, image=config.image_file) 82 | # 83 | # print("Result of uploading image: %r" % result) 84 | # 85 | #testCreateListing() 86 | 87 | --------------------------------------------------------------------------------